-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-userscript-version-bumps.mjs
More file actions
73 lines (60 loc) · 1.74 KB
/
Copy pathcheck-userscript-version-bumps.mjs
File metadata and controls
73 lines (60 loc) · 1.74 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
70
71
72
73
#!/usr/bin/env node
import { execFileSync } from 'node:child_process';
const ROOT_FILES = new Set(['thefp.js']);
const SCRIPT_GLOB = /^scripts\/.+\.user\.js$/;
const ZERO_SHA = /^0+$/;
function usage() {
console.error('Usage: node scripts/check-userscript-version-bumps.mjs <base-sha> <head-sha>');
process.exit(2);
}
function isUserscript(path) {
return ROOT_FILES.has(path) || SCRIPT_GLOB.test(path);
}
function git(args) {
return execFileSync('git', args, { encoding: 'utf8' });
}
function changedUserscripts(base, head) {
return git(['diff', '--name-only', base, head])
.trim()
.split('\n')
.filter(Boolean)
.filter(isUserscript);
}
function fileExistsAt(ref, path) {
try {
git(['cat-file', '-e', `${ref}:${path}`]);
return true;
} catch {
return false;
}
}
function versionAt(ref, path) {
const source = git(['show', `${ref}:${path}`]);
const match = source.match(/\/\/\s+@version\s+(\S+)/);
return match ? match[1] : '';
}
const [base, head] = process.argv.slice(2);
if (!base || !head) usage();
if (ZERO_SHA.test(base)) {
console.log('skipping version bump check for initial push');
process.exit(0);
}
const failures = [];
for (const path of changedUserscripts(base, head)) {
if (!fileExistsAt(base, path) || !fileExistsAt(head, path)) {
continue;
}
const before = versionAt(base, path);
const after = versionAt(head, path);
if (!before || !after || before === after) {
failures.push(`${path}: ${before || '(missing)'} -> ${after || '(missing)'}`);
}
}
if (failures.length > 0) {
console.error('Changed userscripts must bump @version:');
for (const failure of failures) {
console.error(` ${failure}`);
}
process.exit(1);
}
console.log('userscript version bumps verified');