Skip to content

Commit

Permalink
fix(schematics): make override schematic robust to path seperators (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored May 4, 2022
1 parent f0d7e51 commit 0fd617a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions schematics/src/helpers/override/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rule, SchematicsException, chain } from '@angular-devkit/schematics';
import { getWorkspace } from '@schematics/angular/utility/workspace';
import { normalize } from 'path';
import { OverrideOptionsSchema as Options } from 'schemas/helpers/override/schema';

import { copyFile } from '../../utils/filesystem';
Expand All @@ -19,9 +20,14 @@ export function override(options: Options): Rule {
const workspace = await getWorkspace(host);
const project = workspace.projects.get(options.project);
const sourceRoot = project.sourceRoot;
const from = `${
options.path ? `${options.path}/` : !options.from?.startsWith(`${sourceRoot}/app/`) ? `${sourceRoot}/app/` : ''
}${options.from.replace(/\/$/, '')}`;
const path = normalize(options.path ?? '')
.replace(/\\/g, '/')
.replace(/^\./, '');
let from = normalize(options.from).replace(/\\/g, '/');
from = `${path ? `${path}/` : !from.startsWith(`${sourceRoot}/app/`) ? `${sourceRoot}/app/` : ''}${from.replace(
/\/$/,
''
)}`;
if (!host.exists(from) || !from.endsWith('.ts')) {
throw new SchematicsException('Input does not point to an existing TypeScript file.');
}
Expand Down
15 changes: 15 additions & 0 deletions schematics/src/helpers/override/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,19 @@ describe('override Schematic', () => {
]
`);
});

it('should override file if path is windows-styled', async () => {
const tree = await runOverride({
from: 'core\\routing\\product.route.ts',
theme: 'b2b',
ts: true,
});

expect(tree.files.filter(x => x.includes('product.route'))).toMatchInlineSnapshot(`
Array [
"/src/app/core/routing/product.route.ts",
"/src/app/core/routing/product.route.b2b.ts",
]
`);
});
});

0 comments on commit 0fd617a

Please sign in to comment.