|
| 1 | +const path = require('path') |
| 2 | +const fs = require('fs-extra') |
| 3 | +const prompts = require('./uiOptions') |
| 4 | + |
| 5 | +module.exports = api => { |
| 6 | + const { setSharedData, removeSharedData } = api.namespace( |
| 7 | + 'webpack-dashboard-' |
| 8 | + ) |
| 9 | + |
| 10 | + let firstRun = true |
| 11 | + let hadFailed = false |
| 12 | + let modernMode = false |
| 13 | + |
| 14 | + function resetSharedData (key) { |
| 15 | + setSharedData(`${key}-status`, null) |
| 16 | + setSharedData(`${key}-progress`, 0) |
| 17 | + setSharedData(`${key}-operations`, null) |
| 18 | + setSharedData(`${key}-stats`, null) |
| 19 | + setSharedData(`${key}-sizes`, null) |
| 20 | + setSharedData(`${key}-problems`, null) |
| 21 | + } |
| 22 | + |
| 23 | + async function onWebpackMessage ({ data: message }) { |
| 24 | + if (message.webpackDashboardData) { |
| 25 | + const type = message.webpackDashboardData.type |
| 26 | + for (const data of message.webpackDashboardData.value) { |
| 27 | + if (data.type === 'stats') { |
| 28 | + // Stats are read from a file |
| 29 | + const statsFile = path.resolve( |
| 30 | + process.cwd(), |
| 31 | + `./node_modules/.stats-${type}.json` |
| 32 | + ) |
| 33 | + const value = await fs.readJson(statsFile) |
| 34 | + setSharedData(`${type}-${data.type}`, value) |
| 35 | + await fs.remove(statsFile) |
| 36 | + } else if ( |
| 37 | + type.indexOf('build') !== -1 && |
| 38 | + modernMode && |
| 39 | + data.type === 'progress' |
| 40 | + ) { |
| 41 | + // Progress is shared between 'build' and 'build-modern' |
| 42 | + // 'build' first and then 'build-modern' |
| 43 | + const value = type === 'build' ? data.value / 2 : (data.value + 1) / 2 |
| 44 | + // We display the same progress bar for both |
| 45 | + for (const t of ['build', 'build-modern']) { |
| 46 | + setSharedData(`${t}-${data.type}`, value) |
| 47 | + } |
| 48 | + } else { |
| 49 | + setSharedData(`${type}-${data.type}`, data.value) |
| 50 | + |
| 51 | + // Notifications |
| 52 | + if (type === 'serve' && data.type === 'status') { |
| 53 | + if (data.value === 'Failed') { |
| 54 | + api.notify({ |
| 55 | + title: 'Build failed', |
| 56 | + message: 'The build has errors.', |
| 57 | + icon: 'error' |
| 58 | + }) |
| 59 | + hadFailed = true |
| 60 | + } else if (data.value === 'Success') { |
| 61 | + if (hadFailed) { |
| 62 | + api.notify({ |
| 63 | + title: 'Build fixed', |
| 64 | + message: 'The build succeeded.', |
| 65 | + icon: 'done' |
| 66 | + }) |
| 67 | + hadFailed = false |
| 68 | + } else if (firstRun) { |
| 69 | + api.notify({ |
| 70 | + title: 'App ready', |
| 71 | + message: 'The build succeeded.', |
| 72 | + icon: 'done' |
| 73 | + }) |
| 74 | + firstRun = false |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + api.describeTask({ |
| 84 | + match: /vue-cli-service build:electron/, |
| 85 | + description: 'Build your app for production with electron-builder', |
| 86 | + link: |
| 87 | + 'https://github.com/nklayman/vue-cli-plugin-electron-builder/tree/v1-dev#build-command', |
| 88 | + prompts, |
| 89 | + onBeforeRun: ({ answers, args }) => { |
| 90 | + // Args |
| 91 | + if (answers.dir) args.push('--dir') |
| 92 | + if (answers.windows) { |
| 93 | + args.push('--windows') |
| 94 | + answers.windowsTargets.forEach(t => { |
| 95 | + args.push(t) |
| 96 | + }) |
| 97 | + } |
| 98 | + if (answers.linux) { |
| 99 | + args.push('--linux') |
| 100 | + answers.linuxTargets.forEach(t => { |
| 101 | + args.push(t) |
| 102 | + }) |
| 103 | + } |
| 104 | + if (answers.macos) { |
| 105 | + args.push('--macos') |
| 106 | + answers.macosTargets.forEach(t => { |
| 107 | + args.push(t) |
| 108 | + }) |
| 109 | + } |
| 110 | + answers.archs.forEach(a => { |
| 111 | + args.push(`--${a}`) |
| 112 | + }) |
| 113 | + setSharedData('modern-mode', (modernMode = !!answers.modern)) |
| 114 | + args.push('--dashboard') |
| 115 | + // Data |
| 116 | + resetSharedData('build') |
| 117 | + resetSharedData('build-modern') |
| 118 | + }, |
| 119 | + onRun: () => { |
| 120 | + api.ipcOn(onWebpackMessage) |
| 121 | + }, |
| 122 | + onExit: () => { |
| 123 | + api.ipcOff(onWebpackMessage) |
| 124 | + }, |
| 125 | + views: [ |
| 126 | + { |
| 127 | + id: 'vue-webpack-dashboard', |
| 128 | + label: 'vue-webpack.dashboard.title', |
| 129 | + icon: 'dashboard', |
| 130 | + component: 'vue-webpack-dashboard' |
| 131 | + }, |
| 132 | + { |
| 133 | + id: 'vue-webpack-analyzer', |
| 134 | + label: 'vue-webpack.analyzer.title', |
| 135 | + icon: 'donut_large', |
| 136 | + component: 'vue-webpack-analyzer' |
| 137 | + } |
| 138 | + ], |
| 139 | + defaultView: 'vue-webpack-dashboard' |
| 140 | + }) |
| 141 | + api.describeTask({ |
| 142 | + match: /vue-cli-service serve:electron/, |
| 143 | + description: 'Serve your app, launch electron', |
| 144 | + link: |
| 145 | + 'https://github.com/nklayman/vue-cli-plugin-electron-builder/tree/v1-dev#serve-command', |
| 146 | + onBeforeRun: ({ answers, args }) => { |
| 147 | + // Args |
| 148 | + args.push('--dashboard') |
| 149 | + |
| 150 | + // Data |
| 151 | + resetSharedData('serve') |
| 152 | + removeSharedData('serve-url') |
| 153 | + firstRun = true |
| 154 | + hadFailed = false |
| 155 | + }, |
| 156 | + onRun: () => { |
| 157 | + api.ipcOn(onWebpackMessage) |
| 158 | + }, |
| 159 | + onExit: () => { |
| 160 | + api.ipcOff(onWebpackMessage) |
| 161 | + removeSharedData('serve-url') |
| 162 | + }, |
| 163 | + views: [ |
| 164 | + { |
| 165 | + id: 'vue-webpack-dashboard', |
| 166 | + label: 'vue-webpack.dashboard.title', |
| 167 | + icon: 'dashboard', |
| 168 | + component: 'vue-webpack-dashboard' |
| 169 | + } |
| 170 | + ], |
| 171 | + defaultView: 'vue-webpack-dashboard' |
| 172 | + }) |
| 173 | +} |
0 commit comments