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: resolve args from cli instead of prompts #79

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
28 changes: 17 additions & 11 deletions lib/generators/ant-design-pro/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
53 changes: 30 additions & 23 deletions lib/generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 14 additions & 10 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;