-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathcheck-exports-exist.js
51 lines (44 loc) · 1.3 KB
/
check-exports-exist.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
'use strict';
const path = require('path');
const { promises: fs } = require('fs');
function gatherPathsRec(exports = {}) {
if (typeof exports === 'string') {
return [exports];
}
return [...Object.values(exports)]
.map((subpathOrCondition) => {
return gatherPathsRec(subpathOrCondition);
})
.flat();
}
async function main() {
const packageJson = require(path.resolve(process.cwd(), 'package.json'));
const pathsToCheck = new Set(
[
packageJson.main,
packageJson.module,
packageJson.browser,
packageJson.types,
...gatherPathsRec(packageJson.exports),
...gatherPathsRec(packageJson.bin),
]
.filter(Boolean)
.map((exportPath) => exportPath.replace(/^\.\//, ''))
);
for (const exportPath of pathsToCheck.values()) {
try {
await fs.stat(exportPath);
} catch {
throw new Error(
`Export path "${exportPath}" provided in package.json can't be resolved, this might cause issues for external library users. Either make sure that path exists or remove it from the package.json`
);
}
}
console.log(`All exports in package ${packageJson.name} exist`);
}
process.on('unhandledRejection', (err) => {
console.error();
console.error(err.stack || err.message || err);
process.exitCode = 1;
});
main();