10000 feat(cli): Generator support async by xierenyuan · Pull Request #3897 · vuejs/vue-cli · GitHub
[go: up one dir, main page]

Skip to content

feat(cli): Generator support async #3897

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'dev' into dev
  • Loading branch information
haoqunjiang authored Jul 29, 2019
commit 0703b469f1470eaf68b3fd6a550df96c0268951b
56 changes: 38 additions & 18 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = class Generator {
this.pm = new PackageManager({ context })
this.imports = {}
this.rootOptions = {}
this.afterInvokeCbs = []
this.afterInvokeCbs = afterInvokeCbs
this.afterAnyInvokeCbs = afterAnyInvokeCbs
this.configTransforms = {}
this.defaultConfigTransforms = defaultConfigTransforms
Expand All @@ -98,10 +98,8 @@ module.exports = class Generator {
// exit messages
this.exitLogs = []

const pluginIds = plugins.map(p => p.id)

// load all the other plugins
this.allPlugins = Object.keys(this.pkg.dependencies || {})
this.allPluginIds = Object.keys(this.pkg.dependencies || {})
.concat(Object.keys(this.pkg.devDependencies || {}))
.filter(isPlugin)

Expand All @@ -113,22 +111,44 @@ module.exports = class Generator {
this.rootOptions = rootOptions
}

initPlugins () {
async initPlugins () {
const { rootOptions, invoking } = this
return new Promise((resolve, reject) => {
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)
})
const pluginIds = this.plugins.map(p => p.id)

Promise.all(arrP).then(resolve).catch(reject)
})
// apply hooks from all plugins
for (const id of this.allPluginIds) {
const api = new GeneratorAPI(id, this, {}, rootOptions)
const pluginGenerator = loadModule(`${id}/generator`, this.context)

if (pluginGenerator && pluginGenerator.hooks) {
await pluginGenerator.hooks(api, {}, rootOptions, pluginIds)
}
}

// We are doing save/load to make the hook order deterministic
// save "any" hooks
const afterAnyInvokeCbsFromPlugins = this.afterAnyInvokeCbs

// load "any" hooks
this.afterAnyInvokeCbs = afterAnyInvokeCbsFromPlugins
// reset hooks
this.afterAnyInvokeCbs = []
this.postProcessFilesCbs = []

// apply generators from plugins
for (const plugin of this.plugins) {
const { id, apply, options } = plugin
const api = new GeneratorAPI(id, this, options, rootOptions)
await apply(api, options, rootOptions, invoking)

if (apply.hooks) {
// while we execute the entire `hooks` function,
// only the `afterInvoke` hook is respected
// because `afterAnyHooks` is already determined by the `allPluginIds` loop aboe
await apply.hooks(api, options, rootOptions, pluginIds)
}

// restore "any" hooks
this.afterAnyInvokeCbs = afterAnyInvokeCbsFromPlugins
}
}

async generate ({
Expand Down Expand Up @@ -274,7 +294,7 @@ module.exports = class Generator {
hasPlugin (_id, _version) {
return [
...this.plugins.map(p => p.id),
...this.allPlugins
...this.allPluginIds
].some(id => {
if (!matchesPluginId(_id, id)) {
return false
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0