angular-cli/packages/@angular/cli/tasks/schematic-get-options.ts
Hans ae47b46563 refactor: move all CLI-specific exceptions to different options (#7981)
We made too many shortcuts for passing data in and custom schematics could not
work properly.

This is temporary as we will likely move some more logic into schematics tooling
to be able to pass only the raw args and the CLI config, but for now this is
enough to unblock AngularMix.
2017-10-09 22:28:29 -07:00

62 lines
1.7 KiB
TypeScript

const Task = require('../ember-cli/lib/models/task');
const stringUtils = require('ember-cli-string-utils');
import { CliConfig } from '../models/config';
import { getCollection, getSchematic } from '../utilities/schematics';
export interface SchematicGetOptions {
collectionName: string;
schematicName: string;
}
export interface SchematicAvailableOptions {
name: string;
description: string;
aliases: string[];
type: any;
}
export default Task.extend({
run: function (options: SchematicGetOptions): Promise<SchematicAvailableOptions[]> {
const collectionName = options.collectionName ||
CliConfig.getValue('defaults.schematics.collection');
const collection = getCollection(collectionName);
const schematic = getSchematic(collection, options.schematicName);
const properties = schematic.description.schemaJson.properties;
const keys = Object.keys(properties);
const availableOptions = keys
.map(key => ({...properties[key], ...{name: stringUtils.dasherize(key)}}))
.map(opt => {
let type;
switch (opt.type) {
case 'string':
type = String;
break;
case 'boolean':
type = Boolean;
break;
case undefined:
return null;
}
let aliases: string[] = [];
if (opt.alias) {
aliases = [...aliases, opt.alias];
}
if (opt.aliases) {
aliases = [...aliases, ...opt.aliases];
}
return {
...opt,
aliases,
type,
default: undefined // do not carry over schematics defaults
};
}).filter(x => x);
return Promise.resolve(availableOptions);
}
});