-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathvalidate.mjs
More file actions
72 lines (63 loc) · 2.29 KB
/
Copy pathvalidate.mjs
File metadata and controls
72 lines (63 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
import { readFileSync } from "fs";
import { join } from "path";
import { PACKAGES, ROOT, readPackageJson } from "./packages.mjs";
const README_FILES = ["README.md", "README.zh.md"];
export function assertReadmeSync() {
for (const file of README_FILES) {
const rootBuf = readFileSync(join(ROOT, file));
const cliBuf = readFileSync(join(ROOT, "packages/cli", file));
if (!rootBuf.equals(cliBuf)) {
throw new Error(
`${file} differs between root and packages/cli. ` +
`Sync them manually (e.g. \`cp ${file} packages/cli/${file}\`).`,
);
}
}
}
export function loadAndValidatePackages({ packages } = {}) {
const pkgs = packages ?? PACKAGES;
const internalNames = new Set(pkgs.map((p) => p.name));
const jsonByKey = new Map();
for (const pkg of pkgs) {
const json = readPackageJson(pkg);
if (json.name !== pkg.name) {
throw new Error(`${pkg.dir} name must be ${pkg.name}, got ${json.name}`);
}
jsonByKey.set(pkg.key, json);
}
const coreJson = jsonByKey.get("core");
const cliJson = jsonByKey.get("cli");
const version = coreJson.version;
for (const pkg of pkgs) {
const json = jsonByKey.get(pkg.key);
if (json.version !== version) {
throw new Error(
`all package versions must match ${version} (bailian-cli-core), ` +
`but ${pkg.name} is ${json.version}.`,
);
}
for (const [dep, range] of Object.entries(json.dependencies ?? {})) {
if (internalNames.has(dep) && range !== "workspace:*") {
throw new Error(`${pkg.name} dependency on ${dep} must be "workspace:*", got ${range}.`);
}
}
}
return { coreJson, cliJson };
}
const RESERVED_CHANNELS = new Set(["latest", "beta", "alpha", "next", "rc", "canary", "dev"]);
const CHANNEL_FORMAT = /^[a-z][a-z0-9-]{1,30}$/;
export function assertChannel(channel) {
if (!channel || typeof channel !== "string") {
throw new Error("channel is required");
}
if (!CHANNEL_FORMAT.test(channel)) {
throw new Error(
`channel "${channel}" must match ${CHANNEL_FORMAT} (lowercase letters/digits/dashes, start with a letter, 2-31 chars).`,
);
}
if (RESERVED_CHANNELS.has(channel)) {
throw new Error(
`channel "${channel}" is reserved (${[...RESERVED_CHANNELS].join(", ")}); pick a different name.`,
);
}
}