Repository navigation
Expand file tree
/
Copy pathcheck-angular-ts-version.mjs
More file actions
83 lines (71 loc) · 2.45 KB
/
Copy pathcheck-angular-ts-version.mjs
File metadata and controls
83 lines (71 loc) · 2.45 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
82
83
import { existsSync, readFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
const packageName = "@angular-wave/angular.ts";
const verifyRegistryLatest = process.argv.includes("--registry-latest");
const manifest = JSON.parse(readFileSync("package.json", "utf8"));
const declaredVersion = manifest.dependencies?.[packageName];
const failures = [];
if (declaredVersion !== "latest") {
failures.push(
`${packageName} must use the exact \"latest\" npm tag; found ${JSON.stringify(declaredVersion)}.`,
);
}
const installedManifestPath = `node_modules/${packageName}/package.json`;
let installedVersion;
if (!existsSync(installedManifestPath)) {
failures.push(`${packageName} is not installed; run npm install.`);
} else {
installedVersion = JSON.parse(
readFileSync(installedManifestPath, "utf8"),
).version;
}
if (
installedVersion &&
existsSync("package-lock.json") &&
!verifyRegistryLatest
) {
const lockfile = JSON.parse(readFileSync("package-lock.json", "utf8"));
const lockedDeclaration =
lockfile.packages?.[""]?.dependencies?.[packageName];
const lockedVersion =
lockfile.packages?.[`node_modules/${packageName}`]?.version;
if (lockedDeclaration !== "latest") {
failures.push(
`package-lock.json must declare ${packageName} as \"latest\"; found ${JSON.stringify(lockedDeclaration)}.`,
);
}
if (lockedVersion !== installedVersion) {
failures.push(
`${packageName} installation (${installedVersion}) does not match package-lock.json (${lockedVersion}); run npm install.`,
);
}
}
if (installedVersion && verifyRegistryLatest) {
let latestVersion;
try {
latestVersion = execFileSync(
"npm",
["view", packageName, "dist-tags.latest", "--json"],
{ encoding: "utf8", timeout: 15_000 },
).trim();
latestVersion = JSON.parse(latestVersion);
} catch {
failures.push(
`Unable to resolve the latest ${packageName} version from npm.`,
);
}
if (latestVersion && installedVersion !== latestVersion) {
failures.push(
`${packageName} ${installedVersion} is installed, but npm latest is ${latestVersion}; run npm install ${packageName}@latest --save.`,
);
}
}
if (failures.length > 0) {
console.error(failures.join("\n"));
process.exit(1);
}
console.log(
verifyRegistryLatest
? `AngularTS registry compatibility target: latest (${installedVersion}).`
: `AngularTS dependency policy passed: latest (${installedVersion}).`,
);