-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.mjs
77 lines (64 loc) · 1.98 KB
/
validate.mjs
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
import { promises as FS } from "node:fs";
import chalk from "chalk";
const pkgJson = await FS.readFile(`./package.json`);
const pkg = JSON.parse(pkgJson.toString());
let _indent = 2;
let _indentBy = 4;
function logWithIndent(msg) {
console.log(chalk.gray(`${new Array(_indent + 1).fill("").join(" ")}${msg}`));
}
async function checkFile(file, description) {
logWithIndent(`${description ? `"${chalk.cyanBright(description)}": ` : ''}${chalk.blue(`"${file}"`)} ${chalk.green(`✓`)} exists`);
await FS.stat(file);
}
function indent() {
_indent += _indentBy;
}
function dedent(arr = false) {
_indent -= _indentBy;
logWithIndent(arr ? ']' : '}');
}
console.log(chalk.gray(`
Validating ${chalk.blue(`package.json`)}
`))
logWithIndent(`{`);
await checkFile(pkg.main, "main");
await checkFile(pkg.module, "module");
await checkFile(pkg.types, "types");
const exportKeys = Object.keys(pkg.exports);
logWithIndent(`"exports": {`);
indent();
for (const exportKey of exportKeys) {
if (typeof pkg.exports[exportKey] === "string") {
await checkFile(pkg.exports[exportKey], exportKey);
continue;
}
logWithIndent(`"${exportKey}": {`);
const variations = Object.keys(pkg.exports[exportKey]);
indent();
for (const variation of variations) {
await checkFile(pkg.exports[exportKey][variation], variation);
}
dedent();
}
dedent();
const typesVersionKeys = Object.keys(pkg.typesVersions);
logWithIndent(`"typesVersions": {`);
indent();
for (const typesVersionKey of typesVersionKeys) {
const variations = Object.keys(pkg.typesVersions[typesVersionKey]);
logWithIndent(`"${typesVersionKey}": {`);
indent();
for (const variation of variations) {
const listOfTypes = pkg.typesVersions[typesVersionKey][variation];
logWithIndent(`"${variation}": [`);
indent();
for (const pathToTypescriptFile of listOfTypes) {
await checkFile(pathToTypescriptFile);
}
dedent(true);
}
dedent();
}
dedent();
console.log(chalk.greenBright(`\n\n ✓ package.json is valid`));