Skip to content

Commit 8c3ff11

Browse files
author
Guillaume Chau
committed
refactor(project create): run vue create in child process, closes #3664
1 parent c81e6c2 commit 8c3ff11

File tree

5 files changed

+77
-42
lines changed

5 files changed

+77
-42
lines changed

packages/@vue/cli-ui/apollo-server/connectors/logs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/** @typedef {'warn' | 'error' | 'info' | 'done'} LogType */
2+
3+
/**
4+
* @typedef Log
5+
* @prop {string} id
6+
* @prop {string} date
7+
* @prop {LogType} type
8+
* @prop {string} tag
9+
* @prop {string} message
10+
*/
11+
112
const shortId = require('shortid')
213
const { events } = require('@vue/cli-shared-utils/lib/logger')
314
const { generateTitle } = require('@vue/cli/lib/util/clearConsole')
@@ -6,9 +17,15 @@ const channels = require('../channels')
617
// Context
718
const getContext = require('../context')
819

20+
/** @type {Log []} */
921
let logs = []
1022

23+
/**
24+
* @param {Log} log
25+
* @param {any} context
26+
*/
1127
exports.add = function (log, context) {
28+
/** @type {Log} */
1229
const item = {
1330
id: shortId.generate(),
1431
date: new Date().toISOString(),

packages/@vue/cli-ui/apollo-server/connectors/projects.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Creator = require('@vue/cli/lib/Creator')
55
const { getPromptModules } = require('@vue/cli/lib/util/createTools')
66
const { getFeatures } = require('@vue/cli/lib/util/features')
77
const { defaults } = require('@vue/cli/lib/options')
8-
const { toShortPluginId, clearModule } = require('@vue/cli-shared-utils')
8+
const { toShortPluginId, execa } = require('@vue/cli-shared-utils')
99
const { progress: installProgress } = require('@vue/cli/lib/util/installDeps')
1010
const parseGitConfig = require('parse-git-config')
1111
// Connectors
@@ -15,6 +15,7 @@ const prompts = require('./prompts')
1515
const folders = require('./folders')
1616
const plugins = require('./plugins')
1717
const locales = require('./locales')
18+
const logs = require('./logs')
1819
// Context
1920
const getContext = require('../context')
2021
// Utils
@@ -258,53 +259,23 @@ async function create (input, context) {
258259

259260
const targetDir = path.join(cwd.get(), input.folder)
260261

261-
// Delete existing folder
262-
if (fs.existsSync(targetDir)) {
263-
if (input.force) {
264-
setProgress({
265-
info: 'Cleaning folder...'
266-
})
267-
await folders.delete(targetDir)
268-
setProgress({
269-
info: null
270-
})
271-
} else {
272-
throw new Error(`Folder ${targetDir} already exists`)
273-
}
274-
}
275-
276262
cwd.set(targetDir, context)
277263
creator.context = targetDir
278264

279-
process.env.VUE_CLI_CONTEXT = targetDir
280-
clearModule('@vue/cli-service/webpack.config.js', targetDir)
281-
282265
const inCurrent = input.folder === '.'
283-
const name = inCurrent ? path.relative('../', process.cwd()) : input.folder
284-
creator.name = name.toLowerCase()
266+
const name = creator.name = (inCurrent ? path.relative('../', process.cwd()) : input.folder).toLowerCase()
285267

286268
// Answers
287269
const answers = prompts.getAnswers()
288270
await prompts.reset()
289-
let index
290271

291272
// Config files
273+
let index
292274
if ((index = answers.features.indexOf('use-config-files')) !== -1) {
293275
answers.features.splice(index, 1)
294276
answers.useConfigFiles = 'files'
295277
}
296278

297-
const createOptions = {
298-
packageManager: input.packageManager,
299-
bare: input.bare
300-
}
301-
// Git
302-
if (input.enableGit && input.gitCommitMessage) {
303-
createOptions.git = input.gitCommitMessage
304-
} else {
305-
createOptions.git = input.enableGit
306-
}
307-
308279
// Preset
309280
answers.preset = input.preset
310281
if (input.save) {
@@ -330,7 +301,49 @@ async function create (input, context) {
330301
})
331302

332303
// Create
333-
await creator.create(createOptions, preset)
304+
const args = [
305+
'--skipGetStarted'
306+
]
307+
if (input.packageManager) args.push('--packageManager', input.packageManager)
308+
if (input.bar) args.push('--bare')
309+
if (input.force) args.push('--force')
310+
// Git
311+
if (input.enableGit && input.gitCommitMessage) {
312+
args.push('--git', input.gitCommitMessage)
313+
} else if (!input.enableGit) {
314+
args.push('--no-git')
315+
}
316+
// Preset
317+
args.push('--inlinePreset', JSON.stringify(preset))
318+
319+
log('create', name, args)
320+
321+
const child = execa('vue', [
322+
'create',
323+
name,
324+
...args
325+
], {
326+
cwd: cwd.get(),
327+
stdio: ['inherit', 'pipe', 'inherit']
328+
})
329+
330+
const onData = buffer => {
331+
const text = buffer.toString().trim()
332+
if (text) {
333+
setProgress({
334+
info: text
335+
})
336+
logs.add({
337+
type: 'info',
338+
message: text
339+
}, context)
340+
}
341+
}
342+
343+
child.stdout.on('data', onData)
344+
345+
await child
346+
334347
removeCreator()
335348

336349
notify({

packages/@vue/cli-ui/src/components/app/ProgressScreen.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
</div>
3333

3434
<div class="secondary-info">
35-
<div v-if="progress.info" class="info">
36-
{{ progress.info }}
37-
</div>
35+
<div
36+
v-if="progress.info"
37+
class="info"
38+
v-html="ansiColors(progress.info)"
39+
/>
3840

3941
<VueLoadingBar
4042
v-if="progress.progress !== -1"

packages/@vue/cli/bin/vue.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ program
6363
.option('-c, --clone', 'Use git clone when fetching remote preset')
6464
.option('-x, --proxy', 'Use specified proxy when creating project')
6565
.option('-b, --bare', 'Scaffold project without beginner instructions')
66+
.option('--skipGetStarted', 'Skip displaying "Get started" instructions')
6667
.action((name, cmd) => {
6768
const options = cleanArgs(cmd)
6869

packages/@vue/cli/lib/Creator.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,13 @@ module.exports = class Creator extends EventEmitter {
210210
stopSpinner()
211211
log()
212212
log(`🎉 Successfully created project ${chalk.yellow(name)}.`)
213-
log(
214-
`👉 Get started with the following commands:\n\n` +
215-
(this.context === process.cwd() ? `` : chalk.cyan(` ${chalk.gray('$')} cd ${name}\n`)) +
216-
chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : 'npm run serve'}`)
217-
)
213+
if (!cliOptions.skipGetStarted) {
214+
log(
215+
`👉 Get started with the following commands:\n\n` +
216+
(this.context === process.cwd() ? `` : chalk.cyan(` ${chalk.gray('$')} cd ${name}\n`)) +
217+
chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : 'npm run serve'}`)
218+
)
219+
}
218220
log()
219221
this.emit('creation', { event: 'done' })
220222

0 commit comments

Comments
 (0)