Skip to content

Commit

Permalink
feat(@angular-devkit/schematics-cli): add prompt support using Inquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Nov 16, 2018
1 parent 39dac64 commit 34d7e9d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/angular_devkit/schematics_cli/BUILD
Expand Up @@ -31,6 +31,7 @@ ts_library(
"//packages/angular_devkit/schematics:tools",
"@rxjs",
"@rxjs//operators",
"@npm//@types/inquirer",
"@npm//@types/minimist",
"@npm//@types/node",
],
Expand Down
77 changes: 64 additions & 13 deletions packages/angular_devkit/schematics_cli/bin/schematics.ts
Expand Up @@ -12,7 +12,9 @@ import 'symbol-observable';
// tslint:disable-next-line:ordered-imports import-groups
import {
JsonObject,
logging,
normalize,
schema,
tags,
terminal,
virtualFs,
Expand All @@ -24,6 +26,7 @@ import {
UnsuccessfulWorkflowExecution,
} from '@angular-devkit/schematics';
import { NodeModulesEngineHost, NodeWorkflow } from '@angular-devkit/schematics/tools';
import * as inquirer from 'inquirer';
import * as minimist from 'minimist';


Expand Down Expand Up @@ -59,12 +62,68 @@ export interface MainOptions {
stderr?: ProcessOutput;
}


function _listSchematics(collectionName: string, logger: logging.Logger) {
try {
const engineHost = new NodeModulesEngineHost();
const engine = new SchematicEngine(engineHost);
const collection = engine.createCollection(collectionName);
logger.info(engine.listSchematicNames(collection).join('\n'));
} catch (error) {
logger.fatal(error.message);

return 1;
}

return 0;
}

function _createPromptProvider(): schema.PromptProvider {
return (definitions: Array<schema.PromptDefinition>) => {
const questions: inquirer.Questions = definitions.map(definition => {
const question: inquirer.Question = {
name: definition.id,
message: definition.message,
default: definition.default,
};

const validator = definition.validator;
if (validator) {
question.validate = input => validator(input);
}

switch (definition.type) {
case 'confirmation':
return { ...question, type: 'confirm' };
case 'list':
return {
...question,
type: 'list',
choices: definition.items && definition.items.map(item => {
if (typeof item == 'string') {
return item;
} else {
return {
name: item.label,
value: item.value,
};
}
}),
};
default:
return { ...question, type: definition.type };
}
});

return inquirer.prompt(questions);
};
}

export async function main({
args,
stdout = process.stdout,
stderr = process.stderr,
}: MainOptions): Promise<0 | 1> {

const argv = parseArgs(args);

/** Create the DevKit Logger used through the CLI. */
Expand All @@ -84,18 +143,7 @@ export async function main({

/** If the user wants to list schematics, we simply show all the schematic names. */
if (argv['list-schematics']) {
try {
const engineHost = new NodeModulesEngineHost();
const engine = new SchematicEngine(engineHost);
const collection = engine.createCollection(collectionName);
logger.info(engine.listSchematicNames(collection).join('\n'));
} catch (error) {
logger.fatal(error.message);

return 1;
}

return 0;
return _listSchematics(collectionName, logger);
}

if (!schematicName) {
Expand Down Expand Up @@ -208,6 +256,9 @@ export async function main({
});
delete parsedArgs._;

// Add prompts.
workflow.registry.usePromptProvider(_createPromptProvider());


/**
* Execute the workflow, which will report the dry run events, run the tasks, and complete
Expand Down
5 changes: 3 additions & 2 deletions packages/angular_devkit/schematics_cli/package.json
Expand Up @@ -17,8 +17,9 @@
"@angular-devkit/core": "0.0.0",
"@angular-devkit/schematics": "0.0.0",
"@schematics/schematics": "0.0.0",
"inquirer": "6.2.0",
"minimist": "1.2.0",
"symbol-observable": "1.2.0",
"rxjs": "6.3.3"
"rxjs": "6.3.3",
"symbol-observable": "1.2.0"
}
}

0 comments on commit 34d7e9d

Please sign in to comment.