提交 7b42984f 编写于 作者: U ULIVZ

feat($core): flatten return array of functional option

上级 c4e27a87
......@@ -19,21 +19,25 @@ class AsyncOption extends Option {
*/
async asyncApply (...args) {
const items = []
for (const { name, value } of this.items) {
const rawItems = this.items
this.items = []
this.appliedItems = this.items
for (const { name, value } of rawItems) {
try {
items.push({
this.add(
name,
value: isFunction(value)
isFunction(value)
? await value(...args)
: value
})
)
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
}
}
this.appliedItems = items
this.items = rawItems
}
/**
......@@ -43,15 +47,18 @@ class AsyncOption extends Option {
*/
async parallelApply (...args) {
const items = []
await Promise.all(this.items.map(async ({ name, value }) => {
const rawItems = this.items
this.items = []
this.appliedItems = this.items
await Promise.all(rawItems.map(async ({ name, value }) => {
try {
items.push({
this.add(
name,
value: isFunction(value)
isFunction(value)
? await value(...args)
: value
})
)
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
......@@ -59,7 +66,8 @@ class AsyncOption extends Option {
})).catch(error => {
throw error
})
return items
this.items = rawItems
}
/**
......
......@@ -84,21 +84,25 @@ class Option {
*/
syncApply (...args) {
const items = []
for (const { name, value } of this.items) {
const rawItems = this.items
this.items = []
this.appliedItems = this.items
for (const { name, value } of rawItems) {
try {
items.push({
this.add(
name,
value: isFunction(value)
isFunction(value)
? value(...args)
: value
})
)
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
}
}
this.appliedItems = items
this.items = rawItems
}
/**
......
......@@ -14,24 +14,16 @@ module.exports = class ClientDynamicModulesOption extends AsyncOption {
async apply (ctx) {
await super.asyncApply()
for (const item of this.appliedItems) {
let { value: modules } = item
const { name: pluginName } = item
if (!Array.isArray(modules)) {
modules = [modules]
}
await Promise.all(modules.map(async ({ name, content, dirname = 'dynamic' }) => {
await ctx.writeTemp(
`${dirname}/${name}`,
`
for (const { value, name: pluginName } of this.appliedItems) {
const { name, content, dirname = 'dynamic' } = value
await ctx.writeTemp(
`${dirname}/${name}`,
`
/**
* Generated by "${pluginName}"
*/
${content}\n\n
`.trim())
}))
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册