Skip to content

Commit 69a6b0b

Browse files
committed
refactor(cli-serivce): use async functions when running service commands
1 parent da515ac commit 69a6b0b

File tree

3 files changed

+130
-128
lines changed

3 files changed

+130
-128
lines changed

packages/@vue/cli-service/lib/Service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module.exports = class Service {
112112
}
113113
}
114114

115-
run (name, args = {}, rawArgv = []) {
115+
async run (name, args = {}, rawArgv = []) {
116116
args._ = args._ || []
117117
let command = this.commands[name]
118118
if (!command && name) {
@@ -126,7 +126,7 @@ module.exports = class Service {
126126
rawArgv.shift()
127127
}
128128
const { fn } = command
129-
return Promise.resolve(fn(args, rawArgv))
129+
await fn(args, rawArgv)
130130
}
131131

132132
resolveChainableWebpackConfig () {

packages/@vue/cli-service/lib/commands/build/index.js

+30-34
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = (api, options) => {
2020
'--target': `app | lib | wc | wc-async (default: ${defaults.target})`,
2121
'--name': `name for lib or web-component mode (default: "name" in package.json or entry filename)`
2222
}
23-
}, args => {
23+
}, async function build (args) {
2424
args.entry = args.entry || args._[0]
2525
for (const key in defaults) {
2626
if (args[key] == null) {
@@ -122,49 +122,45 @@ module.exports = (api, options) => {
122122
process.exit(1)
123123
}
124124

125+
await fs.remove(targetDir)
126+
125127
return new Promise((resolve, reject) => {
126-
fs.remove(targetDir, err => {
128+
webpack(webpackConfig, (err, stats) => {
129+
stopSpinner(false)
127130
if (err) {
128131
return reject(err)
129132
}
130133

131-
webpack(webpackConfig, (err, stats) => {
132-
stopSpinner(false)
133-
if (err) {
134-
return reject(err)
135-
}
136-
137-
if (stats.hasErrors()) {
138-
return reject(`Build failed with errors.`)
139-
}
134+
if (stats.hasErrors()) {
135+
return reject(`Build failed with errors.`)
136+
}
140137

141-
if (!args.silent) {
142-
const targetDirShort = path.relative(
143-
api.service.context,
144-
targetDir
145-
)
146-
log(formatStats(stats, targetDirShort, api))
147-
if (args.target === 'app') {
148-
done(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.\n`)
149-
if (
150-
options.baseUrl === '/' &&
151-
// only log the tips if this is the first build
152-
!fs.existsSync(api.resolve('node_modules/.cache'))
153-
) {
154-
info(`The app is built assuming that it will be deployed at the root of a domain.`)
155-
info(`If you intend to deploy it under a subpath, update the ${chalk.green('baseUrl')} option`)
156-
info(`in your project config (${chalk.cyan(`vue.config.js`)} or ${chalk.green('"vue"')} field in ${chalk.cyan(`package.json`)}).\n`)
157-
}
138+
if (!args.silent) {
139+
const targetDirShort = path.relative(
140+
api.service.context,
141+
targetDir
142+
)
143+
log(formatStats(stats, targetDirShort, api))
144+
if (args.target === 'app') {
145+
done(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.\n`)
146+
if (
147+
options.baseUrl === '/' &&
148+
// only log the tips if this is the first build
149+
!fs.existsSync(api.resolve('node_modules/.cache'))
150+
) {
151+
info(`The app is built assuming that it will be deployed at the root of a domain.`)
152+
info(`If you intend to deploy it under a subpath, update the ${chalk.green('baseUrl')} option`)
153+
info(`in your project config (${chalk.cyan(`vue.config.js`)} or ${chalk.green('"vue"')} field in ${chalk.cyan(`package.json`)}).\n`)
158154
}
159155
}
156+
}
160157

161-
// test-only signal
162-
if (process.env.VUE_CLI_TEST) {
163-
console.log('Build complete.')
164-
}
158+
// test-only signal
159+
if (process.env.VUE_CLI_TEST) {
160+
console.log('Build complete.')
161+
}
165162

166-
resolve()
167-
})
163+
resolve()
168164
})
169165
})
170166
})

packages/@vue/cli-service/lib/commands/serve.js

+98-92
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const {
22
info,
3-
error,
43
hasYarn,
54
openBrowser
65
} = require('@vue/cli-shared-utils')
@@ -23,7 +22,7 @@ module.exports = (api, options) => {
2322
'--port': `specify port (default: ${defaults.port})`,
2423
'--https': `use https (default: ${defaults.https})`
2524
}
26-
}, args => {
25+
}, async function serve (args) {
2726
info('Starting development server...')
2827

2928
api.setMode(args.mode || defaults.mode)
@@ -40,45 +39,109 @@ module.exports = (api, options) => {
4039
const prepareProxy = require('../util/prepareProxy')
4140
const launchEditorMiddleware = require('launch-editor-middleware')
4241

42+
// load user devServer options
4343
const projectDevServerOptions = options.devServer || {}
44+
45+
// resolve webpack config
46+
const webpackConfig = api.resolveWebpackConfig()
47+
48+
// inject dev & hot-reload middleware entries
49+
if (!isProduction) {
50+
const devClients = [
51+
// dev server client
52+
require.resolve(`webpack-dev-server/client`),
53+
// hmr client
54+
require.resolve(projectDevServerOptions.hotOnly
55+
? 'webpack/hot/only-dev-server'
56+
: 'webpack/hot/dev-server')
57+
// TODO custom overlay client
58+
// `@vue/cli-overlay/dist/client`
59+
]
60+
if (process.env.APPVEYOR) {
61+
devClients.push(`webpack/hot/poll?500`)
62+
}
63+
// inject dev/hot client
64+
addDevClientToEntry(webpackConfig, devClients)
65+
}
66+
67+
// create compiler
68+
const compiler = webpack(webpackConfig)
69+
70+
if (!process.env.VUE_CLI_TEST) {
71+
compiler.apply(new webpack.ProgressPlugin())
72+
}
73+
74+
// resolve server options
4475
const useHttps = args.https || projectDevServerOptions.https || defaults.https
4576
const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
4677
portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
47-
48-
const portPromise = portfinder.getPortPromise()
49-
return portPromise.then(port => new Promise((resolve, reject) => {
50-
const webpackConfig = api.resolveWebpackConfig()
51-
52-
const urls = prepareURLs(
53-
useHttps ? 'https' : 'http',
54-
host,
55-
port
56-
)
57-
58-
if (!isProduction) {
59-
const devClients = [
60-
// dev server client
61-
require.resolve(`webpack-dev-server/client`),
62-
// hmr client
63-
require.resolve(projectDevServerOptions.hotOnly
64-
? 'webpack/hot/only-dev-server'
65-
: 'webpack/hot/dev-server')
66-
// TODO custom overlay client
67-
// `@vue/cli-overlay/dist/client`
68-
]
69-
if (process.env.APPVEYOR) {
70-
devClients.push(`webpack/hot/poll?500`)
71-
}
72-
// inject dev/hot client
73-
addDevClientToEntry(webpackConfig, devClients)
78+
const port = await portfinder.getPortPromise()
79+
80+
const urls = prepareURLs(
81+
useHttps ? 'https' : 'http',
82+
host,
83+
port
84+
)
85+
86+
const proxySettings = prepareProxy(
87+
projectDevServerOptions.proxy,
88+
api.resolve('public')
89+
)
90+
91+
// create server
92+
const server = new WebpackDevServer(compiler, Object.assign({
93+
clientLogLevel: 'none',
94+
historyApiFallback: {
95+
disableDotRule: true
96+
},
97+
contentBase: api.resolve('public'),
98+
watchContentBase: !isProduction,
99+
hot: !isProduction,
100+
quiet: true,
101+
compress: isProduction,
102+
publicPath: '/',
103+
overlay: isProduction // TODO disable this
104+
? false
105+
: { warnings: false, errors: true }
106+
}, projectDevServerOptions, {
107+
https: useHttps,
108+
proxy: proxySettings,
109+
before (app) {
110+
// launch editor support.
111+
// this works with vue-devtools & @vue/cli-overlay
112+
app.use('/__open-in-editor', launchEditorMiddleware(() => console.log(
113+
`To specify an editor, sepcify the EDITOR env variable or ` +
114+
`add "editor" field to your Vue project config.\n`
115+
)))
116+
// allow other plugins to register middlewares, e.g. PWA
117+
api.service.devServerConfigFns.forEach(fn => fn(app))
118+
// apply in project middlewares
119+
projectDevServerOptions.before && projectDevServerOptions.before(app)
74120
}
121+
}))
75122

76-
const compiler = webpack(webpackConfig)
123+
;['SIGINT', 'SIGTERM'].forEach(signal => {
124+
process.on(signal, () => {
125+
server.close(() => {
126+
process.exit(0)
127+
})
128+
})
129+
})
77130

78-
if (!process.env.VUE_CLI_TEST) {
79-
compiler.apply(new webpack.ProgressPlugin())
80-
}
131+
// on appveyor, killing the process with SIGTERM causes execa to
132+
// throw error
133+
if (process.env.VUE_CLI_TEST) {
134+
process.stdin.on('data', data => {
135+
if (data.toString() === 'close') {
136+
console.log('got close signal!')
137+
server.close(() => {
138+
process.exit(0)
139+
})
140+
}
141+
})
142+
}
81143

144+
return new Promise((resolve, reject) => {
82145
// log instructions & open browser on first compilation complete
83146
let isFirstCompile = true
84147
compiler.plugin('done', stats => {
@@ -123,69 +186,12 @@ module.exports = (api, options) => {
123186
}
124187
})
125188

126-
const proxySettings = prepareProxy(
127-
projectDevServerOptions.proxy,
128-
api.resolve('public')
129-
)
130-
131-
const server = new WebpackDevServer(compiler, Object.assign({
132-
clientLogLevel: 'none',
133-
historyApiFallback: {
134-
disableDotRule: true
135-
},
136-
contentBase: api.resolve('public'),
137-
watchContentBase: !isProduction,
138-
hot: !isProduction,
139-
quiet: true,
140-
compress: isProduction,
141-
publicPath: '/',
142-
overlay: isProduction // TODO disable this
143-
? false
144-
: { warnings: false, errors: true }
145-
}, projectDevServerOptions, {
146-
https: useHttps,
147-
proxy: proxySettings,
148-
before (app) {
149-
// launch editor support.
150-
// this works with vue-devtools & @vue/cli-overlay
151-
app.use('/__open-in-editor', launchEditorMiddleware(() => console.log(
152-
`To specify an editor, sepcify the EDITOR env variable or ` +
153-
`add "editor" field to your Vue project config.\n`
154-
)))
155-
// allow other plugins to register middlewares, e.g. PWA
156-
api.service.devServerConfigFns.forEach(fn => fn(app))
157-
// apply in project middlewares
158-
projectDevServerOptions.before && projectDevServerOptions.before(app)
159-
}
160-
}))
161-
162-
;['SIGINT', 'SIGTERM'].forEach(signal => {
163-
process.on(signal, () => {
164-
server.close(() => {
165-
process.exit(0)
166-
})
167-
})
168-
})
169-
170-
// on appveyor, killing the process with SIGTERM causes execa to
171-
// throw error
172-
if (process.env.VUE_CLI_TEST) {
173-
process.stdin.on('data', data => {
174-
if (data.toString() === 'close') {
175-
console.log('got close signal!')
176-
server.close(() => {
177-
process.exit(0)
178-
})
179-
}
180-
})
181-
}
182-
183189
server.listen(port, host, err => {
184190
if (err) {
185-
return error(err)
191+
reject(err)
186192
}
187193
})
188-
}))
194+
})
189195
})
190196
}
191197

0 commit comments

Comments
 (0)