-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
64 lines (55 loc) · 1.53 KB
/
test.js
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
import { env } from 'process';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
export const compareNamedExports = (packageName, requiredPackage, importedPackage) => {
const transpiled = requiredPackage.__esModule || requiredPackage['default']?.__esModule; // Truthy, not just present
const getNames = (obj) => {
const names = new Set();
while (obj) {
Object.keys(obj).forEach(name => {
if (name !== 'default' && name !== '__esModule') {
try {
// Some exported names throw on access, e.g. if deprecated
names.add(name);
} catch {}
}
});
obj = Object.getPrototypeOf(obj);
}
return names;
}
const requiredNames = getNames(requiredPackage);
const importedNames = getNames(importedPackage);
const detectedNames = [];
const missingNames = [];
requiredNames.forEach(name => {
if (importedNames.has(name)) {
detectedNames.push(name);
} else {
missingNames.push(name);
}
});
console.log(JSON.stringify({
name: packageName,
transpiled,
expectedNames: [...requiredNames],
detectedNames,
missingNames
}));
};
import * as importedPackage from 'REPLACE_WITH_PACKAGE_NAME';
// Main entry point
(async () => {
try {
require.cache = Object.create(null); // Clear CommonJS cache
const requiredPackage = require('REPLACE_WITH_PACKAGE_NAME');
return compareNamedExports('REPLACE_WITH_PACKAGE_NAME', requiredPackage, importedPackage);
} catch {
return {
pass: false,
expectedNames: [],
detectedNames: [],
missingNames: []
}
}
})();