Skip to content

Commit 7f4b74f

Browse files
committed
fix(watch): normalize changed file paths
closes #1488
1 parent 0d983b8 commit 7f4b74f

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎src/app/application.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
cleanNameWithoutSpaceAndToLowerCase,
3838
cleanSourcesForWatch,
3939
findMainSourceFolder,
40+
normalizeWatchFilePath,
4041
} from "../utils/utils";
4142

4243
import { AdditionalNode } from "./interfaces/additional-node.interface";
@@ -3711,7 +3712,7 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
37113712
path.extname(file) === ".json"
37123713
) {
37133714
this.watchChangedFiles.push(
3714-
path.join(cwd + path.sep + file),
3715+
normalizeWatchFilePath(file, cwd),
37153716
);
37163717
waiterChange();
37173718
}

‎src/utils/utils.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,24 @@ export function cleanLifecycleHooksFromMethods(methods: Array<any>): Array<any>
141141

142142
export function cleanSourcesForWatch(list) {
143143
return list.filter(element => {
144-
if (fs.existsSync(process.cwd() + path.sep + element)) {
145-
return element;
146-
}
144+
const normalizedPath = isAbsoluteFilePath(element)
145+
? path.normalize(element)
146+
: path.resolve(process.cwd(), element);
147+
return fs.existsSync(normalizedPath);
147148
});
148149
}
149150

151+
export function isAbsoluteFilePath(filePath: string): boolean {
152+
return path.isAbsolute(filePath) || /^[A-Za-z]:[\\/]/.test(filePath);
153+
}
154+
155+
export function normalizeWatchFilePath(filePath: string, basePath: string): string {
156+
if (isAbsoluteFilePath(filePath)) {
157+
return path.normalize(filePath);
158+
}
159+
return path.resolve(basePath, filePath);
160+
}
161+
150162
export function getNamesCompareFn(name?) {
151163
/**
152164
* Copyright https://github.com/ng-bootstrap/ng-bootstrap
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expect } from 'chai';
2+
import * as fs from 'fs-extra';
3+
import * as path from 'path';
4+
5+
import { cleanSourcesForWatch, normalizeWatchFilePath } from '../../../../src/utils/utils';
6+
7+
describe('Watch path utilities', () => {
8+
describe('normalizeWatchFilePath', () => {
9+
it('should resolve relative paths from the provided base path', () => {
10+
const basePath = path.resolve(process.cwd(), 'test/fixtures');
11+
const result = normalizeWatchFilePath('sample-files/index.ts', basePath);
12+
13+
expect(result).to.equal(path.resolve(basePath, 'sample-files/index.ts'));
14+
});
15+
16+
it('should keep absolute POSIX paths as-is', () => {
17+
const absolutePath = path.resolve(process.cwd(), 'test/fixtures/sample-files/index.ts');
18+
const result = normalizeWatchFilePath(absolutePath, process.cwd());
19+
20+
expect(result).to.equal(path.normalize(absolutePath));
21+
});
22+
23+
it('should keep absolute Windows-style paths as-is', () => {
24+
const windowsPath = 'C:\\repo\\src\\app.component.ts';
25+
const result = normalizeWatchFilePath(windowsPath, process.cwd());
26+
27+
expect(result).to.equal(path.normalize(windowsPath));
28+
});
29+
});
30+
31+
describe('cleanSourcesForWatch', () => {
32+
const tempRoot = path.join(process.cwd(), '.tmp-watch-path-utils');
33+
const absoluteFile = path.join(tempRoot, 'absolute.ts');
34+
const relativeFile = '.tmp-watch-path-utils/relative.md';
35+
36+
before(() => {
37+
fs.ensureDirSync(tempRoot);
38+
fs.writeFileSync(absoluteFile, 'export const absolute = true;');
39+
fs.writeFileSync(path.join(process.cwd(), relativeFile), '# relative');
40+
});
41+
42+
after(() => {
43+
fs.removeSync(tempRoot);
44+
});
45+
46+
it('should keep existing absolute and relative sources', () => {
47+
const result = cleanSourcesForWatch([absoluteFile, relativeFile, 'missing-file.ts']);
48+
49+
expect(result).to.include(absoluteFile);
50+
expect(result).to.include(relativeFile);
51+
expect(result).to.not.include('missing-file.ts');
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)