|
| 1 | +import { NodeHost } from '../../lib/ast-tools'; |
| 2 | + |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as chalk from 'chalk'; |
| 6 | +const Blueprint = require('../../ember-cli/lib/models/blueprint'); |
| 7 | +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); |
| 8 | +const findParentModule = require('../../utilities/find-parent-module').default; |
| 9 | +const getFiles = Blueprint.prototype.files; |
| 10 | +const stringUtils = require('ember-cli-string-utils'); |
| 11 | +const astUtils = require('../../utilities/ast-utils'); |
| 12 | + |
| 13 | +export default Blueprint.extend({ |
| 14 | + description: '', |
| 15 | + |
| 16 | + availableOptions: [ |
| 17 | + { name: 'flat', type: Boolean }, |
| 18 | + { name: 'inline-template', type: Boolean, aliases: ['it'] }, |
| 19 | + { name: 'inline-style', type: Boolean, aliases: ['is'] }, |
| 20 | + { name: 'prefix', type: String, default: null }, |
| 21 | + { name: 'spec', type: Boolean }, |
| 22 | + { name: 'view-encapsulation', type: String, aliases: ['ve'] }, |
| 23 | + { name: 'change-detection', type: String, aliases: ['cd'] }, |
| 24 | + { name: 'skip-import', type: Boolean, default: false }, |
| 25 | + { name: 'module', type: String, aliases: ['m'] }, |
| 26 | + { name: 'export', type: Boolean, default: false } |
| 27 | + ], |
| 28 | + |
| 29 | + beforeInstall: function (options: any) { |
| 30 | + if (options.module) { |
| 31 | + // Resolve path to module |
| 32 | + const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`; |
| 33 | + const parsedPath = dynamicPathParser(this.project, modulePath); |
| 34 | + this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base); |
| 35 | + |
| 36 | + if (!fs.existsSync(this.pathToModule)) { |
| 37 | + throw 'Module specified does not exist'; |
| 38 | + } |
| 39 | + } else { |
| 40 | + try { |
| 41 | + this.pathToModule = findParentModule(this.project, this.dynamicPath.dir); |
| 42 | + } catch (e) { |
| 43 | + if (!options.skipImport) { |
| 44 | + throw `Error locating module for declaration\n\t${e}`; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + |
| 50 | + normalizeEntityName: function (entityName: string) { |
| 51 | + const parsedPath = dynamicPathParser(this.project, entityName); |
| 52 | + |
| 53 | + this.dynamicPath = parsedPath; |
| 54 | + |
| 55 | + let defaultPrefix = ''; |
| 56 | + if (this.project.ngConfig && |
| 57 | + this.project.ngConfig.apps[0] && |
| 58 | + this.project.ngConfig.apps[0].prefix) { |
| 59 | + defaultPrefix = this.project.ngConfig.apps[0].prefix; |
| 60 | + } |
| 61 | + |
| 62 | + let prefix = (this.options.prefix === 'false' || this.options.prefix === '') |
| 63 | + ? '' : (this.options.prefix || defaultPrefix); |
| 64 | + prefix = prefix && `${prefix}-`; |
| 65 | + |
| 66 | + this.selector = stringUtils.dasherize(prefix + parsedPath.name); |
| 67 | + |
| 68 | + if (this.selector.indexOf('-') === -1) { |
| 69 | + this._writeStatusToUI(chalk.yellow, 'WARNING', 'selectors should contain a dash'); |
| 70 | + } |
| 71 | + |
| 72 | + return parsedPath.name; |
| 73 | + }, |
| 74 | + |
| 75 | + locals: function (options: any) { |
| 76 | + this.styleExt = 'css'; |
| 77 | + if (this.project.ngConfig && |
| 78 | + this.project.ngConfig.defaults && |
| 79 | + this.project.ngConfig.defaults.styleExt) { |
| 80 | + this.styleExt = this.project.ngConfig.defaults.styleExt; |
| 81 | + } |
| 82 | + |
| 83 | + options.inlineStyle = options.inlineStyle !== undefined ? |
| 84 | + options.inlineStyle : |
| 85 | + this.project.ngConfigObj.get('defaults.component.inlineStyle'); |
| 86 | + |
| 87 | + options.inlineTemplate = options.inlineTemplate !== undefined ? |
| 88 | + options.inlineTemplate : |
| 89 | + this.project.ngConfigObj.get('defaults.component.inlineTemplate'); |
| 90 | + |
| 91 | + options.flat = options.flat !== undefined ? |
| 92 | + options.flat : |
| 93 | + this.project.ngConfigObj.get('defaults.component.flat'); |
| 94 | + |
| 95 | + options.spec = options.spec !== undefined ? |
| 96 | + options.spec : |
| 97 | + this.project.ngConfigObj.get('defaults.component.spec'); |
| 98 | + |
| 99 | + options.viewEncapsulation = options.viewEncapsulation !== undefined ? |
| 100 | + options.viewEncapsulation : |
| 101 | + this.project.ngConfigObj.get('defaults.component.viewEncapsulation'); |
| 102 | + |
| 103 | + options.changeDetection = options.changeDetection !== undefined ? |
| 104 | + options.changeDetection : |
| 105 | + this.project.ngConfigObj.get('defaults.component.changeDetection'); |
| 106 | + |
| 107 | + return { |
| 108 | + dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, ''), |
| 109 | + flat: options.flat, |
| 110 | + spec: options.spec, |
| 111 | + inlineTemplate: options.inlineTemplate, |
| 112 | + inlineStyle: options.inlineStyle, |
| 113 | + route: options.route, |
| 114 | + isAppComponent: !!options.isAppComponent, |
| 115 | + selector: this.selector, |
| 116 | + styleExt: this.styleExt, |
| 117 | + viewEncapsulation: options.viewEncapsulation, |
| 118 | + changeDetection: options.changeDetection |
| 119 | + }; |
| 120 | + }, |
| 121 | + |
| 122 | + files: function () { |
| 123 | + let fileList = getFiles.call(this) as Array<string>; |
| 124 | + |
| 125 | + if (this.options && this.options.inlineTemplate) { |
| 126 | + fileList = fileList.filter(p => p.indexOf('.html') < 0); |
| 127 | + } |
| 128 | + if (this.options && this.options.inlineStyle) { |
| 129 | + fileList = fileList.filter(p => p.indexOf('.__styleext__') < 0); |
| 130 | + } |
| 131 | + if (this.options && !this.options.spec) { |
| 132 | + fileList = fileList.filter(p => p.indexOf('__name__.component.spec.ts') < 0); |
| 133 | + } |
| 134 | + |
| 135 | + return fileList; |
| 136 | + }, |
| 137 | + |
| 138 | + fileMapTokens: function (options: any) { |
| 139 | + // Return custom template variables here. |
| 140 | + return { |
| 141 | + __path__: () => { |
| 142 | + let dir = this.dynamicPath.dir; |
| 143 | + if (!options.locals.flat) { |
| 144 | + dir += path.sep + options.dasherizedModuleName; |
| 145 | + } |
| 146 | + const srcDir = this.project.ngConfig.apps[0].root; |
| 147 | + this.appDir = dir.substr(dir.indexOf(srcDir) + srcDir.length); |
| 148 | + this.generatePath = dir; |
| 149 | + return dir; |
| 150 | + }, |
| 151 | + __styleext__: () => { |
| 152 | + return this.styleExt; |
| 153 | + } |
| 154 | + }; |
| 155 | + }, |
| 156 | + |
| 157 | + afterInstall: function (options: any) { |
| 158 | + if (options.dryRun) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + const returns: Array<any> = []; |
| 163 | + const className = stringUtils.classify(`${options.entity.name}Component`); |
| 164 | + const fileName = stringUtils.dasherize(`${options.entity.name}.component`); |
| 165 | + const componentDir = path.relative(path.dirname(this.pathToModule), this.generatePath); |
| 166 | + const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`; |
| 167 | + |
| 168 | + if (!options.skipImport) { |
| 169 | + const preChange = fs.readFileSync(this.pathToModule, 'utf8'); |
| 170 | + |
| 171 | + returns.push( |
| 172 | + astUtils.addDeclarationToModule(this.pathToModule, className, importPath) |
| 173 | + .then((change: any) => change.apply(NodeHost)) |
| 174 | + .then((result: any) => { |
| 175 | + if (options.export) { |
| 176 | + return astUtils.addExportToModule(this.pathToModule, className, importPath) |
| 177 | + .then((change: any) => change.apply(NodeHost)); |
| 178 | + } |
| 179 | + return result; |
| 180 | + }) |
| 181 | + .then(() => { |
| 182 | + const postChange = fs.readFileSync(this.pathToModule, 'utf8'); |
| 183 | + let moduleStatus = 'update'; |
| 184 | + |
| 185 | + if (postChange === preChange) { |
| 186 | + moduleStatus = 'identical'; |
| 187 | + } |
| 188 | + |
| 189 | + this._writeStatusToUI(chalk.yellow, |
| 190 | + moduleStatus, |
| 191 | + path.relative(this.project.root, this.pathToModule)); |
| 192 | + })); |
| 193 | + } |
| 194 | + |
| 195 | + return Promise.all(returns); |
| 196 | + } |
| 197 | +}); |
0 commit comments