Skip to content

Commit 0a4a6c6

Browse files
authored
Add script to guard against publishing with the wrong dist-tag (#457)
1 parent 09ad350 commit 0a4a6c6

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

.github/RELEASING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
7. After approval, run the following commands to publish to npmjs.com:
2828

2929
```bash
30-
pnpm install && pnpm run all && pnpm --filter "./packages/*" publish
30+
pnpm install && pnpm run all && node scripts/release.mjs
3131
```
3232

3333
8. Merge your PR.

scripts/release.mjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2021-2024 The Connect Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { readdirSync, readFileSync } from "fs";
16+
import { join } from "path";
17+
import { existsSync } from "node:fs";
18+
import { execSync } from "node:child_process";
19+
20+
/*
21+
* Publish connect-query-es. See .github/RELEASING.md
22+
*/
23+
24+
const tag = determinePublishTag(findWorkspaceVersion("packages"));
25+
const command = `pnpm --filter "./packages/*" publish --tag ${tag}`;
26+
execSync(command, {
27+
stdio: "inherit",
28+
});
29+
30+
/**
31+
* @param {string} version
32+
* @returns {string}
33+
*/
34+
function determinePublishTag(version) {
35+
if (/^\d+\.\d+\.\d+$/.test(version)) {
36+
return "latest";
37+
} else if (/^\d+\.\d+\.\d+-alpha.*$/.test(version)) {
38+
return "alpha";
39+
} else if (/^\d+\.\d+\.\d+-beta.*$/.test(version)) {
40+
return "beta";
41+
} else if (/^\d+\.\d+\.\d+-rc.*$/.test(version)) {
42+
return "rc";
43+
} else {
44+
throw new Error(`Unable to determine publish tag from version ${version}`);
45+
}
46+
}
47+
48+
/**
49+
* @param {string} packagesDir
50+
* @returns {string}
51+
*/
52+
function findWorkspaceVersion(packagesDir) {
53+
let version = undefined;
54+
for (const entry of readdirSync(packagesDir, { withFileTypes: true })) {
55+
if (!entry.isDirectory()) {
56+
continue;
57+
}
58+
const path = join(packagesDir, entry.name, "package.json");
59+
if (existsSync(path)) {
60+
const pkg = JSON.parse(readFileSync(path, "utf-8"));
61+
if (pkg.private === true) {
62+
continue;
63+
}
64+
if (!pkg.version) {
65+
throw new Error(`${path} is missing "version"`);
66+
}
67+
if (version === undefined) {
68+
version = pkg.version;
69+
} else if (version !== pkg.version) {
70+
throw new Error(`${path} has unexpected version ${pkg.version}`);
71+
}
72+
}
73+
}
74+
if (version === undefined) {
75+
throw new Error(`unable to find workspace version`);
76+
}
77+
return version;
78+
}

0 commit comments

Comments
 (0)