forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-features.js
More file actions
162 lines (141 loc) · 4.34 KB
/
diff-features.js
File metadata and controls
162 lines (141 loc) · 4.34 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const { execSync } = require('child_process');
const fs = require('fs');
const yargs = require('yargs');
function main({ ref1, ref2, format, github }) {
let refA, refB;
if (ref1 === undefined && ref2 === undefined) {
// No refs: compare HEAD to parent commit
refA = 'HEAD^';
refB = 'HEAD';
} else if (ref2 === undefined) {
// One ref: compare ref to parent of ref
refB = `${ref1}`;
refA = `${ref1}^`;
} else {
// Two refs: compare ref2 to ref1
refA = `${ref2}`;
refB = `${ref1}`;
}
let aSide = enumerate(refA, github === false);
let bSide = enumerate(refB, github === false);
const results = {
added: [...bSide].filter(feature => !aSide.has(feature)),
removed: [...aSide].filter(feature => !bSide.has(feature)),
};
if (format === 'markdown') {
printMarkdown(results);
} else {
console.log(JSON.stringify(results, undefined, 2));
}
}
function enumerate(ref, skipGitHub) {
if (!skipGitHub) {
try {
return new Set(getEnumerationFromGithub(ref));
} catch {
console.error('Fetching artifact from GitHub failed. Using fallback.');
}
}
return new Set(enumerateFeatures(ref));
}
function getEnumerationFromGithub(ref) {
const ENUMERATE_WORKFLOW = '15595228';
const ENUMERATE_WORKFLOW_ARTIFACT = 'enumerate-features';
const ENUMERATE_WORKFLOW_FILE = 'features.json';
const unlinkFile = () => {
try {
fs.unlinkSync(ENUMERATE_WORKFLOW_FILE);
} catch (err) {
if (err.code == 'ENOENT') {
return;
} else {
throw err;
}
}
};
const hash = execSync(`git rev-parse ${ref}`, {
encoding: 'utf-8',
}).trim();
const workflowRun = execSync(
`gh api /repos/:owner/:repo/actions/workflows/${ENUMERATE_WORKFLOW}/runs?per_page=100 --jq '.workflow_runs[] | select(.head_sha=="${hash}") | .id'`,
{
encoding: 'utf-8',
},
).trim();
if (!workflowRun) throw Error('No workflow run found for commit.');
try {
unlinkFile();
execSync(
`gh run download ${workflowRun} -n ${ENUMERATE_WORKFLOW_ARTIFACT}`,
);
return JSON.parse(
fs.readFileSync(ENUMERATE_WORKFLOW_FILE, { encoding: 'utf-8' }),
);
} finally {
unlinkFile();
}
}
function enumerateFeatures(ref = 'HEAD') {
// Get the short hash for this ref.
// Most of the time, you check out named references (a branch or a tag).
// However, if `ref` is already checked out, then `git worktree add` fails. As
// long as you haven't checked out a detached HEAD for `ref`, then
// `git worktree add` for the hash succeeds.
const hash = execSync(`git rev-parse --short ${ref}`, {
encoding: 'utf-8',
}).trim();
const worktree = `__enumerating__${hash}`;
console.error(`Enumerating features for ${ref} (${hash})`);
try {
execSync(`git worktree add ${worktree} ${hash}`);
execSync(`npm ci`, { cwd: worktree });
execSync(`node ./scripts/enumerate-features.js --data-from=${worktree}`);
return JSON.parse(fs.readFileSync('.features.json', { encoding: 'utf-8' }));
} finally {
execSync(`git worktree remove ${worktree}`);
}
}
function printMarkdown({ added, removed }) {
const fmtFeature = feat => `- \`${feat}\``;
if (removed.length) {
console.log('## Removed\n');
console.log(removed.map(fmtFeature).join('\n'));
}
if (added.length) {
if (removed.length) console.log('');
console.log('## Added\n');
console.log(added.map(fmtFeature).join('\n'));
}
}
const { argv } = yargs.command(
'$0 [ref1] [ref2]',
'Compare the set of features at refA and refB',
yargs => {
yargs
.positional('ref1', {
description: 'A Git ref (branch, tag, or commit)',
defaultDescription: 'ref1^',
})
.positional('ref2', {
description: 'A Git ref (branch, tag, or commit)',
defaultDescription: 'HEAD',
})
.option('format', {
type: 'string',
nargs: 1,
choices: ['json', 'markdown'],
demand: 'a named format is required',
default: 'markdown',
})
.option('no-github', {
type: 'boolean',
description: "Don't fetch artifacts from GitHub.",
})
.example('$0', 'compare HEAD to parent commmit')
.example('$0 176d4ed', 'compare 176d4ed to its parent commmit')
.example('$0 topic-branch main', 'compare a branch to main');
},
);
if (require.main === module) {
main(argv);
}