Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Generator support async #3897

Merged
merged 2 commits into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/@vue/cli/__tests__/Generator.spec.js
Expand Up @@ -242,7 +242,7 @@ test('api: extendPackage merge dependencies', async () => {
})

test('api: warn invalid dep range', async () => {
new Generator('/', { plugins: [
const generator = new Generator('/', { plugins: [
{
id: 'test1',
apply: api => {
Expand All @@ -255,6 +255,8 @@ test('api: warn invalid dep range', async () => {
}
] })

await generator.generate()

expect(logs.warn.some(([msg]) => {
return (
msg.match(/invalid version range for dependency "foo"/) &&
Expand All @@ -264,7 +266,7 @@ test('api: warn invalid dep range', async () => {
})

test('api: extendPackage dependencies conflict', async () => {
new Generator('/', { plugins: [
const generator = new Generator('/', { plugins: [
{
id: 'test1',
apply: api => {
Expand All @@ -287,6 +289,8 @@ test('api: extendPackage dependencies conflict', async () => {
}
] })

await generator.generate()

expect(logs.warn.some(([msg]) => {
return (
msg.match(/conflicting versions for project dependency "foo"/) &&
Expand All @@ -298,7 +302,7 @@ test('api: extendPackage dependencies conflict', async () => {
})

test('api: extendPackage merge warn nonstrictly semver deps', async () => {
new Generator('/', { plugins: [
const generator = new Generator('/', { plugins: [
{
id: 'test3',
apply: api => {
Expand All @@ -321,6 +325,8 @@ test('api: extendPackage merge warn nonstrictly semver deps', async () => {
}
] })

await generator.generate()

expect(logs.warn.some(([msg]) => {
return (
msg.match(/conflicting versions for project dependency "bar"/) &&
Expand Down Expand Up @@ -422,10 +428,10 @@ test('api: hasPlugin', () => {
] })
})

test('api: onCreateComplete', () => {
test('api: onCreateComplete', async () => {
const fn = () => {}
const cbs = []
new Generator('/', {
const generator = new Generator('/', {
plugins: [
{
id: 'test',
Expand All @@ -436,6 +442,9 @@ test('api: onCreateComplete', () => {
],
completeCbs: cbs
})

await generator.generate()

expect(cbs).toContain(fn)
})

Expand Down
@@ -0,0 +1,17 @@
const sleep = n => new Promise(resolve => setTimeout(resolve, n))

module.exports = async (api, options) => {
api.render('./template', options)

// add asynchronous code test
await sleep(1000)

api.extendPackage({
scripts: {
testasync: 'this is the test'
},
devDependencies: {
'vue-cli-plugin-async-generator': 'v0.0.1'
}
})
}
@@ -0,0 +1 @@
<%= ok %>
@@ -0,0 +1,5 @@
{
"plugins": {
"@vue/cli-plugin-babel": {}
}
}
@@ -0,0 +1,5 @@
module.exports = [{
type: 'confirm',
name: 'ok',
message: 'Are you ok?'
}]
29 changes: 29 additions & 0 deletions packages/@vue/cli/__tests__/preset.spec.js
Expand Up @@ -56,3 +56,32 @@ test('should recognize generator/index.js in a local preset directory', async ()
const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@vue/cli-plugin-babel')
})

test('should recognize generator/index.js in a local preset directory by async generatory', async () => {
const cwd = path.resolve(__dirname, '../../../test')
const name = 'test-preset-template-async-generator'

expectPrompts([{
message: 'Are you ok',
confirm: true
}])

await create(
name,
{
force: true,
git: false,
cwd,
preset: path.resolve(__dirname, './mock-preset-with-async-generator')
}
)

const testFile = await fs.readFile(path.resolve(cwd, name, 'test.js'), 'utf-8')
expect(testFile).toBe('true\n')

const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@vue/cli-plugin-babel')
expect(pkg.devDependencies).toHaveProperty('vue-cli-plugin-async-generator')
expect(pkg.scripts).toHaveProperty('testasync')
})

22 changes: 18 additions & 4 deletions packages/@vue/cli/lib/Generator.js
Expand Up @@ -97,17 +97,31 @@ module.exports = class Generator {
const rootOptions = cliService
? cliService.options
: inferRootOptions(pkg)
// apply generators from plugins
plugins.forEach(({ id, apply, options }) => {
const api = new GeneratorAPI(id, this, options, rootOptions)
apply(api, options, rootOptions, invoking)

this.rootOptions = rootOptions
}

initPlugins () {
const { rootOptions, invoking } = this
return new Promise((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use async with a for loop?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not do something like:

Suggested change
return new Promise((resolve, reject) => {
return Promise.all(this.plugins.map(({ id, apply, options }) => {
const api = new GeneratorAPI(id, this, options, rootOptions)
return apply(api, options, rootOptions, invoking)
}))

const arrP = []
// apply generators from plugins
this.plugins.forEach(({ id, apply, options }) => {
const api = new GeneratorAPI(id, this, options, rootOptions)
const fn = apply(api, options, rootOptions, invoking)
arrP.push(fn)
})

Promise.all(arrP).then(resolve).catch(reject)
})
}

async generate ({
extractConfigFiles = false,
checkExisting = false
} = {}) {
await this.initPlugins()

// save the file system before applying plugin for comparison
const initialFiles = Object.assign({}, this.files)
// extract configs from package.json into dedicated files.
Expand Down