diff --git a/README.md b/README.md index 038efd5b..851c965b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,12 @@ Loaded templates: _templates added: app/hello.js ``` +Or you can have it prompt for generator and action: + +``` +$ hygen --prompt +``` + You've generated content into the current working directory in `app/`. To see how the generator is built, look at `_templates` (which you should check-in to your project from now on, by the way). You can build a generator that uses an interactive prompt to fill in a variable: diff --git a/src/params.js b/src/params.js index adb39e9d..c90d1618 100644 --- a/src/params.js +++ b/src/params.js @@ -6,14 +6,39 @@ const L = require('lodash') const path = require('path') const yargs = require('yargs-parser') const prompt = require('./prompt') +const { availableActions } = require('./help') const params = async ( - { templates, createPrompter }: RunnerConfig, + { templates, logger, createPrompter }: RunnerConfig, externalArgv: Array ): any => { const argv = yargs(externalArgv) + let [generator, action] = argv._ + const name = argv._[2] + + if (argv.prompt) { + logger.log('Asking for generator and action') + + const availableOptions = availableActions(templates) + + const prompter = createPrompter() + let response = await prompter.prompt({ + type: 'select', + name: 'generator', + message: 'Generator?', + choices: Object.keys(availableOptions) + }) + generator = response.generator // eslint-disable-line prefer-destructuring + + response = await prompter.prompt({ + type: 'select', + name: 'action', + message: 'Action?', + choices: availableOptions[generator] + }) + action = response.action // eslint-disable-line prefer-destructuring + } - const [generator, action, name] = argv._ if (!generator || !action) { return { generator, action, templates } }