-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnx-ignore.js
100 lines (88 loc) · 2.47 KB
/
nx-ignore.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const { findRootSync } = require("@manypkg/find-root");
console.log(
"≫ Using Nx to determine if this project is affected by the commit..."
);
const root = getRoot();
const scope = getScope();
const command = `npx nx print-affected --base=HEAD^`;
console.log(`≫ Analyzing results of \`${command}\`...`);
exec(
command,
{
cwd: root,
},
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
try {
const parsed = JSON.parse(stdout);
if (parsed == null) {
console.error(`≫ Failed to parse JSON output from \`${command}\`.`);
console.error(`≫ Proceeding with build to be safe...`);
process.exit(1);
}
const { packages } = parsed;
if (packages && packages.length > 0) {
console.log(
`≫ The commit affects this project and/or its ${packages.length - 1
} dependencies`
);
console.log(`≫ Proceeding with build...`);
process.exit(1);
} else {
console.log("≫ This project and its dependencies are not affected");
console.log("≫ Ignoring the change");
process.exit(0);
}
} catch (e) {
console.error(`≫ Failed to parse JSON output from \`${command}\`.`);
console.error(e);
console.error(`≫ Proceeding with build to be safe...`);
process.exit(1);
}
}
);
function searchUp(pathName, cwd) {
const root = path.parse(cwd).root;
let found = false;
while (!found && cwd !== root) {
if (fs.existsSync(path.join(cwd, pathName))) {
found = true;
break;
}
cwd = path.dirname(cwd);
}
if (found) {
return cwd;
}
return null;
}
function getScope() {
if (process.argv.length > 1 && process.argv[2] != null) {
return process.argv[2];
}
const raw = fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8");
const pkgJSON = JSON.parse(raw);
console.log(`≫ Inferred \`${pkgJSON.name}\` as scope from "./package.json"`);
return pkgJSON.name;
}
function getRoot() {
let root = searchUp("nx.json", process.cwd());
if (!root) {
root = findRootSync(process.cwd());
if (!root) {
console.error(
"Error: workspace root not found. turbo-ignore inferencing failed, proceeding with build."
);
console.error("");
process.exit(1);
}
}
return root;
}