|
| 1 | +const Blueprint = require('ember-cli/lib/models/blueprint'); |
| 2 | +const getFiles = Blueprint.prototype.files; |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); |
| 6 | +const util = require('../../utilities/route-utils'); |
| 7 | +const SilentError = require('silent-error'); |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + description: 'Generates a route and template', |
| 11 | + |
| 12 | + availableOptions: [ |
| 13 | + { name: 'default', type: Boolean, default: false }, |
| 14 | + { name: 'route', type: String }, |
| 15 | + { name: 'parent', type: String, default: '' }, |
| 16 | + { name: 'outlet', type: Boolean, default: false } |
| 17 | + ], |
| 18 | + |
| 19 | + beforeInstall: function(options){ |
| 20 | + if (process.env.PWD.indexOf('src/app') === -1) { |
| 21 | + throw new SilentError('New route must be within app'); |
| 22 | + } |
| 23 | + this._locals(options) |
| 24 | + .then(names => { |
| 25 | + var route = options.route || this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '') |
| 26 | + + `/+${names.dasherizedModuleName}`; |
| 27 | + // setup options needed for adding path to routes.ts |
| 28 | + this.pathOptions = { |
| 29 | + isDefault: options.default, |
| 30 | + route: route, |
| 31 | + parent: options.parent, |
| 32 | + outlet: options.outlet, |
| 33 | + component: `${names.classifiedModuleName}Component`, |
| 34 | + dasherizedName: names.dasherizedModuleName, |
| 35 | + mainFile: path.join(this.project.root, 'src/main.ts'), |
| 36 | + routesFile: path.join(this.project.root, 'src/routes.ts') |
| 37 | + }; |
| 38 | + |
| 39 | + var component = this.pathOptions.component; |
| 40 | + if (!util.confirmComponentExport(file, component)) { |
| 41 | + throw new SilentError(`Please add export for '${component}' to '${file}'`); |
| 42 | + } |
| 43 | + }); |
| 44 | + // confirm that there is an export of the component in componentFile |
| 45 | + try { |
| 46 | + var file = util.resolveComponentPath(this.project.root, process.env.PWD, this.newRoutePath); |
| 47 | + } catch (e) { |
| 48 | + throw new SilentError(e); |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + files: function() { |
| 53 | + var fileList = getFiles.call(this); |
| 54 | + if (this.project && fs.existsSync(path.join(this.project.root, 'src/routes.ts'))) { |
| 55 | + return []; |
| 56 | + } |
| 57 | + return fileList; |
| 58 | + }, |
| 59 | + |
| 60 | + fileMapTokens: function() { |
| 61 | + return { |
| 62 | + __path__: () => 'src' |
| 63 | + }; |
| 64 | + }, |
| 65 | + |
| 66 | + normalizeEntityName: function(entityName) { |
| 67 | + if (!entityName) { |
| 68 | + throw new SilentError('Please provide new route\'s name'); |
| 69 | + } |
| 70 | + this.dynamicPath = dynamicPathParser(this.project, entityName); |
| 71 | + this.newRoutePath = entityName; |
| 72 | + return entityName; |
| 73 | + }, |
| 74 | + |
| 75 | + afterInstall: function() { |
| 76 | + return util.applyChanges(util.configureMain(this.pathOptions.mainFile, 'routes', './routes')) |
| 77 | + .then(() => { |
| 78 | + return util.applyChanges(util.addPathToRoutes(this.pathOptions.routesFile, this.pathOptions)); |
| 79 | + }).catch(e => { |
| 80 | + throw new SilentError(e.message); |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments