forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff.ts
233 lines (215 loc) · 6.59 KB
/
diff.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import chalk from 'chalk-template';
import deepDiff, { Diff } from 'deep-diff';
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { query } from '../utils/index.js';
import { SupportStatement, Identifier, BrowserName } from '../types/types.js';
import { getMergeBase, getFileContent, getGitDiffStatuses } from './lib/git.js';
import mirror from './build/mirror.js';
interface Contents {
base: string;
head: string;
}
interface DiffItem {
name: string;
description: string;
}
/**
* Get contents from base and head commits
* Note: This does not detect renamed files
* @param baseCommit Base commit
* @param basePath Base path
* @param headCommit Head commit
* @param headPath Head path
* @returns The contents of both commits
*/
const getBaseAndHeadContents = (
baseCommit: string,
basePath: string,
headCommit: string,
headPath: string,
): Contents => {
const base = JSON.parse(getFileContent(baseCommit, basePath));
const head = JSON.parse(getFileContent(headCommit, headPath));
return { base, head };
};
/**
* Returns a formatted string of before-and-after changes
* @param lhs Left-hand (before) side
* @param rhs Right-hand (after) side
* @returns Formatted string
*/
const stringifyChange = (lhs: any, rhs: any): string =>
`${JSON.stringify(lhs)} → ${JSON.stringify(rhs)}`;
/**
* Perform mirroring on specified diff statement
* @param diff The diff to perform mirroring on
* @param diff.base The diff to perform mirroring on
* @param diff.head The diff to perform mirroring on
* @param contents The contents to mirror from
* @param contents.base The contents to mirror from
* @param contents.head The contents to mirror from
* @param path The feature path to mirror
* @param direction Whether to mirror 'base' or 'head'
*/
const doMirror = (
diff: { base: SupportStatement; head: SupportStatement },
contents: { base: Identifier; head: Identifier },
path: string[],
direction: 'base' | 'head',
): void => {
const browser = path[path.length - 1] as BrowserName;
const dataPath = path.slice(0, path.length - 3).join('.');
const data = contents[direction];
diff[direction] = mirror(browser, query(dataPath, data).__compat.support);
};
/**
* Describe the diff in text form (internal function)
* @param diffItem The diff to describe
* @param contents The contents of the diff
* @returns A human-readable diff description
*/
const describeByKind = (
diffItem: Diff<string, string>,
contents: Contents,
): string => {
const diff = { base: (diffItem as any).lhs, head: (diffItem as any).rhs };
// Handle mirroring
let doesMirror = '';
if (diff.base === 'mirror') {
doesMirror = 'No longer mirrors';
doMirror(
diff,
contents as any as { base: Identifier; head: Identifier },
diffItem.path as string[],
'base',
);
} else if (diff.head === 'mirror') {
doesMirror = 'Now mirrors';
doMirror(
diff,
contents as any as { base: Identifier; head: Identifier },
diffItem.path as string[],
'head',
);
}
switch (diffItem.kind) {
case 'N':
return 'added';
case 'D':
return 'deleted';
case 'A':
case 'E':
return `edited (${stringifyChange(diff.base, diff.head)})${
doesMirror && ` - ${doesMirror}`
}`;
default:
return '';
}
};
/**
* Describe the diff in text form
* @param diffItem The diff to describe
* @param contents The contents of the diff
* @returns A human-readable diff description
*/
const describeDiffItem = (
diffItem: Diff<string, string>,
contents: Contents,
): DiffItem => {
const path = (diffItem.path as string[]).join('.');
if (path.includes('.__compat.')) {
const [name, member] = path.split('.__compat.');
if (path.endsWith('.notes') && diffItem.kind === 'E') {
return { name, description: `${member} is edited (prose change)` };
}
return {
name,
description: `${member} is ${describeByKind(diffItem, contents)}`,
};
}
return {
name: (diffItem.path as string[]).slice(0, -1).join('.'),
description: `${path} is ${describeByKind(diffItem, contents)}`,
};
};
/**
* Merge diff together as a map
* @param items Diff items to merge
* @returns A map of the diff items
*/
const mergeAsMap = (items: DiffItem[]): Map<string, string> => {
const map = new Map();
for (const item of items) {
const descriptions = map.get(item.name) || [];
descriptions.push(item.description);
map.set(item.name, descriptions);
}
return map;
};
/**
* Get the diffs as a map
* @param base Base ref
* @param head Head ref
* @returns A map of the diff items
*/
const getDiffs = (base: string, head = ''): Map<string, string> => {
const namedDescriptions: { name: string; description: string }[] = [];
for (const status of getGitDiffStatuses(base, head)) {
if (!status.headPath.endsWith('.json') || !status.headPath.includes('/')) {
continue;
}
// Note that A means Added for git while it means Array for deep-diff
if (status.value === 'A' || status.value === 'D') {
namedDescriptions.push({
name: status.basePath.replace(/\//g, '.').slice(0, -5), // trim file extension
description: status.value === 'A' ? 'Newly added' : 'Entirely removed',
});
} else {
const contents = getBaseAndHeadContents(
base,
status.basePath,
head,
status.headPath,
);
const diff = deepDiff.diff(contents.base, contents.head);
if (diff) {
namedDescriptions.push(
...diff.map((item) => describeDiffItem(item, contents)),
);
}
}
}
return mergeAsMap(namedDescriptions);
};
if (esMain(import.meta)) {
const { argv } = yargs(hideBin(process.argv)).command(
'$0 [base] [head]',
'Print a formatted diff for changes between base and head commits',
(yargs) => {
yargs
.positional('base', {
describe:
'The base commit; may be commit hash or other git ref (e.g. "origin/main")',
type: 'string',
default: 'origin/main',
})
.positional('head', {
describe:
'The head commit that changes are applied to; may be commit hash or other git ref (e.g. "origin/main")',
type: 'string',
default: 'HEAD',
});
},
);
const { base, head } = argv as any;
for (const [key, values] of getDiffs(getMergeBase(base, head), head)) {
console.log(chalk`{bold ${key}}:`);
for (const value of values) {
console.log(` → ${value}`);
}
}
}