-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-changed-tokens.js
More file actions
69 lines (60 loc) · 1.78 KB
/
Copy pathcheck-changed-tokens.js
File metadata and controls
69 lines (60 loc) · 1.78 KB
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
import { readFileSync } from 'fs'
import { execFileSync } from 'child_process'
const baseSha = process.env.BASE_SHA
const files = process.argv.slice(2)
// Content on the base branch; missing means the PR added the file.
const readBase = (file) => {
try {
return execFileSync('git', ['show', `${baseSha}:${file}`], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore']
})
} catch {
return null
}
}
// Content on this PR; missing means the PR deleted the file.
const readHead = (file) => {
try {
return readFileSync(file, 'utf8')
} catch {
return null
}
}
const getMissingTokens = (base, head) => {
const missingTokens = [];
const isLeaf = (obj) =>
!!obj && typeof obj === "object" && "value" in obj && "type" in obj;
const crawlObject = (base, head, path) => {
const keys = Object.keys(base);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!isLeaf(base) && !!head[key]) {
crawlObject(base[key], head[key], `${path ? path + "." : ""}${key}`);
}
if (!isLeaf(base) && !head[key]) {
missingTokens.push(`${path ? path + "." : ""}${key}`);
}
}
};
crawlObject(base, head, "");
return missingTokens;
};
for (const file of files) {
const base = readBase(file);
const head = readHead(file);
const status =
base === null ? "added" : head === null ? "deleted" : "modified";
if (status === "deleted") {
console.log(`File was deleted: ${file}`);
process.exitCode = 1;
}
if (status === "modified") {
const missingTokens = getMissingTokens(JSON.parse(base), JSON.parse(head));
if (missingTokens.length > 0) {
console.log(`missing token(s) from ${file}:`);
missingTokens.forEach((token) => console.log(token));
process.exitCode = 1;
}
}
}