|
1 | 1 | const path = require('path');
|
| 2 | +const fs = require('fs'); |
2 | 3 | const chalk = require('chalk');
|
3 | 4 | const dynamicPathParser = require('../../utilities/dynamic-path-parser');
|
4 | 5 | const Blueprint = require('../../ember-cli/lib/models/blueprint');
|
| 6 | +const NodeHost = require('@angular-cli/ast-tools').NodeHost; |
| 7 | +const stringUtils = require('ember-cli-string-utils'); |
| 8 | +const astUtils = require('../../utilities/ast-utils'); |
5 | 9 | const getFiles = Blueprint.prototype.files;
|
6 | 10 |
|
7 | 11 | module.exports = {
|
8 | 12 | description: '',
|
9 | 13 |
|
10 | 14 | availableOptions: [
|
11 | 15 | { name: 'flat', type: Boolean, default: true },
|
12 |
| - { name: 'spec', type: Boolean } |
| 16 | + { name: 'spec', type: Boolean }, |
| 17 | + { name: 'module', type: String, aliases: ['m'] } |
13 | 18 | ],
|
14 | 19 |
|
| 20 | + beforeInstall: function(options) { |
| 21 | + if (options.module) { |
| 22 | + // Resolve path to module |
| 23 | + const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`; |
| 24 | + const parsedPath = dynamicPathParser(this.project, modulePath); |
| 25 | + this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base); |
| 26 | + |
| 27 | + if (!fs.existsSync(this.pathToModule)) { |
| 28 | + throw 'Module specified does not exist'; |
| 29 | + } |
| 30 | + } |
| 31 | + }, |
| 32 | + |
15 | 33 | normalizeEntityName: function (entityName) {
|
16 | 34 | var parsedPath = dynamicPathParser(this.project, entityName);
|
17 | 35 |
|
@@ -54,8 +72,25 @@ module.exports = {
|
54 | 72 | };
|
55 | 73 | },
|
56 | 74 |
|
57 |
| - afterInstall() { |
58 |
| - const warningMessage = 'Service is generated but not provided, it must be provided to be used'; |
59 |
| - this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage); |
| 75 | + afterInstall(options) { |
| 76 | + const returns = []; |
| 77 | + |
| 78 | + if (!this.pathToModule) { |
| 79 | + const warningMessage = 'Service is generated but not provided, it must be provided to be used'; |
| 80 | + this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage); |
| 81 | + } else { |
| 82 | + const className = stringUtils.classify(`${options.entity.name}Service`); |
| 83 | + const fileName = stringUtils.dasherize(`${options.entity.name}.service`); |
| 84 | + const fullGeneratePath = path.join(this.project.root, this.generatePath); |
| 85 | + const moduleDir = path.parse(this.pathToModule).dir; |
| 86 | + const relativeDir = path.relative(moduleDir, fullGeneratePath); |
| 87 | + const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`; |
| 88 | + returns.push( |
| 89 | + astUtils.addProviderToModule(this.pathToModule, className, importPath) |
| 90 | + .then(change => change.apply(NodeHost))); |
| 91 | + this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); |
| 92 | + } |
| 93 | + |
| 94 | + return Promise.all(returns); |
60 | 95 | }
|
61 | 96 | };
|
0 commit comments