-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-package-names.js
More file actions
81 lines (69 loc) · 2.29 KB
/
check-package-names.js
File metadata and controls
81 lines (69 loc) · 2.29 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
#!/usr/bin/env node
// This verifies that all package.json files have a name property
// that matches the directory they are in.
// To run this, enter `node check-package-names.js`.
import { readdir, readFile } from "fs/promises";
import { join } from "path";
async function checkPackageJsonNames() {
const errors = [];
const cwd = process.cwd();
try {
// Get top-level directories
const topLevelEntries = await readdir(cwd, { withFileTypes: true });
const topLevelDirs = topLevelEntries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
// Check top-level directories
for (const dir of topLevelDirs) {
const dirPath = join(cwd, dir);
await checkDirectory(dirPath, dir, errors);
// Check one level deeper
try {
const secondLevelEntries = await readdir(dirPath, {
withFileTypes: true,
});
const secondLevelDirs = secondLevelEntries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
for (const subDir of secondLevelDirs) {
const subDirPath = join(dirPath, subDir);
await checkDirectory(subDirPath, subDir, errors);
}
} catch (err) {
// Skip if can't read directory (permissions, etc.)
}
}
// Report results
if (errors.length === 0) {
console.log("✓ All package.json names match their directory names!");
} else {
console.error("✗ Found mismatches:\n");
errors.forEach((error) => {
console.error(` ${error.path}`);
console.error(` Directory name: "${error.dirName}"`);
console.error(` Package name: "${error.packageName}"\n`);
});
process.exit(1);
}
} catch (err) {
console.error("Error:", err.message);
process.exit(1);
}
}
async function checkDirectory(dirPath, dirName, errors) {
const packageJsonPath = join(dirPath, "package.json");
try {
const content = await readFile(packageJsonPath, "utf-8");
const packageJson = JSON.parse(content);
if (packageJson.name !== dirName) {
errors.push({
path: packageJsonPath,
dirName,
packageName: packageJson.name,
});
}
} catch (err) {
// No package.json or invalid JSON - skip silently
}
}
checkPackageJsonNames();