Skip to content

Commit 71a9aca

Browse files
committed
fix: lint
1 parent c7f3be0 commit 71a9aca

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

packages/isaacscript-common-node/src/functions/packageJSON.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export function getPackageJSON(
3636
}
3737

3838
/**
39-
* Helper function to get the "dependencies" or "devDependencies" field from a "package.json" file.
40-
* If the corresponding field does not exist, `undefined` will be returned.
39+
* Helper function to get the "dependencies" or "devDependencies" or "peerDependencies" field from a
40+
* "package.json" file. If the corresponding field does not exist, `undefined` will be returned.
4141
*
4242
* This will print an error message and exit the program if the "package.json" file cannot be found
4343
* or is otherwise invalid.
@@ -52,7 +52,7 @@ export function getPackageJSON(
5252
export function getPackageJSONDependencies(
5353
filePathOrDirPathOrRecord: string | Record<string, unknown> | undefined,
5454
dependencyFieldName: PackageJSONDependencyFieldName = "dependencies",
55-
): Record<string, unknown> | undefined {
55+
): Record<string, string> | undefined {
5656
const packageJSON =
5757
typeof filePathOrDirPathOrRecord === "object"
5858
? filePathOrDirPathOrRecord
@@ -81,7 +81,19 @@ export function getPackageJSONDependencies(
8181
);
8282
}
8383

84-
return field;
84+
for (const [key, value] of Object.entries(field)) {
85+
if (typeof value !== "string") {
86+
fatalError(
87+
`Failed to parse the "${chalk.green(
88+
dependencyFieldName,
89+
)}" field in a "${chalk.green(
90+
PACKAGE_JSON,
91+
)}" file since the "${chalk.green(key)}" entry was not a string.`,
92+
);
93+
}
94+
}
95+
96+
return field as Record<string, string>;
8597
}
8698

8799
/**

scripts/packageJSONLint.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
echo,
99
exit,
1010
getPackageJSON,
11+
getPackageJSONDependencies,
1112
readFile,
1213
} from "isaacscript-common-node";
1314
import { isKebabCase } from "isaacscript-common-ts";
@@ -229,12 +230,6 @@ function packageJSONLint(
229230
}
230231
}
231232

232-
const { files } = packageJSON;
233-
if (files !== undefined) {
234-
echo(`File has a "files" field: ${packageJSONPath}`);
235-
return false;
236-
}
237-
238233
if (!isTemplateFile && rootDeps !== undefined) {
239234
const { dependencies } = packageJSON;
240235
if (!checkDeps(dependencies, rootDeps, packageJSONPath)) {
@@ -255,24 +250,17 @@ function packageJSONLint(
255250

256251
/** Gets the dependencies of the root monorepo "package.json" file. */
257252
function getDeps(packageJSONPath: string): Record<string, string> {
258-
const packageJSONString = readFile(packageJSONPath);
259-
const packageJSON = getPackageJSON(packageJSONString);
260-
261-
let { dependencies, devDependencies, peerDependencies } = packageJSON;
262-
if (typeof dependencies !== "object") {
263-
dependencies = {};
264-
}
265-
if (typeof devDependencies !== "object") {
266-
devDependencies = {};
267-
}
268-
if (typeof peerDependencies !== "object") {
269-
peerDependencies = {};
270-
}
253+
const dependencies =
254+
getPackageJSONDependencies(packageJSONPath, "dependencies") ?? {};
255+
const devDependencies =
256+
getPackageJSONDependencies(packageJSONPath, "devDependencies") ?? {};
257+
const peerDependencies =
258+
getPackageJSONDependencies(packageJSONPath, "peerDependencies") ?? {};
271259

272260
const deps = {
273-
...(dependencies as Record<string, string>),
274-
...(devDependencies as Record<string, string>),
275-
...(peerDependencies as Record<string, string>),
261+
...dependencies,
262+
...devDependencies,
263+
...peerDependencies,
276264
};
277265

278266
// `eslint-plugin-isaacscript` is not a root dependency of the monorepo, so it has to be
@@ -292,8 +280,7 @@ function getVersionForSpecificPackage(packageName: string): string {
292280
packageName,
293281
"package.json",
294282
);
295-
const packageJSONString = readFile(packageJSONPath);
296-
const packageJSON = getPackageJSON(packageJSONString);
283+
const packageJSON = getPackageJSON(packageJSONPath);
297284
const packageVersion = packageJSON["version"];
298285
if (typeof packageVersion !== "string") {
299286
throw new TypeError(`Failed to parse the version from: ${packageJSONPath}`);
@@ -325,8 +312,7 @@ function checkDeps(
325312
key,
326313
PACKAGE_JSON,
327314
);
328-
const depPackageJSONString = readFile(depPackageJSONPath);
329-
const depPackageJSON = getPackageJSON(depPackageJSONString);
315+
const depPackageJSON = getPackageJSON(depPackageJSONPath);
330316
const depVersion = depPackageJSON["version"];
331317
if (typeof depVersion !== "string") {
332318
throw new TypeError(
@@ -360,8 +346,7 @@ function checkRootDepsUpToDate(
360346
continue;
361347
}
362348

363-
const packageJSONString = readFile(matchingPackageJSONPath);
364-
const packageJSON = getPackageJSON(packageJSONString);
349+
const packageJSON = getPackageJSON(matchingPackageJSONPath);
365350

366351
const { version } = packageJSON;
367352
if (typeof version !== "string" || version === "") {

0 commit comments

Comments
 (0)