UNPKG

@angular/cli

Version:
143 lines 5.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chalk = require("chalk"); const path = require("path"); const ast_tools_1 = require("../../lib/ast-tools"); const config_1 = require("../../models/config"); const dynamic_path_parser_1 = require("../../utilities/dynamic-path-parser"); const app_utils_1 = require("../../utilities/app-utils"); const resolve_module_file_1 = require("../../utilities/resolve-module-file"); const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); const findParentModule = require('../../utilities/find-parent-module').default; const Blueprint = require('../../ember-cli/lib/models/blueprint'); const getFiles = Blueprint.prototype.files; exports.default = Blueprint.extend({ name: 'pipe', description: '', aliases: ['p'], availableOptions: [ { name: 'flat', type: Boolean, description: 'Flag to indicate if a dir is created.' }, { name: 'spec', type: Boolean, description: 'Specifies if a spec file is generated.' }, { name: 'skip-import', type: Boolean, default: false, description: 'Allows for skipping the module import.' }, { name: 'module', type: String, aliases: ['m'], description: 'Allows specification of the declaring module.' }, { name: 'export', type: Boolean, default: false, description: 'Specifies if declaring module exports the pipe.' }, { name: 'app', type: String, aliases: ['a'], description: 'Specifies app name to use.' } ], beforeInstall: function (options) { const appConfig = app_utils_1.getAppFromConfig(this.options.app); if (options.module) { this.pathToModule = resolve_module_file_1.resolveModulePath(options.module, this.project, this.project.root, appConfig); } else { try { this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath); } catch (e) { if (!options.skipImport) { throw `Error locating module for declaration\n\t${e}`; } } } }, normalizeEntityName: function (entityName) { const appConfig = app_utils_1.getAppFromConfig(this.options.app); const dynamicPathOptions = { project: this.project, entityName, appConfig, dryRun: this.options.dryRun }; const parsedPath = dynamic_path_parser_1.dynamicPathParser(dynamicPathOptions); this.dynamicPath = parsedPath; return parsedPath.name; }, locals: function (options) { options.flat = options.flat !== undefined ? options.flat : config_1.CliConfig.getValue('defaults.pipe.flat'); options.spec = options.spec !== undefined ? options.spec : config_1.CliConfig.getValue('defaults.pipe.spec'); return { dynamicPath: this.dynamicPath.dir, flat: options.flat }; }, files: function () { let fileList = getFiles.call(this); if (this.options && !this.options.spec) { fileList = fileList.filter(p => p.indexOf('__name__.pipe.spec.ts') < 0); } return fileList; }, fileMapTokens: function (options) { // Return custom template variables here. return { __path__: () => { let dir = this.dynamicPath.dir; if (!options.locals.flat) { dir += path.sep + options.dasherizedModuleName; } this.generatePath = dir; return dir; } }; }, afterInstall: function (options) { const returns = []; const className = stringUtils.classify(`${options.entity.name}Pipe`); const fileName = stringUtils.dasherize(`${options.entity.name}.pipe`); const fullGeneratePath = path.join(this.project.root, this.generatePath); const moduleDir = path.parse(this.pathToModule).dir; const relativeDir = path.relative(moduleDir, fullGeneratePath); const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`; const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`; if (!options.skipImport) { if (options.dryRun) { this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); return; } returns.push(astUtils.addDeclarationToModule(this.pathToModule, className, importPath) .then((change) => change.apply(ast_tools_1.NodeHost)) .then((result) => { if (options.export) { return astUtils.addExportToModule(this.pathToModule, className, importPath) .then((change) => change.apply(ast_tools_1.NodeHost)); } return result; })); this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); this.addModifiedFile(this.pathToModule); } return Promise.all(returns); } }); //# sourceMappingURL=/users/hansl/sources/angular-cli/blueprints/pipe/index.js.map