-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversions.js
66 lines (59 loc) · 1.64 KB
/
versions.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
const fs = require("fs");
const { execSync } = require("child_process");
const stdout = execSync("git diff --cached --name-status", {
encoding: "utf8",
});
const diff = stdout
.split(/[\r\n]+/)
.filter((n) => /\.lua$/.test(n))
.map((row) => {
return {
method: row.split(/[\t\s]+/)[0],
filename: row.split(/[\t\s]+/)[1],
};
});
const oldList = JSON.parse(fs.readFileSync("./list.json", "utf8"));
const newFiles = diff
.filter((f) => f.method === "M" || f.method === "A")
.map(({ filename }) => {
version = parseInt(
fs.readFileSync(filename, "utf8").split(/--\s?(\d+)/)[1]
);
return {
name: filename,
version,
};
});
const removedFiles = diff
.filter((f) => f.method === "D")
.map(({ filename }) => filename);
const allCorrect = newFiles.every((newFile) => {
const oldFile = oldList.find((f) => f.name === newFile.name);
const correct = !(oldFile && oldFile.version >= newFile.version);
if (!correct) {
console.error(`Version in ${newFile.name} must be updated`);
}
return correct;
});
if (allCorrect) {
console.log(
`Updating ${diff.length} file record${diff.length === 1 ? "" : "s"}`
);
fs.writeFileSync(
"./list.json",
JSON.stringify(
oldList
.filter(({ name }) => !removedFiles.includes(name))
.reduce((newList, oldFile) => {
return newList.find((f) => f.name === oldFile.name)
? newList
: newList.concat([oldFile]);
}, newFiles)
.sort((a, b) => (a.name > b.name ? 1 : a.name < b.name ? -1 : 0))
)
);
execSync("git add list.json ");
process.exit(0);
} else {
process.exit(1);
}