Repository navigation
Expand file tree
/
Copy pathnpm_dist_tag.mjs
More file actions
63 lines (57 loc) · 2.39 KB
/
Copy pathnpm_dist_tag.mjs
File metadata and controls
63 lines (57 loc) · 2.39 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
// Resolve the npm dist-tag a release is published under.
//
// Usage: node scripts/npm_dist_tag.mjs <requested> (prints the tag, or exits 1)
//
// `auto` publishes a stable version under `latest`, and a prerelease under `latest` only
// while no stable version exists on the registry; after that a prerelease goes to `beta`.
// npm's trusted publishing can set a tag only at publish time, so a prerelease that took
// `latest` from a stable release could not be moved back from the workflow.
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { repoRoot } from "./sync_versions.mjs";
const REGISTRY_PACKAGE = "@visionset/ui-core";
const TAG_NAME = /^[a-z][a-z0-9-]*$/;
const isPrerelease = (version) => version.includes("-");
/**
* @param {{ version: string, requested: string, published: readonly string[] }} input
* @returns {string}
*/
export function resolveDistTag({ version, requested, published }) {
const stableExists = published.some((v) => !isPrerelease(v));
if (requested === "auto") {
return isPrerelease(version) && stableExists ? "beta" : "latest";
}
if (!TAG_NAME.test(requested)) {
throw new Error(`"${requested}" is not a dist-tag name (lowercase letters, digits and hyphens)`);
}
if (requested === "latest" && isPrerelease(version) && stableExists) {
throw new Error(`refusing to put prerelease ${version} on latest while a stable version is published`);
}
return requested;
}
function publishedVersions() {
try {
const out = execFileSync("pnpm", ["view", REGISTRY_PACKAGE, "versions", "--json"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
const parsed = JSON.parse(out);
return Array.isArray(parsed) ? parsed : [parsed];
} catch (error) {
if (/FETCH_404|E404/.test(String(error.stdout ?? "") + String(error.stderr ?? ""))) return [];
throw error;
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const requested = process.argv[2] ?? "auto";
const pkg = path.join(repoRoot(), "frontend", "ui-core", "package.json");
const { version } = JSON.parse(readFileSync(pkg, "utf8"));
try {
console.log(resolveDistTag({ version, requested, published: publishedVersions() }));
} catch (error) {
console.error(`error: ${error.message}`);
process.exit(1);
}
}