Skip to content

Commit 307b22b

Browse files
committed
feat(ui): Add cli ui support
1 parent 0e731f8 commit 307b22b

File tree

4 files changed

+404
-8
lines changed

4 files changed

+404
-8
lines changed

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ module.exports = (api, options) => {
4848
],
4949
extends: null
5050
}
51+
const vueArgs = {
52+
_: [],
53+
dashboard: args.dashboard,
54+
dest: outputDir + '/bundled'
55+
}
5156
const userBuildConfig = pluginOptions.builderOptions || {}
5257
const mainConfig = new Config()
5358
mainConfig
@@ -80,12 +85,7 @@ module.exports = (api, options) => {
8085
console.log('Bundling render process:')
8186
rendererConfig.target('electron-renderer').output.publicPath('./')
8287
rendererConfig.node.set('__dirname', false).set('__filename', false)
83-
await buildRenderer(
84-
{ _: [], dest: outputDir + '/bundled' },
85-
api,
86-
options,
87-
rendererConfig
88-
)
88+
await buildRenderer(vueArgs, api, options, rendererConfig)
8989
if (fs.existsSync(api.resolve(outputDir + '/bundled/fonts'))) {
9090
fs.mkdirSync(api.resolve(outputDir + '/bundled/css/fonts'))
9191
fs.copySync(
@@ -150,7 +150,7 @@ module.exports = (api, options) => {
150150
usage: 'vue-cli-service serve:electron',
151151
details: `See https://github.com/nklayman/vue-cli-plugin-electron-builder for more details about this plugin.`
152152
},
153-
() => {
153+
args => {
154154
const execa = require('execa')
155155
const serve = require('@vue/cli-service/lib/commands/serve').serve
156156
const rendererConfig = api.resolveChainableWebpackConfig()
@@ -216,7 +216,12 @@ module.exports = (api, options) => {
216216

217217
console.log('\nStarting development server:\n')
218218

219-
serve({ _: [] }, api, options, rendererConfig).then(server => {
219+
serve(
220+
{ _: [], dashboard: args.dashboard },
221+
api,
222+
options,
223+
rendererConfig
224+
).then(server => {
220225
console.log('\nLaunching Electron...\n')
221226
const child = execa(
222227
`./node_modules/.bin/electron ${outputDir}/background.js`,

logo.png

6.57 KB
Loading

ui.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)