Skip to content

Commit d660fbb

Browse files
Karen BaneyKaren Baney
authored andcommitted
update prompts. Update generator
1 parent da7c204 commit d660fbb

File tree

3 files changed

+136
-63
lines changed

3 files changed

+136
-63
lines changed

generator/index.js

Lines changed: 109 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,122 @@
11
module.exports = (api, options, rootOptions) => {
2-
if (options.workboxPwa) {
2+
const fs = require('fs')
3+
const ext = api.hasPlugin('typescript') ? 'ts' : 'js'
4+
const fileEncoding = { encoding: 'utf-8' }
5+
6+
api.extendPackage({
7+
scripts: {
8+
'pwa-build': 'vue-cli-service build --mode pwalocalserve',
9+
'pwa-serve': 'npm run pwa-build && serve -s dist -l 8080'
10+
},
11+
dependencies: {
12+
'workbox-cacheable-response': '^6.1.5',
13+
'workbox-core': '^6.1.5',
14+
'workbox-expiration': '^6.1.5',
15+
'workbox-routing': '^6.1.5',
16+
'workbox-strategies': '^6.1.5',
17+
'workbox-window': '^6.1.5'
18+
},
19+
devDependencies: {
20+
'workbox-webpack-plugin': '^6.1.5'
21+
}
22+
})
23+
24+
25+
// set up pwaconfiguration
26+
let pwaConfig = {
27+
name: 'PWA App',
28+
shorName: 'App',
29+
themeColor: '#4DBA87',
30+
msTileColor: '#000000',
31+
manifestOptions: {
32+
start_url: '/'
33+
}
34+
}
35+
36+
const manifestType = options.manifestType[0]
37+
console.log(`manifestType: ${options.manifestType}`)
38+
pwaConfig.workboxPluginMode = manifestType
39+
40+
if (manifestType == 'InjectManifest') {
41+
pwaConfig. workboxOptions = {
42+
swSrc: './src/sw.js',
43+
swDest: 'service-worker.js'
44+
}
45+
}
46+
47+
if (options.vue?.pwa) {
48+
// update existing?
49+
console.log('has vue pwa')
50+
} else {
351
api.extendPackage({
4-
scripts: {
5-
'pwa-build': 'vue-cli-service build --mode pwalocalserve',
6-
'pwa-serve': 'npm run pwa-build && serve -s dist -l 8080'
7-
},
8-
dependencies: {
9-
'workbox-cacheable-response': '^6.1.5',
10-
'workbox-core': '^6.1.5',
11-
'workbox-expiration': '^6.1.5',
12-
'workbox-routing': '^6.1.5',
13-
'workbox-strategies': '^6.1.5',
14-
'workbox-window': '^6.1.5'
15-
},
16-
devDependencies: {
17-
'workbox-webpack-plugin': '^6.1.5'
52+
vue: {
53+
pwa: pwaConfig
1854
}
1955
})
2056

21-
api.render('./template', {
22-
...options,
23-
})
57+
console.log('pwa config set up complete')
58+
}
2459

25-
// Inject service worker registration into main.js/.ts
26-
const importRegister = `\nimport register from './service-worker/register-service-worker'`
27-
api.onCreateComplete(() => {
28-
// inject to main.js
29-
const fs = require('fs')
30-
const ext = api.hasPlugin('typescript') ? 'ts' : 'js'
31-
const mainPath = api.resolve(`./src/main.${ext}`)
60+
// TODO: this is putting it in the root :(
61+
api.render('./template/public', {
62+
...options,
63+
})
3264

33-
// get existing content
34-
let contentMain = fs.readFileSync(mainPath, { encoding: 'utf-8' })
65+
api.render('./template/src', {
66+
...options,
67+
})
3568

36-
// modify add to content
37-
let addedContent = '\nregister()\n'
38-
addedContent += `\nif (process.env.NODE_ENV === 'development' || process.env.VUE_APP_PWA_LOCAL_SERVE === 'true') {`
39-
addedContent += '\n console.log(`PWA Local Serve: ${process.env.VUE_APP_PWA_LOCAL_SERVE}`) // eslint-disable no-console'
40-
addedContent += '\n console.log(`Node Env: ${process.env.NODE_ENV}`) // eslint-disable no-console'
41-
addedContent += '\n}\n'
69+
// TODO: this errors with cwd command. It has to be a path. So I need another way to copy env files?
70+
// api.render('./template/_env.pwalocalserve')
4271

43-
contentMain += addedContent
72+
// console.log('copying files complete')
4473

45-
const lines = contentMain.split(/\r?\n/g).reverse()
74+
// console.log('starting .env setup')
75+
// if (fs.existsSync('.env')) {
76+
// const envFile = api.resolve('.env')
77+
// let envContent = fs.readFileSync(envFile, fileEncoding)
78+
// envContent += '\nVUE_APP_PWA_LOCAL_SERVE=false'
79+
// fs.writeFileSync(envFile, envContent, fileEncoding)
80+
// } else {
81+
// api.render('./template/_env')
82+
// }
4683

47-
// inject import
48-
const lastImportIndex = lines.findIndex(line => line.match(/^import/))
49-
lines[lastImportIndex] += importRegister
84+
// // if there is an .env.development file, copy it to .env.pwalocalserve and add the new setting
85+
// if (fs.existsSync('.env.development')) {
86+
// fs.copyFileSync('.env.development', '.env.pwalocalserve')
87+
// let envPwaFile = api.resolve('.env.pwalocalserve')
88+
// let envPwaContent = fs.readFileSync(envPwaFile, fileEncoding)
89+
// envPwaContent += '\nVUE_APP_PWA_LOCAL_SERVE=true'
90+
// fs.writeFileSync(envPwaFile, envPwaContent, fileEncoding)
91+
// } else {
92+
// api.render('./template/_env.pwalocalserve')
93+
// }
5094

51-
// rebuild content
52-
contentMain = lines.reverse().join(`\n`)
53-
fs.writeFileSync(mainPath, contentMain, { encoding: 'utf-8' })
54-
})
55-
}
95+
// console.log('finished .env setup')
96+
97+
// Inject service worker registration into main.js/.ts
98+
api.onCreateComplete(() => {
99+
const importRegister = `\nimport register from './service-worker/register-service-worker'`
100+
const mainPath = api.resolve(`./src/main.${ext}`)
101+
// get existing content
102+
let contentMain = fs.readFileSync(mainPath, fileEncoding)
103+
104+
// modify add to content
105+
let addedContent = '\nregister()\n'
106+
addedContent += `\nif (process.env.NODE_ENV === 'development' || process.env.VUE_APP_PWA_LOCAL_SERVE === 'true') {`
107+
addedContent += '\n console.log(`PWA Local Serve: ${process.env.VUE_APP_PWA_LOCAL_SERVE}`) // eslint-disable no-console'
108+
addedContent += '\n console.log(`Node Env: ${process.env.NODE_ENV}`) // eslint-disable no-console'
109+
addedContent += '\n}\n'
110+
111+
contentMain += addedContent
112+
const lines = contentMain.split(/\r?\n/g).reverse()
113+
114+
// inject import
115+
const lastImportIndex = lines.findIndex(line => line.match(/^import/))
116+
lines[lastImportIndex] += importRegister
117+
118+
// rebuild content
119+
contentMain = lines.reverse().join(`\n`)
120+
fs.writeFileSync(mainPath, contentMain, fileEncoding)
121+
})
56122
}

generator/template/vue.config.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

prompts.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
module.exports = [
22
{
3-
name: `workboxPwa`,
4-
type: 'confirm',
5-
message: 'Add workbox pwa to your application?',
6-
default: false
3+
name: 'manifestType',
4+
type: 'checkbox',
5+
message: 'Which type of workbox manifest to use?',
6+
choices: [
7+
{
8+
name: 'Inject Manifest (more flexible)',
9+
value: 'InjectManifest',
10+
checked: true
11+
},
12+
{
13+
name: 'Generate Manifest (auto-generated)',
14+
value: 'GenerateSW'
15+
}
16+
],
17+
default: 'InjectManifest'
718
}
19+
// {
20+
// name: 'addManualUpdate',
21+
// type: 'confirm',
22+
// message: 'Add code for prompt to user for manual updates of the service worker (y/N)?',
23+
// default: false
24+
// },
25+
// {
26+
// name: 'addAutoUpdate',
27+
// type: 'confirm',
28+
// message: 'Add code to auto update the service worker at an interval (default 1 hr) (y/N)?',
29+
// default: false
30+
// }
831
]

0 commit comments

Comments
 (0)