forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompareAPIs.js
More file actions
361 lines (344 loc) · 12.5 KB
/
Copy pathcompareAPIs.js
File metadata and controls
361 lines (344 loc) · 12.5 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
let fs = require('fs-extra');
let fg = require('fast-glob');
let path = require('path');
let changesets = require('json-diff-ts');
let util = require('util');
let {walkObject} = require('walk-object');
let chalk = require('chalk');
let yargs = require('yargs');
let argv = yargs
.option('verbose', {alias: 'v', type: 'boolean'})
.option('organizedBy', {choices: ['type', 'change']})
.option('rawNames', {type: 'boolean'})
.argv;
compare().catch(err => {
console.error(err.stack);
process.exit(1);
});
/**
* This takes the json files generated by the buildBranchAPI and buildPublishedAPI and diffs each of the corresponding
* json files. It outputs a JSON string that tells if each property is an addition, removal, or addition to the API.
* From this, we can determine if we've made a breaking change or introduced an API we meant to be private.
* We can high level some of this information in a series of summary messages that are color coded at the tail of the run.
*/
async function compare() {
let branchDir = path.join(__dirname, '..', 'dist', 'branch-api');
let publishedDir = path.join(__dirname, '..', 'dist', 'published-api');
if (!(fs.existsSync(branchDir) && fs.existsSync(publishedDir))) {
console.log(chalk.redBright(`you must have both a branchDir ${branchDir} and publishedDir ${publishedDir}`));
return;
}
let summaryMessages = [];
let branchAPIs = fg.sync(`${branchDir}/**/api.json`);
let publishedAPIs = fg.sync(`${publishedDir}/**/api.json`);
let pairs = [];
// we only care about changes to already published APIs, so find all matching pairs based on what's been published
for (let pubApi of publishedAPIs) {
let pubApiPath = pubApi.split(path.sep);
let sharedPath = path.join(...pubApiPath.slice(pubApiPath.length - 4));
let matchingBranchFile;
for (let branchApi of branchAPIs) {
if (branchApi.includes(sharedPath)) {
matchingBranchFile = branchApi;
pairs.push({pubApi, branchApi});
break;
}
}
if (!matchingBranchFile) {
summaryMessages.push({msg: `removed module ${pubApi}`, severity: 'error'});
}
}
let privatePackages = [];
// don't care about not private APIs, but we do care if we're about to publish a new one
for (let branchApi of branchAPIs) {
let branchApiPath = branchApi.split(path.sep);
let sharedPath = path.join(...branchApiPath.slice(branchApiPath.length - 4));
let matchingPubFile;
for (let pubApi of publishedAPIs) {
if (pubApi.includes(sharedPath)) {
matchingPubFile = pubApi;
// don't re-add to pairs
break;
}
}
if (!matchingPubFile) {
let json = JSON.parse(fs.readFileSync(path.join(branchApi, '..', '..', 'package.json')), 'utf8');
if (!json.private) {
summaryMessages.push({msg: `added module ${branchApi}`, severity: 'warn'});
} else {
privatePackages.push(branchApi);
}
}
}
let count = 0;
let diffs = {};
for (let pair of pairs) {
let diff = getDiff(summaryMessages, pair);
if (diff.diff.length > 0) {
count += 1;
diffs[diff.name] = diff.diff;
}
}
let modulesAdded = branchAPIs.length - privatePackages.length - publishedAPIs.length;
if (modulesAdded !== 0) {
summaryMessages.push({msg: `${Math.abs(modulesAdded)} modules ${modulesAdded > 0 ? 'added' : 'removed'}`, severity: modulesAdded > 0 ? 'warn' : 'error'});
} else {
summaryMessages.push({msg: 'no modules removed or added', severity: 'info'});
}
if (count !== 0) {
summaryMessages.push({msg: `${count} modules had changes to their API ${Object.keys(diffs).map(key => `\n - ${simplifyModuleName(key)}`)}`, severity: 'warn'});
} else {
summaryMessages.push({msg: 'no modules changed their API', severity: 'info'});
}
summaryMessages.push({});
let matches = analyzeDiffs(diffs);
let moreMessages = generateMessages(matches);
[...summaryMessages, ...moreMessages].forEach(({msg, severity}) => {
if (!msg) {
console.log('');
return;
}
let color = 'default';
switch (severity) {
case 'info':
color = 'greenBright';
break;
case 'log':
color = 'blueBright';
break;
case 'warn':
color = 'yellowBright';
break;
case 'error':
color = 'redBright';
break;
default:
color = 'defaultBright';
break;
}
console[severity](chalk[color](msg));
});
}
function getDiff(summaryMessages, pair) {
let name = pair.branchApi.replace(/.*branch-api/, '');
// console.log(`diffing ${name}`);
let publishedApi = fs.readJsonSync(pair.pubApi);
delete publishedApi.links;
walkObject(publishedApi, ({value, location, isLeaf}) => {
if (!isLeaf && value.id && typeof value.id === 'string') {
value.id = value.id.replace(/.*(node_modules|packages)/, '');
}
});
let branchApi = fs.readJsonSync(pair.branchApi);
delete branchApi.links;
walkObject(branchApi, ({value, location, isLeaf}) => {
if (!isLeaf && value.id && typeof value.id === 'string') {
value.id = value.id.replace(/.*(node_modules|packages)/, '');
}
});
let diff = changesets.diff(publishedApi, branchApi);
if (diff.length > 0 && argv.verbose) {
console.log(`diff found in ${name}`);
// for now print the whole diff
console.log(util.inspect(diff, {depth: null}));
}
let publishedExports = publishedApi.exports;
let branchExports = branchApi.exports;
let addedExports = Object.keys(branchExports).filter(key => !publishedExports[key]);
let removedExports = Object.keys(publishedExports).filter(key => !branchExports[key]);
if (addedExports.length > 0) {
summaryMessages.push({msg: `added exports ${addedExports} to ${pair.branchApi}`, severity: 'warn'});
}
if (removedExports.length > 0) {
summaryMessages.push({msg: `removed exports ${removedExports} from ${pair.branchApi}`, severity: 'error'});
}
return {diff, name};
}
function analyzeDiffs(diffs) {
let matches = new Map();
let used = new Map();
for (let [key, value] of Object.entries(diffs)) {
walkChanges(value, {
UPDATE: (change, path) => {
if (used.has(change) || !(change.key === 'type' && (change.value === 'link' || change.oldValue === 'link'))) {
return;
}
matches.set(change, [`${key}:${path}`]);
used.set(change, true);
for (let [name, diff] of Object.entries(diffs)) {
walkChanges(diff, {
UPDATE: (addChange, addPath) => {
let subDiff = changesets.diff(addChange, change);
if (subDiff.length === 0) {
// guaranteed to have the match because we added it before doing this walk
let match = matches.get(change);
if (name !== key && !used.has(addChange)) {
match.push(`${name}:${addPath}`);
used.set(addChange, true);
}
}
}
});
}
},
ADD: (change, path) => {
if (used.has(change)) {
return;
}
matches.set(change, [`${key}:${path}`]);
used.set(change, true);
for (let [name, diff] of Object.entries(diffs)) {
walkChanges(diff, {
ADD: (addChange, addPath) => {
let subDiff = changesets.diff(addChange, change);
if (subDiff.length === 0) {
// guaranteed to have the match because we added it before doing this walk
let match = matches.get(change);
if (name !== key && !used.has(addChange)) {
match.push(`${name}:${addPath}`);
used.set(addChange, true);
}
}
}
});
}
},
REMOVE: (change, path) => {
if (used.has(change)) {
return;
}
matches.set(change, [`${key}:${path}`]);
used.set(change, true);
for (let [name, diff] of Object.entries(diffs)) {
walkChanges(diff, {
REMOVE: (addChange, addPath) => {
let subDiff = changesets.diff(addChange, change);
if (subDiff.length === 0) {
// guaranteed to have the match because we added it before doing this walk
let match = matches.get(change);
if (name !== key && !used.has(addChange)) {
match.push(`${name}:${addPath}`);
used.set(addChange, true);
}
}
}
});
}
}
});
}
return matches;
}
// Recursively walks a json object and calls a processing function on each node based on its type ["ADD", "REMOVE", "UPDATE"]
// tracks the path it's taken through the json object and passes that to the processing function
function walkChanges(changes, process, path = '') {
for (let change of changes) {
if (process[change.type]) {
process[change.type](change, path);
}
if (change.changes && change.changes.length >= 0) {
walkChanges(change.changes, process, `${path}${path.length > 0 ? `.${change.key}` : change.key}`);
}
}
}
function generateMessages(matches) {
let summaryMessages = [];
if (argv.organizedBy === 'change') {
for (let [key, value] of matches) {
/** matches
* {"identifier UPDATED to link": ["/@react-aria/i18n:exports.useDateFormatter.return", "/@react-aria/textfield:exports.useTextField.parameters.1.value.base"]}
*/
let targets = value.map(loc => simplifyModuleName(loc)).map(loc => {
if (!argv.rawNames) {
return `\n - ${loc.split(':')[0]}:${getRealName(loc, loc.split(':')[1])}`;
} else {
return `\n - ${loc}`;
}
});
let severity = 'log';
let message = `${key.key} ${key.type} to:${targets}`;
if (key.type === 'REMOVE') {
message = `${key.key} ${key.type} from:${targets}`;
severity = 'warn';
}
if (key.type === 'UPDATE') {
message = `${key.oldValue} UPDATED to ${key.value}:${targets}`;
}
summaryMessages.push({msg: message, severity});
}
} else {
let invertedMap = new Map();
/** invertedMap
* {"/@react-aria/i18n:exports.useDateFormatter.return": ["identifier UPDATED to link"],
* "exports.useTextField.parameters.1.value.base": ["identifier UPDATED to link"]}
*/
for (let [key, value] of matches) {
for (let loc of value.map(simplifyModuleName)) {
let entry = invertedMap.get(loc);
if (entry) {
entry.push(key);
} else {
invertedMap.set(loc, [key]);
}
}
}
for (let [key, value] of invertedMap) {
let realName = getRealName(key);
let targets = value.map(change => {
let message = '';
switch (change.type) {
case 'REMOVE':
message = chalk.redBright(`${change.key} ${change.type}D`);
break;
case 'UPDATE':
message = `${change.oldValue} UPDATED to ${change.value}`;
break;
default:
message = `${change.key} ${change.type}ED`;
break;
}
return `\n - ${message}`;
});
let severity = 'log';
if (!argv.rawNames) {
summaryMessages.push({msg: `${key.split(':')[0]}:${realName}${targets}`, severity});
} else {
summaryMessages.push({msg: `${key}${targets}`, severity});
}
}
}
return summaryMessages;
}
/**
* Looks up the path through the json object and tries to replace hard to read values with easier to read ones
* @param diffName - /@react-aria/textfield:exports.useTextField.parameters.1.value.base
* @param type - ["ADD", "REMOVE", "UPDATE"]
* @returns {string} - /@react-aria/textfield:exports.useTextField.parameters.ref.value.base
*/
function getRealName(diffName, type = 'ADD') {
let [file, jsonPath] = diffName.split(':');
let filePath = path.join(__dirname, '..', 'dist', type === 'REMOVE' ? 'published-api' : 'branch-api', file, 'dist', 'api.json');
let json = JSON.parse(fs.readFileSync(filePath), 'utf8');
let name = [];
for (let property of jsonPath.split('.')) {
json = json[property];
name.push(json.name ?? property);
}
return name.join('.');
}
function simplifyModuleName(apiJsonPath) {
return apiJsonPath.replace(/\/dist\/.*\.json/, '');
}
function run(cmd, args, opts) {
return new Promise((resolve, reject) => {
let child = spawn(cmd, args, opts);
child.on('error', reject);
child.on('close', code => {
if (code !== 0) {
reject(new Error('Child process failed'));
return;
}
resolve();
});
});
}