-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathdiff-flat.js
More file actions
710 lines (635 loc) · 19.9 KB
/
diff-flat.js
File metadata and controls
710 lines (635 loc) · 19.9 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
/** @import {CompatData, SimpleSupportStatement} from '../types/types.js' */
/**
* @typedef {'html' | 'plain'} Format
*/
import { styleText } from 'node:util';
import { diffArrays } from 'diff';
import esMain from 'es-main';
import stripAnsi from 'strip-ansi';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import bcd from '../index.js';
import { spawn, walk } from '../utils/index.js';
import { addVersionLast, applyMirroring, transformMD } from './build/index.js';
import { getMergeBase, getFileContent, getGitDiffStatuses } from './lib/git.js';
import dataFolders from './lib/data-folders.js';
const BROWSER_NAMES = Object.keys(bcd.browsers);
/** @type {Format[]} */
const FORMATS = ['html', 'plain'];
const DEFAULT_FORMAT = 'plain';
// FIXME This is bad.
/** @type {string[]} */
const allFlags = [];
/** @type {string[]} */
const allNotes = [];
/**
* Formats a flag reference.
* @param {number} index the flag index.
* @returns {string} formatted reference.
*/
const formatFlagIndex = (index) => `[^f${index + 1}]`;
/**
* Formats a flag reference.
* @param {number} index the flag index.
* @returns {string} formatted reference.
*/
const formatNoteIndex = (index) => `[^n${index + 1}]`;
/**
* Flattens an object.
* @param {*} obj the object to flatten.
* @param {string} [parentKey] the parent key path.
* @param {Record<string, *>} [result] the intermediate result.
* @returns {Record<string, *>} the flattened object.
*/
const flattenObject = (obj, parentKey = '', result = {}) => {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const fullKey = parentKey ? `${parentKey}.${key}` : key;
if (
key !== 'version' &&
typeof obj[key] === 'string' &&
obj[key] === 'mirror'
) {
obj[key] = {
version: 'mirror',
};
}
if (typeof obj[key] === 'object' && obj[key] !== null) {
// Merge values.
if ('status' in obj[key]) {
const { deprecated, standard_track, experimental } = obj[key].status;
const statusFlags = [
deprecated && 'deprecated',
standard_track && 'standard_track',
experimental && 'experimental',
].filter(Boolean);
obj[key].status = statusFlags.join(',');
}
if ('tags' in obj[key]) {
obj[key].tags = obj[key].tags.join(',');
}
if ('version_added' in obj[key]) {
if ('flags' in obj[key]) {
// Deduplicate flag.
const flagsJson = JSON.stringify(obj[key].flags);
if (!allFlags.includes(flagsJson)) {
allFlags.push(flagsJson);
}
const flagIndex = allFlags.indexOf(flagsJson);
obj[key].flags = formatFlagIndex(flagIndex);
}
if ('notes' in obj[key]) {
const notes = toArray(obj[key].notes);
obj[key].notes = notes
.map((note) => {
const notesJson = JSON.stringify(note);
if (!allNotes.includes(notesJson)) {
allNotes.push(notesJson);
}
const noteIndex = allNotes.indexOf(notesJson);
return noteIndex;
})
.sort()
.map((index) => formatNoteIndex(index))
.join(',');
}
const {
version_added,
version_last,
partial_implementation,
alternative_name,
prefix,
flags,
notes,
} = /** @type {SimpleSupportStatement} */ (obj[key]);
const parts = [
typeof version_added === 'string'
? typeof version_last === 'string'
? `${version_added}−${version_last}`
: `${version_added}+`
: `${version_added}`,
partial_implementation && '(partial)',
flags,
prefix && `prefix=${prefix}`,
alternative_name && `altname=${alternative_name}`,
notes,
].filter(Boolean);
obj[key].version = parts.join(' ');
delete obj[key].version_added;
delete obj[key].version_last;
delete obj[key].version_removed;
delete obj[key].partial_implementation;
delete obj[key].alternative_name;
delete obj[key].prefix;
delete obj[key].flags;
delete obj[key].notes;
}
// Recursively flatten nested objects
flattenObject(
BROWSER_NAMES.includes(key) ? toArray(obj[key]).reverse() : obj[key],
fullKey,
result,
);
} else {
// Assign value to the flattened key
result[fullKey] = obj[key];
}
}
}
return result;
};
/**
* Converts value to array unless it isn't.
* @param {*} value array or any value.
* @returns {*[]} the array, or an array with the value as a single item.
*/
const toArray = (value) => {
if (!Array.isArray(value)) {
value = [value];
}
return value;
};
/**
* Formats a key diff'ed with the previous key.
* @param {string} key the current key
* @param {string} lastKey the previous key
* @param {object} options Options
* @param {number} [options.fill] The number of characters to fill up to
* @param {Format} options.format Whether to return HTML, otherwise plaintext
* @returns {string} diffed key
*/
const diffKeys = (key, lastKey, options) => {
const len = key.length;
let fill = options.fill ?? 0;
/**
* Filters out irrelevant keys.
* @param {string} part the key part.
* @returns {boolean} true, if the part should be ignored, false otherwise
*/
const keyFilter = (part) => part !== '__compat' && part !== 'support';
return diffArrays(
lastKey.split('.').filter(keyFilter),
key.split('.').filter(keyFilter),
)
.filter((part) => !part.removed)
.map((part) => {
const key = part.value.join('.');
if (part.added) {
const space = fill && len < fill ? ' '.repeat(fill - len) : '';
fill = 0;
return (
(options.format === 'html'
? `<strong>${key}</strong>`
: styleText('blue', key)) + space
);
}
return key;
})
.join('.');
};
/**
* Deeply merges a source object into a target object.
* @param {*} target The target object to merge into.
* @param {*} source The source object to merge.
* @returns {*} the target object with source merged.
*/
const deepMerge = (target, source) => {
if (typeof target !== 'object' || target === null) {
return source;
}
if (typeof source !== 'object' || source === null) {
return source;
}
for (const key of Object.keys(source)) {
const sourceValue = source[key];
const targetValue = target[key];
if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {
target[key] = targetValue.concat(sourceValue);
} else if (
typeof sourceValue === 'object' &&
typeof targetValue === 'object' &&
sourceValue !== null &&
targetValue !== null
) {
target[key] = deepMerge({ ...targetValue }, sourceValue);
} else {
target[key] = sourceValue;
}
}
return target;
};
/**
* Print diffs
* @param {string} base Base ref
* @param {string} head Head ref
* @param {object} options Options
* @param {boolean} options.group Whether to group by value, rather than the common feature
* @param {Format} options.format What output format to use ("color", "html" or "patch")
* @param {boolean} options.mirror Whether to apply mirroring, rather than ignore "mirror" values
* @param {boolean} options.transform Whether to apply transforms
* @returns {void}
*/
const printDiffs = (base, head, options) => {
if (options.format === 'html') {
console.log('<pre style="font-family: monospace">');
}
/** @type {Map<string, Set<string>>} */
const groups = new Map();
/** @type {CompatData} */
const baseContents = /** @type {*} */ ({});
/** @type {CompatData} */
const headContents = /** @type {*} */ ({});
for (const status of getGitDiffStatuses(base, head)) {
if (
!(
status.headPath.endsWith('.json') &&
dataFolders.some((folder) => status.headPath.startsWith(`${folder}/`))
)
) {
continue;
}
const baseFileContents = /** @type {CompatData} */ (
status.value !== 'A'
? JSON.parse(getFileContent(base, status.basePath))
: {}
);
const headFileContents = /** @type {CompatData} */ (
status.value !== 'D'
? JSON.parse(getFileContent(head, status.headPath))
: {}
);
deepMerge(baseContents, baseFileContents);
deepMerge(headContents, headFileContents);
}
if (options.mirror) {
for (const feature of walk(undefined, baseContents)) {
applyMirroring(feature);
}
for (const feature of walk(undefined, headContents)) {
applyMirroring(feature);
}
}
for (const feature of walk(undefined, baseContents)) {
addVersionLast(feature);
}
for (const feature of walk(undefined, headContents)) {
addVersionLast(feature);
}
if (options.transform) {
for (const feature of walk(undefined, baseContents)) {
transformMD(feature);
}
for (const feature of walk(undefined, headContents)) {
transformMD(feature);
}
}
const baseData = flattenObject(baseContents);
const headData = flattenObject(headContents);
const keys = [
...new Set([...Object.keys(baseData), ...Object.keys(headData)]).values(),
].sort();
if (!keys.length) {
console.log('✔ No data file changed.');
return;
}
const prefix = diffArrays(
keys.at(0)?.split('.') ?? [],
keys.at(-1)?.split('.') ?? [],
)[0]?.value.join('.');
const commonName =
options.format === 'html' ? `<h3>${prefix}</h3>` : `${prefix}`;
let lastKey = '';
for (const key of keys) {
const baseValue = JSON.stringify(baseData[key] ?? null);
const headValue = JSON.stringify(headData[key] ?? null);
if (baseValue === headValue) {
continue;
}
if (!lastKey) {
lastKey = key;
}
const keyDiff = diffKeys(
key.slice(prefix.length),
lastKey.slice(prefix.length),
options,
);
const splitRegexp =
/(?<=^")|(?<=[\],/ ])|(?=[[,/ ])|(?="$)|(?<=\d)(?=−)|(?<=−)(?=\d)|(?=#)/;
let headValueForDiff = headValue;
let baseValueForDiff = baseValue;
if (baseValue == 'null') {
baseValueForDiff = '';
if (headValue == '"mirror"' || headValue == '"false"') {
// Ignore initial "mirror"/"false" values.
headValueForDiff = '';
}
} else if (headValue == 'null') {
headValueForDiff = '';
}
const valueDiff = diffArrays(
headValueForDiff.split(splitRegexp),
baseValueForDiff.split(splitRegexp),
)
.map((part) => {
// Note: removed/added is deliberately inversed here, to have additions first.
const value = part.value.join('');
if (part.removed) {
return options.format == 'html'
? `<ins style="color: green">${value}</ins>`
: styleText('green', value);
} else if (part.added) {
return options.format == 'html'
? `<del style="color: red">${value}</del>`
: styleText('red', value);
}
return value;
})
.join('');
const value = valueDiff;
if (!value.length) {
// e.g. null => "mirror"
continue;
}
if (options.group) {
const reverseKeyParts = key.split('.').reverse();
const browser = reverseKeyParts.find((part) =>
BROWSER_NAMES.includes(part),
);
const field = reverseKeyParts.find((part) => !/^\d+$/.test(part));
const groupKey = `${!browser ? '' : options.format == 'html' ? `<strong>${browser}</strong>.` : `${styleText('cyan', browser)}.`}${field} = ${value}`;
const groupValue = key
.split('.')
.map((part) => (part !== browser && part !== field ? part : '{}'))
.reverse()
.filter((value, index) => index > 0 || value !== '{}')
.reverse()
.map((value) =>
value !== '{}'
? value
: options.format == 'html'
? '<small>{}</small>'
: styleText('dim', '{}'),
)
.join('.');
const group = groups.get(groupKey) ?? new Set();
group.add(groupValue);
groups.set(groupKey, group);
} else {
const change = `${keyDiff} = ${value}`;
const group = groups.get(commonName) ?? new Set();
group.add(change);
groups.set(commonName, group);
}
lastKey = key;
}
if (groups.size === 0) {
console.log('✔ No changes.');
return;
}
/** @type {[string, string[]][]} */
const originalEntries = [...groups.entries()].map(([key, set]) => [
key,
[...set.values()],
]);
/** @type {Map<string, string[]>} */
const entryGroups = new Map();
for (const [key, values] of originalEntries) {
const groupKey = JSON.stringify(values);
const keys = entryGroups.get(groupKey) ?? [];
keys.push(key);
entryGroups.set(groupKey, keys);
}
const rawEntries = [...entryGroups.entries()];
if (options.group) {
// Natural sort.
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
});
rawEntries.sort(([, a], [, b]) =>
collator.compare(
stripAnsi(/** @type {string} */ (a.at(0))),
stripAnsi(/** @type {string} */ (b.at(0))),
),
);
}
const entries = rawEntries.map(([valuesJson, keys]) => [
keys,
JSON.parse(valuesJson),
]);
const json = JSON.stringify(entries);
for (const flagIndex of allFlags.keys()) {
if (!json.includes(formatFlagIndex(flagIndex))) {
allFlags[flagIndex] = '';
}
}
for (const noteIndex of allNotes.keys()) {
if (!json.includes(formatNoteIndex(noteIndex))) {
allNotes[noteIndex] = '';
}
}
/**
* Prints references found in the inputs.
* @param {...string} inputs the inputs to scan for references.
* @returns {void}
*/
const printRefs = (...inputs) => {
/** @type {string[]} */
const lines = [];
for (const [index, content] of allFlags.entries()) {
const ref = formatFlagIndex(index);
if (inputs.some((input) => input.includes(ref))) {
lines.push(`${ref}: ${content}`);
}
}
for (const [index, content] of allNotes.entries()) {
const ref = formatNoteIndex(index);
if (inputs.some((input) => input.includes(ref))) {
lines.push(`${ref}: ${content}`);
}
}
if (lines.length > 0) {
console.log();
lines.forEach((line) =>
console.log(
options.format == 'html'
? `<em>${line}</em>`
: styleText('italic', line),
),
);
}
};
for (const entry of entries) {
/** @type {string | null} */
let previousKey = null;
if (options.group) {
const [values, keys] = entry;
if (keys.length == 1) {
const key = /** @type {string} */ (keys.at(0));
const keyDiff = diffKeys(key, previousKey ?? key, options);
values.forEach((value) => console.log(`${value}`));
console.log(` ${keyDiff}`);
printRefs(...values);
previousKey = key;
} else {
previousKey = null;
console.log(values.join('\n'));
const maxKeyLength = Math.max(...keys.map((key) => key.length));
if (options.format == 'html') {
process.stdout.write(
`<details><summary>${keys.length} ${keys.length === 1 ? 'path' : 'paths'}</summary>`,
);
}
for (const key of keys) {
const keyDiff = diffKeys(
key,
previousKey ?? /** @type {string} */ (keys.at(1)),
{
...options,
fill: maxKeyLength,
},
);
console.log(` ${keyDiff}`);
previousKey = key;
}
if (options.format == 'html') {
process.stdout.write('</details>');
}
printRefs(...values);
previousKey = null;
}
} else {
const [keys, values] = entry;
if (values.length == 1) {
for (const key of keys) {
const keyDiff = diffKeys(key, previousKey ?? key, options);
console.log(`${keyDiff}`);
previousKey = key;
}
values.forEach((value) => console.log(` ${value}`));
printRefs(...values);
} else {
for (const key of keys) {
const keyDiff = diffKeys(key, previousKey ?? key, options);
console.log(`${keyDiff}`);
previousKey = key;
}
values.forEach((value) => console.log(` ${value}`));
printRefs(...values);
}
previousKey = null;
}
console.log('');
}
if (options.format == 'html') {
console.log('</pre>');
}
};
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',
)
.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',
})
.option('format', {
alias: 'f',
type: 'string',
default: DEFAULT_FORMAT,
choices: /** @type {Readonly<Format[]>} */ (FORMATS),
/**
* @param {string} value
* @returns {Format}
*/
coerce: (value) => FORMATS.find((f) => f === value) ?? DEFAULT_FORMAT,
})
.option('group', {
type: 'boolean',
default: true,
})
.option('mirror', {
type: 'boolean',
default: false,
})
.option('transform', {
type: 'boolean',
default: false,
})
.parseSync();
const options = argv;
if (/^\d+$/.test(options.base)) {
options.head = `pull/${options.base}/merge`;
options.base = 'origin/main';
}
const remote =
spawn('git', ['remote', '-v'])
.split('\n')
.find((line) => line.includes('mdn/browser-compat-data'))
?.split(/\s+/, 2)
.at(0) ?? 'origin';
/**
* Runs `git fetch` for a reference.
* @param {string} ref - the reference to fetch.
* @returns {string} Combined standard output/error of the command.
*/
const gitFetch = (ref) => spawn('git', ['fetch', remote, ref]);
/**
* Runs `git rev-parse` for a reference.
* @param {string} ref - the reference to parse.
* @returns {string} Standard output of the command.
*/
const gitRevParse = (ref) => spawn('git', ['rev-parse', ref]);
/**
* Resolves and fetches the reference.
* @param {string} ref - the reference to fetch and resolve.
* @returns {string} Commit hash corresponding to the reference.
*/
const fetchAndResolve = (ref) => {
if (ref.startsWith('origin/')) {
const remoteRef = ref.slice('origin/'.length);
gitFetch(remoteRef);
return gitRevParse(ref);
} else if (ref.startsWith(`${remote}/`)) {
const remoteRef = ref.slice(`${remote}/`.length);
gitFetch(remoteRef);
return gitRevParse(ref);
} else if (ref.startsWith('pull/')) {
gitFetch(ref);
return gitRevParse('FETCH_HEAD');
} else if (ref.includes(':')) {
const remoteRef = `gh pr view ${ref} --json headRefOid -q '.headRefOid'`;
gitFetch(remoteRef);
return remoteRef;
} else if (/^[0-9a-f]{40}$/.test(ref)) {
try {
gitRevParse(ref);
} catch {
gitFetch(ref);
}
return ref;
}
return gitRevParse(ref);
};
options.base = fetchAndResolve(options.base);
options.head = fetchAndResolve(options.head);
const { base, head, group, format, mirror, transform } = options;
printDiffs(getMergeBase(base, head), head, {
group,
format,
mirror,
transform,
});
}