Skip to content

Commit f91c2fe

Browse files
Handle package-json-validator breaking changes
1 parent 68e6e4d commit f91c2fe

2 files changed

Lines changed: 17 additions & 37 deletions

File tree

src/main.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import semver from "semver";
22
import { intersect as semverIntersect } from "semver-intersect";
3-
import { PJV, INpmValidateResult } from "package-json-validator";
3+
import { validate as packageJsonValidator } from "package-json-validator";
44
import fs from "fs";
55
import path from "path";
66
import chalk from "chalk";
@@ -14,8 +14,6 @@ export {
1414
InvalidBowerPackageException,
1515
} from "./exceptions.js";
1616

17-
const packageJsonValidator = PJV.validate;
18-
1917
// Known dependency keys
2018
const npmDependencyTypes = [
2119
"dependencies",
@@ -61,6 +59,14 @@ interface ITemplatePath {
6159
path: string;
6260
}
6361

62+
interface INpmValidateResult {
63+
valid: boolean;
64+
errors?: { field: string; message: string }[];
65+
critical?: Record<string, string> | string;
66+
warnings?: string[];
67+
recommendations?: string[];
68+
}
69+
6470
/**
6571
* @public
6672
*/
@@ -296,7 +302,10 @@ function npmValidate(pkg: INodeTemplate, pkgSpec: string, log: LogOption): void
296302
if (pkg[depType] && typeof pkg[depType] !== 'object') {
297303
if (!results.errors) results.errors = [];
298304
results.valid = false;
299-
results.errors.push(`"${depType}" must be an object.`);
305+
results.errors.push({
306+
field: depType,
307+
message: "must be an object."
308+
});
300309
}
301310
}
302311
}
@@ -328,14 +337,16 @@ function npmErrors(results: INpmValidateResult, log: LogOption): void {
328337
if (log) {
329338
// Announce critical errors.
330339
if (results.critical) {
331-
log(chalk.bgRed("Critical Error: ") + chalk.red(results.critical));
340+
const criticalError = typeof results.critical === "string" ? results.critical : JSON.stringify(results.critical);
341+
log(chalk.bgRed("Critical Error: ") + chalk.red(criticalError));
332342
}
333343

334344
// Announce errors.
335345
if (results.errors) {
336346
log(chalk.underline.red("Errors:"));
337347
for (let error of results.errors) {
338-
log(" " + chalk.red(error));
348+
const errorMessage = `${error.field}: ${error.message}`;
349+
log(" " + chalk.red(errorMessage));
339350
}
340351
}
341352

src/missing-types.d.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
11
declare module "semver-intersect" {
22
export function intersect(...versions: string[]): string;
33
}
4-
5-
declare module "package-json-validator" {
6-
export interface INpmValidateResult {
7-
valid: boolean;
8-
errors?: string[];
9-
critical?: string;
10-
warnings?: string[];
11-
recommendations?: string[]
12-
}
13-
type PackageSpecs = "npm" | "commonjs_1.0" | "commonjs_1.1";
14-
type PJV = {
15-
packageFormat: string,
16-
versionFormat: string,
17-
urlFormat: string,
18-
emailFormat: string,
19-
getSpecMap(specName: PackageSpecs): false | {},
20-
parse(data: string): string;
21-
validate(
22-
data: string,
23-
specName?: PackageSpecs,
24-
options?: {}
25-
): INpmValidateResult,
26-
validateType(name: string, field: {}, value: any): string[],
27-
validateDependencies(name: string, deps: {}): string[],
28-
isValidVersionRange(v: string): boolean,
29-
validateUrlOrMailto(name: string, obj: {}): string[],
30-
validatePeople(name: string, obj: {}): string[],
31-
validateUrlTypes(name: string, obj: {}): string[],
32-
}
33-
export const PJV: PJV;
34-
}

0 commit comments

Comments
 (0)