Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit a1a5c06

Browse files
author
Aleksei Dmitriev
authored
fix: Workspace path match (#33)
* fix: Project path compare * chore: Create @garment/utils package, add isSubPath win32 test * chore: Change path import
1 parent a16a853 commit a1a5c06

File tree

9 files changed

+83
-7
lines changed

9 files changed

+83
-7
lines changed

core/garment/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
"@garment/runner": "^0.14.0",
1010
"@garment/scheduler": "^0.14.0",
1111
"@garment/schema-validator": "^0.14.0",
12+
"@garment/utils": "^0.0.14",
1213
"@garment/workspace": "^0.14.0",
1314
"@parcel/watcher": "^2.0.0-alpha.9",
1415
"fs-extra": "8.1.0",
16+
"globby": "10.0.1",
1517
"is-valid-path": "^0.1.1",
1618
"matcher": "^2.1.0",
1719
"memfs": "^3.1.2",
1820
"multimatch": "^4.0.0",
1921
"normalize-path": "^3.0.0",
2022
"tempy": "0.3.0",
21-
"unionfs": "^4.4.0",
22-
"globby": "10.0.1"
23+
"unionfs": "^4.4.0"
2324
},
2425
"files": [
2526
"lib",
@@ -30,4 +31,4 @@
3031
"@types/is-valid-path": "^0.1.0",
3132
"@types/tempy": "^0.2.0"
3233
}
33-
}
34+
}

core/garment/src/garment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { FileCache } from './FileCache';
3939
import { getProjectsByName } from './getProjectsByName';
4040
import globby = require('globby');
4141
import normalizePath = require('normalize-path');
42+
import { isSubPath } from '@garment/utils';
4243

4344
export type Cache =
4445
| {
@@ -954,7 +955,7 @@ async function garmentFromWorkspace(
954955

955956
if (
956957
subscription.type === 'glob' &&
957-
normalizePath(event.path).startsWith(subscription.input.rootDir)
958+
isSubPath(subscription.input.rootDir, normalizePath(event.path))
958959
) {
959960
const matched = multimatch(
960961
event.path,

core/utils/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@garment/utils",
3+
"version": "0.0.14",
4+
"main": "lib/index.js",
5+
"license": "MIT",
6+
"dependencies": {},
7+
"files": [
8+
"lib"
9+
]
10+
}

core/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './isSubPath';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { isSubPath } from '..';
2+
import * as Path from 'path';
3+
4+
describe('utils | isSubPath', () => {
5+
it('should return true if path is a subpath', () => {
6+
expect(isSubPath('/root', '/root/child')).toBeTruthy();
7+
});
8+
9+
it('should return false if path is not a subpath', () => {
10+
expect(isSubPath('/root/foo', '/root/bar/baz')).toBeFalsy();
11+
});
12+
13+
it('should return false if path has a similar substring', () => {
14+
expect(isSubPath('/root/foo', '/root/foo bar/baz')).toBeFalsy();
15+
});
16+
17+
it("should return true if path has special path .. symbols and it's a subpath", () => {
18+
expect(isSubPath('/root/foo', '/root/../root/foo/bar')).toBeTruthy();
19+
});
20+
21+
it("should return true if path has special path . symbol and it's a subpath", () => {
22+
expect(isSubPath('/root/foo', '/root/./foo/bar')).toBeTruthy();
23+
});
24+
});
25+
26+
describe('utils | isSubPath [win32]', () => {
27+
let relativeSpy: jest.SpyInstance;
28+
let isAbsoluteSpy: jest.SpyInstance;
29+
30+
beforeEach(() => {
31+
relativeSpy = jest
32+
.spyOn(Path, 'relative')
33+
.mockImplementationOnce(Path.win32.relative);
34+
isAbsoluteSpy = jest
35+
.spyOn(Path, 'isAbsolute')
36+
.mockImplementationOnce(Path.win32.isAbsolute);
37+
});
38+
39+
afterEach(() => {
40+
relativeSpy.mockRestore();
41+
isAbsoluteSpy.mockRestore();
42+
});
43+
44+
it('should return true if path is a subpath', () => {
45+
expect(isSubPath('C:\\Foo', 'C:\\Foo\\Bar')).toBeTruthy();
46+
});
47+
it('should return false if path is not a subpath', () => {
48+
expect(isSubPath('C:\\Foo\\Bar', 'D:\\Foo\\Bar')).toBeFalsy();
49+
});
50+
});

core/utils/src/isSubPath/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as Path from 'path';
2+
3+
export const isSubPath = (parent: string, child: string) => {
4+
const relative = Path.relative(parent, child);
5+
6+
return relative && !relative.startsWith('..') && !Path.isAbsolute(relative);
7+
};

core/workspace/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
],
1010
"dependencies": {
1111
"@garment/schema-validator": "^0.14.0",
12+
"@garment/utils": "^0.0.14",
1213
"dependency-graph": "^0.8.0",
1314
"fs-extra": "8.1.0",
1415
"mustache": "^3.0.1",
@@ -21,4 +22,4 @@
2122
"@types/normalize-path": "^3.0.0",
2223
"@types/object-hash": "^1.3.0"
2324
}
24-
}
25+
}

core/workspace/src/ProjectRegistry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Project } from './Project';
22

33
import normalizePath = require('normalize-path');
4+
import { isSubPath } from '@garment/utils';
45

56
export class ProjectRegistry {
67
[Symbol.iterator]() {
@@ -32,13 +33,13 @@ export class ProjectRegistry {
3233
getByPaths(...paths: string[]) {
3334
paths = paths.map(path => normalizePath(path));
3435
return this.list().filter(project =>
35-
paths.some(path => path.indexOf(project.fullPath) === 0)
36+
paths.some(path => isSubPath(project.fullPath, path))
3637
);
3738
}
3839

3940
getByPath(path: string) {
4041
path = normalizePath(path);
41-
return this.list().find(project => path.indexOf(project.fullPath) === 0);
42+
return this.list().find(project => isSubPath(project.fullPath, path));
4243
}
4344

4445
getByPathExact(path: string) {

garment.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@
268268
"visualize-graph": {
269269
"path": "utils/visualize-graph",
270270
"extends": ["tspackage"]
271+
},
272+
"utils": {
273+
"path": "core/utils",
274+
"extends": ["tspackage"]
271275
}
272276
},
273277
"schematics": ["@garment/schematics", "@garment/schematics-typescript"],

0 commit comments

Comments
 (0)