|
| 1 | +import { cyan, yellow } from 'chalk'; |
| 2 | +const stringUtils = require('ember-cli-string-utils'); |
| 3 | +import { oneLine } from 'common-tags'; |
| 4 | +import { CliConfig } from '../models/config'; |
| 5 | + |
| 6 | +import 'rxjs/add/observable/of'; |
| 7 | +import 'rxjs/add/operator/ignoreElements'; |
| 8 | +import { getCollection, getEngineHost, getSchematic, runSchematic } from '../utilities/schematics'; |
| 9 | +import { DynamicPathOptions, dynamicPathParser } from '../utilities/dynamic-path-parser'; |
| 10 | +import { getAppFromConfig } from '../utilities/app-utils'; |
| 11 | +import * as path from 'path'; |
| 12 | + |
| 13 | +const Command = require('../ember-cli/lib/models/command'); |
| 14 | +const SilentError = require('silent-error'); |
| 15 | + |
| 16 | +function mapSchematicOptions(schematic: any): any[] { |
| 17 | + const properties = schematic.description.schemaJson.properties; |
| 18 | + const keys = Object.keys(properties); |
| 19 | + const options = keys |
| 20 | + .map(key => ({...properties[key], ...{name: stringUtils.dasherize(key)}})) |
| 21 | + .map(opt => { |
| 22 | + let type; |
| 23 | + switch(opt.type) { |
| 24 | + case 'string': |
| 25 | + type = String; |
| 26 | + break; |
| 27 | + case 'boolean': |
| 28 | + type = Boolean; |
| 29 | + break; |
| 30 | + } |
| 31 | + let aliases: string[] = []; |
| 32 | + if (opt.alias) { |
| 33 | + aliases = [...aliases, opt.alias]; |
| 34 | + } |
| 35 | + if (opt.aliases) { |
| 36 | + aliases = [...aliases, ...opt.aliases]; |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + ...opt, |
| 41 | + aliases, |
| 42 | + type |
| 43 | + }; |
| 44 | + }); |
| 45 | + |
| 46 | + return options; |
| 47 | +} |
| 48 | + |
| 49 | +export default Command.extend({ |
| 50 | + name: 'brocco', |
| 51 | + description: 'Generates and/or modifies files based on a schematic.', |
| 52 | + aliases: ['b'], |
| 53 | + |
| 54 | + availableOptions: [ |
| 55 | + { |
| 56 | + name: 'dry-run', |
| 57 | + type: Boolean, |
| 58 | + default: false, |
| 59 | + aliases: ['d'], |
| 60 | + description: 'Run through without making any changes.' |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'force', |
| 64 | + type: Boolean, |
| 65 | + default: false, |
| 66 | + aliases: ['f'], |
| 67 | + description: 'Forces overwriting of files.' |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'app', |
| 71 | + type: String, |
| 72 | + aliases: ['a'], |
| 73 | + description: 'Specifies app name to use.' |
| 74 | + }, |
| 75 | + { |
| 76 | + name: 'collection', |
| 77 | + type: String, |
| 78 | + aliases: ['c'], |
| 79 | + description: 'Schematics collection to use.' |
| 80 | + } |
| 81 | + ], |
| 82 | + |
| 83 | + anonymousOptions: [ |
| 84 | + '<schemtatic>' |
| 85 | + ], |
| 86 | + |
| 87 | + getCollectionName(rawArgs: string[]) { |
| 88 | + let collectionName = CliConfig.getValue('defaults.schematics.collection'); |
| 89 | + if (rawArgs) { |
| 90 | + const parsedArgs = this.parseArgs(rawArgs, false); |
| 91 | + if (parsedArgs.options.collection) { |
| 92 | + collectionName = parsedArgs.options.collection; |
| 93 | + } |
| 94 | + } |
| 95 | + return collectionName; |
| 96 | + }, |
| 97 | + |
| 98 | + beforeRun: function(rawArgs: string[]) { |
| 99 | + const collection = getCollection(this.getCollectionName(rawArgs)); |
| 100 | + |
| 101 | + const isHelp = ['--help', '-h'].includes(rawArgs[0]); |
| 102 | + if (isHelp) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const schematicName = rawArgs[0]; |
| 107 | + if (!schematicName) { |
| 108 | + return Promise.reject(new SilentError(oneLine` |
| 109 | + The "ng brocco" command requires a |
| 110 | + schematic name to be specified. |
| 111 | + For more details, use "ng help". |
| 112 | + `)); |
| 113 | + } |
| 114 | + |
| 115 | + this.schematic = getSchematic(collection, schematicName); |
| 116 | + |
| 117 | + const schematicOptions = mapSchematicOptions(this.schematic); |
| 118 | + |
| 119 | + this.registerOptions({availableOptions: schematicOptions}); |
| 120 | + }, |
| 121 | + |
| 122 | + run: function (commandOptions: any, rawArgs: string[]) { |
| 123 | + const entityName = rawArgs[1]; |
| 124 | + commandOptions.name = stringUtils.dasherize(entityName.split(path.sep).pop()); |
| 125 | + |
| 126 | + const appConfig = getAppFromConfig(commandOptions.app); |
| 127 | + const dynamicPathOptions: DynamicPathOptions = { |
| 128 | + project: this.project, |
| 129 | + entityName: entityName, |
| 130 | + appConfig: appConfig, |
| 131 | + dryRun: commandOptions.dryRun |
| 132 | + }; |
| 133 | + const parsedPath = dynamicPathParser(dynamicPathOptions); |
| 134 | + commandOptions.path = parsedPath.dir.replace(parsedPath.appRoot + path.sep, ''); |
| 135 | + const cwd = process.cwd(); |
| 136 | + const collectionName = this.getCollectionName(rawArgs); |
| 137 | + const collection = getCollection(collectionName); |
| 138 | + const schematicName = rawArgs[0]; |
| 139 | + const schematic = getSchematic(collection, schematicName); |
| 140 | + return runSchematic(schematic, cwd, commandOptions, this.project.root); |
| 141 | + |
| 142 | + }, |
| 143 | + |
| 144 | + printDetailedHelp: function () { |
| 145 | + const engineHost = getEngineHost(); |
| 146 | + const collectionName = this.getCollectionName(); |
| 147 | + const collection = getCollection(collectionName); |
| 148 | + const schematicNames: string[] = engineHost.listSchematics(collection) |
| 149 | + this.ui.writeLine(cyan('Available schematics:')); |
| 150 | + schematicNames.forEach(schematicName => { |
| 151 | + this.ui.writeLine(yellow(` ${schematicName}`)); |
| 152 | + }) |
| 153 | + this.ui.writeLine(''); |
| 154 | + } |
| 155 | +}); |
0 commit comments