-
Notifications
You must be signed in to change notification settings - Fork 1
/
a.dependencetree.ts
78 lines (75 loc) · 1.74 KB
/
a.dependencetree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
interface FileTree {
fileName: string;
dependencies?: FileTree[];
}
/**
* 写一个脚本根据文件依赖关系自动计算得到回归范围。
已知通过webpack获取到文件依赖关系文件,通过
简单加工处理,得到json数据。
输入文件,返回回归范围。
*/
const dependenceTreeFind = (
fileTree: FileTree[],
targetFileName: string,
path?: string[]
) => {
const files: string[][] = [];
for (let i = 0; i < fileTree.length; i++) {
const startPath: string[] = path || [];
const file = fileTree[i];
if (file.fileName === targetFileName) {
files.push([...startPath, file.fileName]);
}
if (file.dependencies) {
startPath.push(file.fileName);
files.push(
...dependenceTreeFind(file.dependencies, targetFileName, startPath)
);
}
}
return files;
};
test("dependenceTreeFind", () => {
expect(
dependenceTreeFind(
[
{
fileName: "a.js",
dependencies: [
{
fileName: "b.js",
},
{
fileName: "c.js",
},
],
},
{
fileName: "c.js",
},
{
fileName: "d.js",
dependencies: [
{
fileName: "b.js",
},
{
fileName: "e.js",
dependencies: [
{
fileName: "f.js",
dependencies: [
{
fileName: "c.js",
},
],
},
],
},
],
},
],
"c.js"
)
).toEqual([["a.js", "c.js"], ["c.js"], ["d.js", "e.js", "f.js", "c.js"]]);
});