Skip to content

Commit c0c03f9

Browse files
cexbrayatfilipesilva
authored andcommitted
fix(@angular/cli): simplify import path if possible (#6184)
Fixes #6183
1 parent a9baddf commit c0c03f9

File tree

10 files changed

+30
-25
lines changed

10 files changed

+30
-25
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ export default Blueprint.extend({
222222
const className = stringUtils.classify(`${options.entity.name}Component`);
223223
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);
224224
const componentDir = path.relative(path.dirname(this.pathToModule), this.generatePath);
225-
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
225+
const normalizeRelativeDir = componentDir.startsWith('.') ? componentDir : `./${componentDir}`;
226+
const importPath = componentDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
226227

227228
if (!options.skipImport) {
228229
if (options.dryRun) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ export default Blueprint.extend({
136136
const fullGeneratePath = path.join(this.project.root, this.generatePath);
137137
const moduleDir = path.parse(this.pathToModule).dir;
138138
const relativeDir = path.relative(moduleDir, fullGeneratePath);
139-
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
139+
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
140+
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
140141

141142
if (!options.skipImport) {
142143
if (options.dryRun) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export default Blueprint.extend({
103103
const fullGeneratePath = path.join(this.project.root, this.generatePath);
104104
const moduleDir = path.parse(this.pathToModule).dir;
105105
const relativeDir = path.relative(moduleDir, fullGeneratePath);
106-
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
106+
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
107+
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
107108
returns.push(
108109
astUtils.addProviderToModule(this.pathToModule, className, importPath)
109110
.then((change: any) => change.apply(NodeHost)));

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export default Blueprint.extend({
121121
const fullGeneratePath = path.join(this.project.root, this.generatePath);
122122
const moduleDir = path.parse(this.pathToModule).dir;
123123
const relativeDir = path.relative(moduleDir, fullGeneratePath);
124-
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
124+
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
125+
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
125126

126127
if (!options.skipImport) {
127128
if (options.dryRun) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export default Blueprint.extend({
115115
const fullGeneratePath = path.join(this.project.root, this.generatePath);
116116
const moduleDir = path.parse(this.pathToModule).dir;
117117
const relativeDir = path.relative(moduleDir, fullGeneratePath);
118-
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
118+
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
119+
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
119120
returns.push(
120121
astUtils.addProviderToModule(this.pathToModule, className, importPath)
121122
.then((change: any) => change.apply(NodeHost)));

tests/acceptance/generate-component.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe('Acceptance: ng generate component', function () {
267267
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
268268
.then(() => readFile(modulePath, 'utf-8'))
269269
.then(content => {
270-
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
270+
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
271271
expect(content).matches(/declarations:\s+\[BazComponent]/m);
272272
});
273273
});
@@ -281,7 +281,7 @@ describe('Acceptance: ng generate component', function () {
281281
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'foo')]))
282282
.then(() => readFile(modulePath, 'utf-8'))
283283
.then(content => {
284-
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
284+
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
285285
expect(content).matches(/declarations:\s+\[BazComponent]/m);
286286
});
287287
});
@@ -295,7 +295,7 @@ describe('Acceptance: ng generate component', function () {
295295
.then(() => ng(['generate', 'component', 'baz', '--module', 'foo']))
296296
.then(() => readFile(modulePath, 'utf-8'))
297297
.then(content => {
298-
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
298+
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
299299
expect(content).matches(/declarations:\s+\[BazComponent]/m);
300300
});
301301
});
@@ -310,7 +310,7 @@ describe('Acceptance: ng generate component', function () {
310310
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'bar')]))
311311
.then(() => readFile(modulePath, 'utf-8'))
312312
.then(content => {
313-
expect(content).matches(/import.*BazComponent.*from '.\/..\/..\/baz\/baz.component';/);
313+
expect(content).matches(/import.*BazComponent.*from '..\/..\/baz\/baz.component';/);
314314
expect(content).matches(/declarations:\s+\[BazComponent]/m);
315315
});
316316
});

tests/acceptance/generate-directive.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('Acceptance: ng generate directive', function () {
222222
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
223223
.then(() => readFile(modulePath, 'utf-8'))
224224
.then(content => {
225-
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
225+
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
226226
expect(content).matches(/declarations:\s+\[BazDirective]/m);
227227
});
228228
});
@@ -236,7 +236,7 @@ describe('Acceptance: ng generate directive', function () {
236236
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'foo')]))
237237
.then(() => readFile(modulePath, 'utf-8'))
238238
.then(content => {
239-
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
239+
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
240240
expect(content).matches(/declarations:\s+\[BazDirective]/m);
241241
});
242242
});
@@ -250,7 +250,7 @@ describe('Acceptance: ng generate directive', function () {
250250
.then(() => ng(['generate', 'directive', 'baz', '--module', 'foo']))
251251
.then(() => readFile(modulePath, 'utf-8'))
252252
.then(content => {
253-
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
253+
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
254254
expect(content).matches(/declarations:\s+\[BazDirective]/m);
255255
});
256256
});
@@ -265,7 +265,7 @@ describe('Acceptance: ng generate directive', function () {
265265
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'bar')]))
266266
.then(() => readFile(modulePath, 'utf-8'))
267267
.then(content => {
268-
expect(content).matches(/import.*BazDirective.*from '.\/..\/..\/baz.directive';/);
268+
expect(content).matches(/import.*BazDirective.*from '..\/..\/baz.directive';/);
269269
expect(content).matches(/declarations:\s+\[BazDirective]/m);
270270
});
271271
});

tests/acceptance/generate-guard.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ describe('Acceptance: ng generate guard', function () {
209209
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
210210
.then(() => readFile(modulePath, 'utf-8'))
211211
.then(content => {
212-
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
212+
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
213213
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
214214
});
215215
});
@@ -223,7 +223,7 @@ describe('Acceptance: ng generate guard', function () {
223223
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo')]))
224224
.then(() => readFile(modulePath, 'utf-8'))
225225
.then(content => {
226-
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
226+
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
227227
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
228228
});
229229
});
@@ -237,7 +237,7 @@ describe('Acceptance: ng generate guard', function () {
237237
.then(() => ng(['generate', 'guard', 'baz', '--module', 'foo']))
238238
.then(() => readFile(modulePath, 'utf-8'))
239239
.then(content => {
240-
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
240+
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
241241
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
242242
});
243243
});
@@ -252,7 +252,7 @@ describe('Acceptance: ng generate guard', function () {
252252
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'bar')]))
253253
.then(() => readFile(modulePath, 'utf-8'))
254254
.then(content => {
255-
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/..\/baz.guard';/);
255+
expect(content).to.matches(/import.*BazGuard.*from '..\/..\/baz.guard';/);
256256
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
257257
});
258258
});

tests/acceptance/generate-pipe.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('Acceptance: ng generate pipe', function () {
200200
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
201201
.then(() => readFile(modulePath, 'utf-8'))
202202
.then(content => {
203-
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
203+
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
204204
expect(content).matches(/declarations:\s+\[BazPipe]/m);
205205
});
206206
});
@@ -214,7 +214,7 @@ describe('Acceptance: ng generate pipe', function () {
214214
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'foo')]))
215215
.then(() => readFile(modulePath, 'utf-8'))
216216
.then(content => {
217-
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
217+
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
218218
expect(content).matches(/declarations:\s+\[BazPipe]/m);
219219
});
220220
});
@@ -228,7 +228,7 @@ describe('Acceptance: ng generate pipe', function () {
228228
.then(() => ng(['generate', 'pipe', 'baz', '--module', 'foo']))
229229
.then(() => readFile(modulePath, 'utf-8'))
230230
.then(content => {
231-
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
231+
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
232232
expect(content).matches(/declarations:\s+\[BazPipe]/m);
233233
});
234234
});
@@ -243,7 +243,7 @@ describe('Acceptance: ng generate pipe', function () {
243243
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'bar')]))
244244
.then(() => readFile(modulePath, 'utf-8'))
245245
.then(content => {
246-
expect(content).matches(/import.*BazPipe.*from '.\/..\/..\/baz.pipe';/);
246+
expect(content).matches(/import.*BazPipe.*from '..\/..\/baz.pipe';/);
247247
expect(content).matches(/declarations:\s+\[BazPipe]/m);
248248
});
249249
});

tests/acceptance/generate-service.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('Acceptance: ng generate service', function () {
210210
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
211211
.then(() => readFile(modulePath, 'utf-8'))
212212
.then(content => {
213-
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
213+
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
214214
expect(content).to.matches(/providers:\s*\[BazService\]/m);
215215
});
216216
});
@@ -224,7 +224,7 @@ describe('Acceptance: ng generate service', function () {
224224
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'foo')]))
225225
.then(() => readFile(modulePath, 'utf-8'))
226226
.then(content => {
227-
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
227+
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
228228
expect(content).to.matches(/providers:\s*\[BazService\]/m);
229229
});
230230
});
@@ -238,7 +238,7 @@ describe('Acceptance: ng generate service', function () {
238238
.then(() => ng(['generate', 'service', 'baz', '--module', 'foo']))
239239
.then(() => readFile(modulePath, 'utf-8'))
240240
.then(content => {
241-
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
241+
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
242242
expect(content).to.matches(/providers:\s*\[BazService\]/m);
243243
});
244244
});
@@ -253,7 +253,7 @@ describe('Acceptance: ng generate service', function () {
253253
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'bar')]))
254254
.then(() => readFile(modulePath, 'utf-8'))
255255
.then(content => {
256-
expect(content).to.matches(/import.*BazService.*from '.\/..\/..\/baz.service';/);
256+
expect(content).to.matches(/import.*BazService.*from '..\/..\/baz.service';/);
257257
expect(content).to.matches(/providers:\s*\[BazService\]/m);
258258
});
259259
});

0 commit comments

Comments
 (0)