Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
fix(@angular-devkit/core): fix relative path in schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Sep 19, 2017
1 parent e08f89d commit e77e3c3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/angular_devkit/core/src/virtual-fs/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export function normalize(path: string): Path {
} else if (p[i] == '..') {
if (i < 2 && !relative) {
throw new InvalidPathException(original);
} else if (i >= 2) {
} else if (i >= 2 && p[i - 1] != '..') {
p.splice(i - 1, 2);
i--;
} else {
Expand Down
9 changes: 9 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/path_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ describe('path', () => {

// Out of directory.
expect(normalize('..')).toBe('..');
expect(normalize('../..')).toBe('../..');
expect(normalize('../../a')).toBe('../../a');
expect(normalize('b/../../a')).toBe('../a');
expect(normalize('./..')).toBe('..');
expect(normalize('../a/b/c')).toBe('../a/b/c');
expect(normalize('./a/../../a/b/c')).toBe('../a/b/c');
Expand Down Expand Up @@ -116,8 +119,14 @@ describe('path', () => {
const tests = [
['/a/b/c', '/a/b/c', ''],
['/a/b', '/a/b/c', 'c'],
['/a/b', '/a/b/c/d', 'c/d'],
['/a/b/c', '/a/b', '..'],
['/a/b/c', '/a/b/d', '../d'],
['/a/b/c/d/e', '/a/f/g', '../../../../f/g'],
[
'/src/app/sub1/test1', '/src/app/sub2/test2',
'../../sub2/test2',
],
];

for (const [from, to, result] of tests) {
Expand Down
20 changes: 12 additions & 8 deletions packages/schematics/angular/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { normalize } from '@angular-devkit/core';
import { basename, dirname, normalize, relative } from '@angular-devkit/core';
import {
Rule,
SchematicContext,
Expand All @@ -25,7 +25,7 @@ import * as ts from 'typescript';
import * as stringUtils from '../strings';
import { addImportToModule } from '../utility/ast-utils';
import { InsertChange } from '../utility/change';
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
import { findModuleFromOptions } from '../utility/find-module';
import { Schema as ModuleOptions } from './schema';


Expand All @@ -35,7 +35,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
return host;
}

const modulePath = options.module;
const modulePath = normalize('/' + options.module);

const text = host.read(modulePath);
if (text === null) {
Expand All @@ -44,11 +44,15 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
const sourceText = text.toString('utf-8');
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);

const importModulePath = `/${options.sourceDir}/${options.path}/`
+ (options.flat ? '' : stringUtils.dasherize(options.name) + '/')
+ stringUtils.dasherize(options.name)
+ '.module';
const relativePath = buildRelativePath(modulePath, importModulePath);
const importModulePath = normalize(
`/${options.sourceDir}/${options.path}/`
+ (options.flat ? '' : stringUtils.dasherize(options.name) + '/')
+ stringUtils.dasherize(options.name)
+ '.module',
);
const relativeDir = relative(dirname(modulePath), dirname(importModulePath));
const relativePath = (relativeDir.startsWith('.') ? relativeDir : './' + relativeDir)
+ '/' + basename(importModulePath);
const changes = addImportToModule(source, modulePath,
stringUtils.classify(`${options.name}Module`),
relativePath);
Expand Down
23 changes: 22 additions & 1 deletion packages/schematics/angular/module/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,28 @@ describe('Module Schematic', () => {
expect(content).toMatch(/imports: \[(.|\s)*FooModule(.|\s)*\]/m);
});

it('should createa routing module', () => {
it('should import into another module (deep)', () => {
let tree = appTree;

tree = schematicRunner.runSchematic('module', {
...defaultOptions,
path: 'app/sub1',
appRoot: 'app',
name: 'test1',
}, tree);
tree = schematicRunner.runSchematic('module', {
...defaultOptions,
path: 'app/sub2',
appRoot: 'app',
name: 'test2',
module: 'sub1/test1',
}, tree);

const content = getFileContent(tree, '/src/app/sub1/test1/test1.module.ts');
expect(content).toMatch(/import { Test2Module } from '..\/..\/sub2\/test2\/test2.module'/);
});

it('should create a routing module', () => {
const options = { ...defaultOptions, routing: true };

const tree = schematicRunner.runSchematic('module', options, appTree);
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Path, normalize, relative, dirname } from '@angular-devkit/core';
import { Path, dirname, normalize, relative } from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { dasherize } from '../strings';

Expand Down

0 comments on commit e77e3c3

Please sign in to comment.