diff --git a/cli.js b/cli.js index d046cbb..15bd6a2 100755 --- a/cli.js +++ b/cli.js @@ -24,7 +24,10 @@ if (!semver.satisfies(process.version, '>= 8.0.0')) { } const name = args._[0] || ''; +const { type } = args; +delete args.type; run({ name, + type, args, }); diff --git a/lib/generators/ant-design-pro/index.js b/lib/generators/ant-design-pro/index.js index 07075b6..b1a004d 100644 --- a/lib/generators/ant-design-pro/index.js +++ b/lib/generators/ant-design-pro/index.js @@ -33,17 +33,23 @@ const getGithubUrl = async () => { class AntDesignProGenerator extends BasicGenerator { prompting() { - const prompts = [ - { - name: 'language', - type: 'list', - message: 'Which language do you want to use?', - choices: ['TypeScript', 'JavaScript'], - }, - ]; - return this.prompt(prompts).then(props => { - this.prompts = props; - }); + if (this.opts.args.language) { + this.prompts = { + language: this.opts.args.language, + }; + } else { + const prompts = [ + { + name: 'language', + type: 'list', + message: 'Which language do you want to use?', + choices: ['TypeScript', 'JavaScript'], + }, + ]; + return this.prompt(prompts).then(props => { + this.prompts = props; + }); + } } async writing() { diff --git a/lib/generators/app/index.js b/lib/generators/app/index.js index fce6c75..e560a05 100644 --- a/lib/generators/app/index.js +++ b/lib/generators/app/index.js @@ -3,29 +3,36 @@ const BasicGenerator = require('../../BasicGenerator'); class Generator extends BasicGenerator { prompting() { - const prompts = [ - { - name: 'isTypeScript', - type: 'confirm', - message: 'Do you want to use typescript?', - default: false, - }, - { - name: 'reactFeatures', - message: 'What functionality do you want to enable?', - type: 'checkbox', - choices: [ - { name: 'antd', value: 'antd' }, - { name: 'dva', value: 'dva' }, - { name: 'code splitting', value: 'dynamicImport' }, - { name: 'dll', value: 'dll' }, - { name: 'internationalization', value: 'locale' }, - ], - }, - ]; - return this.prompt(prompts).then(props => { - this.prompts = props; - }); + if (this.opts.args && 'isTypeScript' in this.opts.args && 'reactFeatures' in this.opts.args) { + this.prompts = { + isTypeScript: this.opts.args.isTypeScript, + reactFeatures: this.opts.args.reactFeatures, + }; + } else { + const prompts = [ + { + name: 'isTypeScript', + type: 'confirm', + message: 'Do you want to use typescript?', + default: false, + }, + { + name: 'reactFeatures', + message: 'What functionality do you want to enable?', + type: 'checkbox', + choices: [ + {name: 'antd', value: 'antd'}, + {name: 'dva', value: 'dva'}, + {name: 'code splitting', value: 'dynamicImport'}, + {name: 'dll', value: 'dll'}, + {name: 'internationalization', value: 'locale'}, + ], + }, + ]; + return this.prompt(prompts).then(props => { + this.prompts = props; + }); + } } writing() { diff --git a/lib/run.js b/lib/run.js index 979d524..375bc8a 100644 --- a/lib/run.js +++ b/lib/run.js @@ -49,22 +49,26 @@ const runGenerator = async (generatorPath, { name = '', cwd = process.cwd(), arg const run = async config => { process.send && process.send({ type: 'prompt' }); process.emit('message', { type: 'prompt' }); - return inquirer - .prompt([ + + let { type } = config; + if (!type) { + const answers = await inquirer.prompt([ { name: 'type', message: 'Select the boilerplate type', type: 'list', choices: generators, }, - ]) - .then(answers => { - return runGenerator(`./generators/${answers.type}`, config); - }) - .catch(e => { - console.error(chalk.red(`> Generate failed`), e); - process.exit(1); - }); + ]); + type = answers.type; + } + + try { + return runGenerator(`./generators/${type}`, config); + } catch(e) { + console.error(chalk.red(`> Generate failed`), e); + process.exit(1); + } }; module.exports = run;