Skip to content

Commit 6a244cf

Browse files
committed
fix(build): build command returns and resolves a promise
1 parent 7e4afa6 commit 6a244cf

File tree

1 file changed

+125
-123
lines changed

1 file changed

+125
-123
lines changed

index.js

Lines changed: 125 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -36,139 +36,141 @@ module.exports = (api, options) => {
3636
`See https://www.electron.build/cli for cli options\n` +
3737
`See https://nklayman.github.io/vue-cli-plugin-electron-builder/ for more details about this plugin.`
3838
},
39-
async (args, rawArgs) => {
40-
// Use custom config for webpack
41-
process.env.IS_ELECTRON = true
42-
const builder = require('electron-builder')
43-
const yargs = require('yargs')
44-
// Import the yargs options from electron-builder
45-
const configureBuildCommand = require('electron-builder/out/builder')
46-
.configureBuildCommand
47-
// Prevent custom args from interfering with electron-builder
48-
const removeArg = (arg, count) => {
49-
const index = rawArgs.indexOf(arg)
50-
if (index !== -1) rawArgs.splice(index, count)
51-
}
52-
removeArg('--mode', 2)
53-
removeArg('--dest', 2)
54-
removeArg('--legacy', 1)
55-
removeArg('--skipBundle', 1)
56-
// Parse the raw arguments using electron-builder yargs config
57-
const builderArgs = yargs
58-
.command(['build', '*'], 'Build', configureBuildCommand)
59-
.parse(rawArgs)
60-
// Base config used in electron-builder build
61-
const outputDir = args.dest || pluginOptions.outputDir || 'dist_electron'
62-
const defaultBuildConfig = {
63-
directories: {
64-
output: outputDir,
65-
app: `${outputDir}/bundled`
66-
},
67-
files: ['**'],
68-
extends: null
69-
}
70-
// User-defined electron-builder config, overwrites/adds to default config
71-
const userBuildConfig = pluginOptions.builderOptions || {}
72-
if (args.skipBundle) {
73-
console.log('Not bundling app as --skipBundle was passed')
74-
// Build with electron-builder
75-
buildApp()
76-
} else {
77-
// Arguments to be passed to renderer build
78-
const vueArgs = {
79-
_: [],
80-
// For the cli-ui webpack dashboard
81-
dashboard: args.dashboard,
82-
// Make sure files are outputted to proper directory
83-
dest: outputDir + '/bundled',
84-
// Enable modern mode unless --legacy is passed
85-
modern: !args.legacy
39+
(args, rawArgs) =>
40+
new Promise(async (resolve, reject) => {
41+
// Use custom config for webpack
42+
process.env.IS_ELECTRON = true
43+
const builder = require('electron-builder')
44+
const yargs = require('yargs')
45+
// Import the yargs options from electron-builder
46+
const configureBuildCommand = require('electron-builder/out/builder')
47+
.configureBuildCommand
48+
// Prevent custom args from interfering with electron-builder
49+
const removeArg = (arg, count) => {
50+
const index = rawArgs.indexOf(arg)
51+
if (index !== -1) rawArgs.splice(index, count)
8652
}
87-
// Set the base url so that the app protocol is used
88-
options.baseUrl = './'
89-
console.log('Bundling render process:')
90-
// Build the render process with the custom args
91-
await api.service.run('build', vueArgs)
92-
// Copy package.json to output dir
93-
fs.copySync(
94-
api.resolve('./package.json'),
95-
`${outputDir}/bundled/package.json`
96-
)
97-
// Prevent electron-builder from installing app deps
98-
fs.ensureDirSync(`${outputDir}/bundled/node_modules`)
99-
// Copy fonts to css/fonts. Fixes some issues with static font imports
100-
if (fs.existsSync(api.resolve(outputDir + '/bundled/fonts'))) {
101-
fs.ensureDirSync(api.resolve(outputDir + '/bundled/css/fonts'))
53+
removeArg('--mode', 2)
54+
removeArg('--dest', 2)
55+
removeArg('--legacy', 1)
56+
removeArg('--skipBundle', 1)
57+
// Parse the raw arguments using electron-builder yargs config
58+
const builderArgs = yargs
59+
.command(['build', '*'], 'Build', configureBuildCommand)
60+
.parse(rawArgs)
61+
// Base config used in electron-builder build
62+
const outputDir =
63+
args.dest || pluginOptions.outputDir || 'dist_electron'
64+
const defaultBuildConfig = {
65+
directories: {
66+
output: outputDir,
67+
app: `${outputDir}/bundled`
68+
},
69+
files: ['**'],
70+
extends: null
71+
}
72+
// User-defined electron-builder config, overwrites/adds to default config
73+
const userBuildConfig = pluginOptions.builderOptions || {}
74+
if (args.skipBundle) {
75+
console.log('Not bundling app as --skipBundle was passed')
76+
// Build with electron-builder
77+
buildApp()
78+
} else {
79+
// Arguments to be passed to renderer build
80+
const vueArgs = {
81+
_: [],
82+
// For the cli-ui webpack dashboard
83+
dashboard: args.dashboard,
84+
// Make sure files are outputted to proper directory
85+
dest: outputDir + '/bundled',
86+
// Enable modern mode unless --legacy is passed
87+
modern: !args.legacy
88+
}
89+
// Set the base url so that the app protocol is used
90+
options.baseUrl = './'
91+
console.log('Bundling render process:')
92+
// Build the render process with the custom args
93+
await api.service.run('build', vueArgs)
94+
// Copy package.json to output dir
10295
fs.copySync(
103-
api.resolve(outputDir + '/bundled/fonts'),
104-
api.resolve(outputDir + '/bundled/css/fonts')
96+
api.resolve('./package.json'),
97+
`${outputDir}/bundled/package.json`
10598
)
106-
}
107-
// Build the main process into the renderer process output dir
108-
const bundle = bundleMain({
109-
mode: 'build',
110-
api,
111-
args,
112-
pluginOptions,
113-
outputDir,
114-
mainProcessFile,
115-
mainProcessChain,
116-
usesTypescript
117-
})
118-
console.log('Bundling main process:\n')
119-
bundle.run((err, stats) => {
120-
if (err) {
121-
console.error(err.stack || err)
122-
if (err.details) {
123-
console.error(err.details)
124-
}
125-
process.exit(1)
99+
// Prevent electron-builder from installing app deps
100+
fs.ensureDirSync(`${outputDir}/bundled/node_modules`)
101+
// Copy fonts to css/fonts. Fixes some issues with static font imports
102+
if (fs.existsSync(api.resolve(outputDir + '/bundled/fonts'))) {
103+
fs.ensureDirSync(api.resolve(outputDir + '/bundled/css/fonts'))
104+
fs.copySync(
105+
api.resolve(outputDir + '/bundled/fonts'),
106+
api.resolve(outputDir + '/bundled/css/fonts')
107+
)
126108
}
109+
// Build the main process into the renderer process output dir
110+
const bundle = bundleMain({
111+
mode: 'build',
112+
api,
113+
args,
114+
pluginOptions,
115+
outputDir,
116+
mainProcessFile,
117+
mainProcessChain,
118+
usesTypescript
119+
})
120+
console.log('Bundling main process:\n')
121+
bundle.run((err, stats) => {
122+
if (err) {
123+
console.error(err.stack || err)
124+
if (err.details) {
125+
console.error(err.details)
126+
}
127+
return reject(err)
128+
}
127129

128-
const info = stats.toJson()
129-
130-
if (stats.hasErrors()) {
131-
console.error(info.errors)
132-
process.exit(1)
133-
}
130+
const info = stats.toJson()
134131

135-
if (stats.hasWarnings()) {
136-
console.warn(info.warnings)
137-
}
132+
if (stats.hasErrors()) {
133+
return reject(info.errors)
134+
}
138135

139-
console.log(
140-
stats.toString({
141-
chunks: false,
142-
colors: true
143-
})
144-
)
136+
if (stats.hasWarnings()) {
137+
console.warn(info.warnings)
138+
}
145139

146-
buildApp()
147-
})
148-
}
149-
function buildApp () {
150-
console.log('\nBuilding app with electron-builder:\n')
151-
// Build the app using electron builder
152-
builder
153-
.build({
154-
// Args parsed with yargs
155-
...builderArgs,
156-
config: merge(
157-
defaultBuildConfig,
158-
// User-defined config overwrites defaults
159-
userBuildConfig
140+
console.log(
141+
stats.toString({
142+
chunks: false,
143+
colors: true
144+
})
160145
)
146+
147+
buildApp()
161148
})
162-
.then(() => {
163-
// handle result
164-
console.log('\nBuild complete!\n')
165-
})
166-
.catch(err => {
167-
// handle error
168-
throw err
169-
})
170-
}
171-
}
149+
}
150+
function buildApp () {
151+
console.log('\nBuilding app with electron-builder:\n')
152+
// Build the app using electron builder
153+
builder
154+
.build({
155+
// Args parsed with yargs
156+
...builderArgs,
157+
config: merge(
158+
defaultBuildConfig,
159+
// User-defined config overwrites defaults
160+
userBuildConfig
161+
)
162+
})
163+
.then(() => {
164+
// handle result
165+
console.log('\nBuild complete!\n')
166+
resolve()
167+
})
168+
.catch(err => {
169+
// handle error
170+
return reject(err)
171+
})
172+
}
173+
})
172174
)
173175
api.registerCommand(
174176
'serve:electron',

0 commit comments

Comments
 (0)