-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-checker.mjs
65 lines (57 loc) · 1.61 KB
/
import-checker.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
// @ts-check
import fastGlob from "fast-glob";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import { resolve } from "node:path";
const { glob } = fastGlob;
const CHECK_FIELDS = ["main", "react-native", "types"];
/**
* @type {string[]}
*/
const allowNoTypes = [];
const packageJson = await glob("**/package.json", {
ignore: ["**/node_modules/**", "**/dist/**"],
});
/**
* @type {string[]}
*/
const errors = [];
await Promise.allSettled(
packageJson.map(async (path) => {
const pkg = JSON.parse(await fs.readFile(path, "utf8"));
const directoryPath = resolve(path, "..");
for (const field of CHECK_FIELDS) {
if (field === "types" && allowNoTypes.some((it) => path.includes(it))) {
console.log("skipped types for :", path);
continue;
}
const fieldValue = pkg[field];
if (!fieldValue) {
errors.push(`Missing ${field} in ${path}`);
continue;
}
if (typeof fieldValue !== "string") {
errors.push(
`Invalid ${field} in ${path}; must be a string. But ${fieldValue} is a ${typeof fieldValue}`
);
continue;
}
const isRelative = fieldValue.startsWith(".");
if (!isRelative) {
errors.push(
`Invalid ${field} in ${path}; '${fieldValue}' must be a relative path`
);
continue;
}
if (!existsSync(resolve(directoryPath, fieldValue))) {
errors.push(
`Invalid ${field} in ${path}; '${fieldValue}' does not exist`
);
}
}
})
);
if (errors.length > 0) {
console.error(errors.join("\n"));
process.exit(1);
}