Skip to content

Commit 44e2dc3

Browse files
committed
refactor(@angular/cli): update generate command module file resolution
closes #5461
1 parent f620993 commit 44e2dc3

File tree

11 files changed

+580
-118
lines changed

11 files changed

+580
-118
lines changed

packages/@angular/cli/blueprints/component/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import * as chalk from 'chalk';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
14
import { NodeHost } from '../../lib/ast-tools';
25
import { CliConfig } from '../../models/config';
36
import { getAppFromConfig } from '../../utilities/app-utils';
4-
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
7+
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
8+
import { resolveModulePath } from '../../utilities/resolve-module-file';
59

6-
import * as fs from 'fs';
7-
import * as path from 'path';
8-
import * as chalk from 'chalk';
910
const Blueprint = require('../../ember-cli/lib/models/blueprint');
1011
const findParentModule = require('../../utilities/find-parent-module').default;
1112
const getFiles = Blueprint.prototype.files;
@@ -107,14 +108,8 @@ export default Blueprint.extend({
107108
beforeInstall: function (options: any) {
108109
const appConfig = getAppFromConfig(this.options.app);
109110
if (options.module) {
110-
// Resolve path to module
111-
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
112-
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
113-
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);
114-
115-
if (!fs.existsSync(this.pathToModule)) {
116-
throw 'Module specified does not exist';
117-
}
111+
this.pathToModule =
112+
resolveModulePath(options.module, this.project, this.project.root, appConfig);
118113
} else {
119114
try {
120115
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
@@ -135,7 +130,7 @@ export default Blueprint.extend({
135130
const defaultPrefix = (appConfig && appConfig.prefix) || '';
136131

137132
let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
138-
? '' : (this.options.prefix || defaultPrefix);
133+
? '' : (this.options.prefix || defaultPrefix);
139134
prefix = prefix && `${prefix}-`;
140135

141136
this.selector = stringUtils.dasherize(prefix + parsedPath.name);

packages/@angular/cli/blueprints/directive/index.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {NodeHost} from '../../lib/ast-tools';
2-
import {CliConfig} from '../../models/config';
3-
import {getAppFromConfig} from '../../utilities/app-utils';
4-
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
5-
6-
const path = require('path');
7-
const fs = require('fs');
8-
const chalk = require('chalk');
1+
import * as chalk from 'chalk';
2+
import * as path from 'path';
3+
import { NodeHost } from '../../lib/ast-tools';
4+
import { CliConfig } from '../../models/config';
5+
import { getAppFromConfig } from '../../utilities/app-utils';
6+
import { resolveModulePath } from '../../utilities/resolve-module-file';
7+
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
8+
99
const stringUtils = require('ember-cli-string-utils');
1010
const astUtils = require('../../utilities/ast-utils');
1111
const findParentModule = require('../../utilities/find-parent-module').default;
@@ -58,17 +58,11 @@ export default Blueprint.extend({
5858
}
5959
],
6060

61-
beforeInstall: function(options: any) {
61+
beforeInstall: function (options: any) {
6262
const appConfig = getAppFromConfig(this.options.app);
6363
if (options.module) {
64-
// Resolve path to module
65-
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
66-
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
67-
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);
68-
69-
if (!fs.existsSync(this.pathToModule)) {
70-
throw ' ';
71-
}
64+
this.pathToModule =
65+
resolveModulePath(options.module, this.project, this.project.root, appConfig);
7266
} else {
7367
try {
7468
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
@@ -89,7 +83,7 @@ export default Blueprint.extend({
8983
const defaultPrefix = (appConfig && appConfig.prefix) || '';
9084

9185
let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
92-
? '' : (this.options.prefix || defaultPrefix);
86+
? '' : (this.options.prefix || defaultPrefix);
9387
prefix = prefix && `${prefix}-`;
9488

9589

@@ -111,7 +105,7 @@ export default Blueprint.extend({
111105
};
112106
},
113107

114-
files: function() {
108+
files: function () {
115109
let fileList = getFiles.call(this) as Array<string>;
116110

117111
if (this.options && !this.options.spec) {
@@ -135,7 +129,7 @@ export default Blueprint.extend({
135129
};
136130
},
137131

138-
afterInstall: function(options: any) {
132+
afterInstall: function (options: any) {
139133
const returns: Array<any> = [];
140134
const className = stringUtils.classify(`${options.entity.name}Directive`);
141135
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);
@@ -161,9 +155,9 @@ export default Blueprint.extend({
161155
}
162156
return result;
163157
}));
164-
this._writeStatusToUI(chalk.yellow,
165-
'update',
166-
path.relative(this.project.root, this.pathToModule));
158+
this._writeStatusToUI(chalk.yellow,
159+
'update',
160+
path.relative(this.project.root, this.pathToModule));
167161
}
168162

169163
return Promise.all(returns);

packages/@angular/cli/blueprints/guard/index.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import * as chalk from 'chalk';
2+
import * as path from 'path';
13
import { oneLine } from 'common-tags';
24
import { NodeHost } from '../../lib/ast-tools';
35
import { CliConfig } from '../../models/config';
6+
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
47
import { getAppFromConfig } from '../../utilities/app-utils';
5-
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
8+
import { resolveModulePath } from '../../utilities/resolve-module-file';
69

7-
const path = require('path');
8-
const fs = require('fs');
9-
const chalk = require('chalk');
1010
const Blueprint = require('../../ember-cli/lib/models/blueprint');
1111
const stringUtils = require('ember-cli-string-utils');
1212
const astUtils = require('../../utilities/ast-utils');
@@ -35,17 +35,11 @@ export default Blueprint.extend({
3535
}
3636
],
3737

38-
beforeInstall: function(options: any) {
39-
const appConfig = getAppFromConfig(this.options.app);
38+
beforeInstall: function (options: any) {
4039
if (options.module) {
41-
// Resolve path to module
42-
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
43-
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
44-
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);
45-
46-
if (!fs.existsSync(this.pathToModule)) {
47-
throw 'Module specified does not exist';
48-
}
40+
const appConfig = getAppFromConfig(this.options.app);
41+
this.pathToModule =
42+
resolveModulePath(options.module, this.project, this.project.root, appConfig);
4943
}
5044
},
5145

@@ -70,7 +64,7 @@ export default Blueprint.extend({
7064
};
7165
},
7266

73-
files: function() {
67+
files: function () {
7468
let fileList = getFiles.call(this) as Array<string>;
7569

7670
if (this.options && !this.options.spec) {
@@ -114,8 +108,8 @@ export default Blueprint.extend({
114108
astUtils.addProviderToModule(this.pathToModule, className, importPath)
115109
.then((change: any) => change.apply(NodeHost)));
116110
this._writeStatusToUI(chalk.yellow,
117-
'update',
118-
path.relative(this.project.root, this.pathToModule));
111+
'update',
112+
path.relative(this.project.root, this.pathToModule));
119113
}
120114

121115
return Promise.all(returns);

packages/@angular/cli/blueprints/pipe/index.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {NodeHost} from '../../lib/ast-tools';
2-
import {CliConfig} from '../../models/config';
3-
import {getAppFromConfig} from '../../utilities/app-utils';
4-
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
5-
6-
const path = require('path');
7-
const fs = require('fs');
8-
const chalk = require('chalk');
1+
import * as chalk from 'chalk';
2+
import * as path from 'path';
3+
import { NodeHost } from '../../lib/ast-tools';
4+
import { CliConfig } from '../../models/config';
5+
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
6+
import { getAppFromConfig } from '../../utilities/app-utils';
7+
import { resolveModulePath } from '../../utilities/resolve-module-file';
8+
99
const stringUtils = require('ember-cli-string-utils');
1010
const astUtils = require('../../utilities/ast-utils');
1111
const findParentModule = require('../../utilities/find-parent-module').default;
@@ -56,14 +56,8 @@ export default Blueprint.extend({
5656
beforeInstall: function(options: any) {
5757
const appConfig = getAppFromConfig(this.options.app);
5858
if (options.module) {
59-
// Resolve path to module
60-
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
61-
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
62-
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);
63-
64-
if (!fs.existsSync(this.pathToModule)) {
65-
throw 'Module specified does not exist';
66-
}
59+
this.pathToModule =
60+
resolveModulePath(options.module, this.project, this.project.root, appConfig);
6761
} else {
6862
try {
6963
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);

packages/@angular/cli/blueprints/service/index.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {NodeHost} from '../../lib/ast-tools';
2-
import {CliConfig} from '../../models/config';
3-
import {getAppFromConfig} from '../../utilities/app-utils';
4-
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
1+
import * as chalk from 'chalk';
2+
import * as path from 'path';
53
import { oneLine } from 'common-tags';
4+
import { NodeHost } from '../../lib/ast-tools';
5+
import { CliConfig } from '../../models/config';
6+
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
7+
import { getAppFromConfig } from '../../utilities/app-utils';
8+
import { resolveModulePath } from '../../utilities/resolve-module-file';
69

7-
const path = require('path');
8-
const fs = require('fs');
9-
const chalk = require('chalk');
1010
const Blueprint = require('../../ember-cli/lib/models/blueprint');
1111
const stringUtils = require('ember-cli-string-utils');
1212
const astUtils = require('../../utilities/ast-utils');
@@ -40,17 +40,11 @@ export default Blueprint.extend({
4040
}
4141
],
4242

43-
beforeInstall: function(options: any) {
43+
beforeInstall: function (options: any) {
4444
if (options.module) {
45-
// Resolve path to module
46-
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
4745
const appConfig = getAppFromConfig(this.options.app);
48-
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
49-
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);
50-
51-
if (!fs.existsSync(this.pathToModule)) {
52-
throw 'Module specified does not exist';
53-
}
46+
this.pathToModule =
47+
resolveModulePath(options.module, this.project, this.project.root, appConfig);
5448
}
5549
},
5650

@@ -75,7 +69,7 @@ export default Blueprint.extend({
7569
};
7670
},
7771

78-
files: function() {
72+
files: function () {
7973
let fileList = getFiles.call(this) as Array<string>;
8074

8175
if (this.options && !this.options.spec) {
@@ -119,8 +113,8 @@ export default Blueprint.extend({
119113
astUtils.addProviderToModule(this.pathToModule, className, importPath)
120114
.then((change: any) => change.apply(NodeHost)));
121115
this._writeStatusToUI(chalk.yellow,
122-
'update',
123-
path.relative(this.project.root, this.pathToModule));
116+
'update',
117+
path.relative(this.project.root, this.pathToModule));
124118
}
125119

126120
return Promise.all(returns);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import { dynamicPathParser } from './dynamic-path-parser';
4+
5+
export function resolveModulePath(
6+
moduleNameFromFlag: string, project: any, projectRoot: any, appConfig: any): string {
7+
let baseModuleName = moduleNameFromFlag;
8+
let parentFolders = '';
9+
10+
if (baseModuleName.includes(path.sep)) {
11+
const splitPath = baseModuleName.split(path.sep);
12+
baseModuleName = splitPath.pop();
13+
parentFolders = splitPath.join(path.sep);
14+
}
15+
16+
if (baseModuleName.includes('.')) {
17+
baseModuleName = baseModuleName
18+
.split('.')
19+
.filter(part => part !== 'module' && part !== 'ts')
20+
.join('.');
21+
}
22+
23+
const baseModuleWithFileSuffix = `${baseModuleName}.module.ts`;
24+
25+
const moduleRelativePath = path.join(parentFolders, baseModuleWithFileSuffix);
26+
let fullModulePath = buildFullPath(project, moduleRelativePath, appConfig, projectRoot);
27+
28+
if (!fs.existsSync(fullModulePath)) {
29+
const moduleWithFolderPrefix =
30+
path.join(parentFolders, baseModuleName, baseModuleWithFileSuffix);
31+
fullModulePath = buildFullPath(project, moduleWithFolderPrefix, appConfig, projectRoot);
32+
}
33+
34+
if (!fs.existsSync(fullModulePath)) {
35+
throw 'Specified module does not exist';
36+
}
37+
38+
return fullModulePath;
39+
}
40+
41+
function buildFullPath(project: any, relativeModulePath: string, appConfig: any, projectRoot: any) {
42+
const parsedPath = dynamicPathParser(project, relativeModulePath, appConfig);
43+
const fullModulePath = path.join(projectRoot, parsedPath.dir, parsedPath.base);
44+
45+
return fullModulePath;
46+
}

0 commit comments

Comments
 (0)