|
| 1 | +/* eslint-disable vue-libs/no-async-functions */ |
| 2 | +const path = require('path') |
| 3 | +const fs = require('fs') |
| 4 | + |
| 5 | +function readAppManifest (cwd) { |
| 6 | + const manifestPath = path.join(cwd, 'public/manifest.json') |
| 7 | + if (fs.existsSync(manifestPath)) { |
| 8 | + try { |
| 9 | + return JSON.parse(fs.readFileSync(manifestPath, { encoding: 'utf8' })) |
| 10 | + } catch (e) { |
| 11 | + console.log(`Can't read JSON in ${manifestPath}`) |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function updateAppManifest (cwd, handler) { |
| 17 | + const manifestPath = path.join(cwd, 'public/manifest.json') |
| 18 | + if (fs.existsSync(manifestPath)) { |
| 19 | + try { |
| 20 | + const manifest = JSON.parse(fs.readFileSync(manifestPath, { encoding: 'utf8' })) |
| 21 | + handler(manifest) |
| 22 | + fs.writeFileSync(manifestPath, JSON.stringify(manifest), { encoding: 'utf8' }) |
| 23 | + } catch (e) { |
| 24 | + console.log(`Can't update JSON in ${manifestPath}`) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +module.exports = api => { |
| 30 | + // Config file |
| 31 | + api.describeConfig({ |
| 32 | + id: 'pwa', |
| 33 | + name: 'PWA', |
| 34 | + description: 'pwa.config.pwa.description', |
| 35 | + link: 'https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa#configuration', |
| 36 | + icon: 'mobile.xm', |
| 37 | + files: { |
| 38 | + js: ['vue.config.js'] |
| 39 | + }, |
| 40 | + onRead: ({ data, cwd }) => { |
| 41 | + const manifest = readAppManifest(cwd) |
| 42 | + return { |
| 43 | + prompts: [ |
| 44 | + { |
| 45 | + name: 'workboxPluginMode', |
| 46 | + type: 'list', |
| 47 | + message: 'Plugin mode', |
| 48 | + description: 'This allows you to the choose between the two modes supported by the underlying `workbox-webpack-plugin`', |
| 49 | + link: 'https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#which_plugin_to_use', |
| 50 | + default: 'GenerateSW', |
| 51 | + value: data.pwa && data.pwa.workboxPluginMode, |
| 52 | + choices: [ |
| 53 | + { |
| 54 | + name: 'GenerateSW', |
| 55 | + value: 'GenerateSW' |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'InjectManifest', |
| 59 | + value: 'InjectManifest' |
| 60 | + } |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + name: 'name', |
| 65 | + type: 'input', |
| 66 | + message: 'App name', |
| 67 | + description: 'Used as the value for the `apple-mobile-web-app-title` meta tag in the generated HTML.', |
| 68 | + value: data.pwa && data.pwa.name |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'themeColor', |
| 72 | + type: 'input', |
| 73 | + message: 'Theme color', |
| 74 | + description: 'Color used to theme the browser', |
| 75 | + default: '#4DBA87', |
| 76 | + value: data.pwa && data.pwa.themeColor |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'backgroundColor', |
| 80 | + type: 'input', |
| 81 | + message: 'Splash background color', |
| 82 | + description: 'Background color used for the app splash screen', |
| 83 | + default: '#000000', |
| 84 | + value: manifest && manifest.background_color, |
| 85 | + skipSave: true |
| 86 | + }, |
| 87 | + { |
| 88 | + name: 'msTileColor', |
| 89 | + type: 'input', |
| 90 | + message: 'Windows app tile color', |
| 91 | + description: 'Color used for the app tile on Windows', |
| 92 | + default: '#000000', |
| 93 | + value: data.pwa && data.pwa.msTileColor |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'appleMobileWebAppStatusBarStyle', |
| 97 | + type: 'input', |
| 98 | + message: 'Apple mobile status bar style', |
| 99 | + description: 'Style for the web app status bar on iOS', |
| 100 | + default: 'default', |
| 101 | + value: data.pwa && data.pwa.appleMobileWebAppStatusBarStyle |
| 102 | + } |
| 103 | + ] |
| 104 | + } |
| 105 | + }, |
| 106 | + onWrite: async ({ api, prompts, cwd }) => { |
| 107 | + const result = {} |
| 108 | + for (const prompt of prompts.filter(p => !p.raw.skipSave)) { |
| 109 | + result[`pwa.${prompt.id}`] = await api.getAnswer(prompt.id) |
| 110 | + } |
| 111 | + api.setData(result) |
| 112 | + |
| 113 | + // Update app manifest |
| 114 | + |
| 115 | + const name = result['pwa.name'] |
| 116 | + if (name) { |
| 117 | + updateAppManifest(cwd, manifest => { |
| 118 | + manifest.name = name |
| 119 | + manifest.short_name = name |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + const themeColor = result['pwa.themeColor'] |
| 124 | + if (themeColor) { |
| 125 | + updateAppManifest(cwd, manifest => { |
| 126 | + manifest.theme_color = themeColor |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + const backgroundColor = await api.getAnswer('backgroundColor') |
| 131 | + if (backgroundColor) { |
| 132 | + updateAppManifest(cwd, manifest => { |
| 133 | + manifest.background_color = backgroundColor |
| 134 | + }) |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | +} |
0 commit comments