Skip to content

Commit d6f47c0

Browse files
committed
fix publish scripts
1 parent ef55f27 commit d6f47c0

File tree

2 files changed

+181
-187
lines changed

2 files changed

+181
-187
lines changed

scripts/manual-publish.js

Lines changed: 115 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,115 @@
1-
/**
2-
* allow only patch changes from release branches.
3-
* Major and minor changes allowed only from main branch.
4-
* pre-release only from branch containing dev or alpha in the branchname
5-
*/
6-
7-
/** Let the following error be thrown by npm. There are situations where publish could have failed for different reasons. */
8-
// throws an exception if process.env.oldv === process.env.v The library version is not up to date, error(" Not able to release to the same version.
9-
10-
const { execSync } = require("child_process");
11-
const fs = require("fs");
12-
const path = require("path");
13-
14-
const BRANCH = process.env.BRANCH;
15-
const DEFAULT_BRANCH = process.env.DEFAULT_BRANCH;
16-
17-
const isLatestRelease = BRANCH === DEFAULT_BRANCH || BRANCH.includes("release-");
18-
let tag = "";
19-
20-
const { version: OLD_VERSION, name } = require("../lib/package.json");
21-
if (!isLatestRelease) {
22-
/** pre-release branch name should be the tag name (e.g., beta, canery, etc.) or tag name followed by a '-' and version or other specifiers. e.g. beta-2.0 */
23-
tag = BRANCH.split("-")[0];
24-
try {
25-
execSync(`pnpm changeset pre enter ${tag}`);
26-
} catch (e) {
27-
console.log({ e });
28-
}
29-
}
30-
/** Apply changeset */
31-
execSync("pnpm changeset version");
32-
33-
// exit pre mode -- to avoid collision with full releases
34-
try {
35-
execSync("pnpm changeset pre exit");
36-
} catch {
37-
// empty
38-
}
39-
40-
/** not requiring as require is cached by npm/node */
41-
const NEW_VERSION = JSON.parse(
42-
fs.readFileSync(path.resolve(__dirname, "..", "lib", "package.json")),
43-
).version;
44-
45-
const [newMajor, newMinor] = NEW_VERSION.split(".");
46-
const [oldMajor, oldMinor] = OLD_VERSION.split(".");
47-
48-
const isNotPatch = newMajor !== oldMajor || newMinor !== oldMinor;
49-
50-
const pushCmd = `git add . && git commit -m "Apply changesets and update CHANGELOG" && git push origin ${BRANCH}`;
51-
52-
if (isNotPatch && BRANCH === DEFAULT_BRANCH) {
53-
try {
54-
execSync(pushCmd);
55-
} catch (e) {
56-
console.log({ e });
57-
}
58-
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
59-
/** Create new release branch for every Major or Minor release */
60-
const releaseBranch = `release-${newMajor}.${newMinor}`;
61-
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
62-
} else if (isLatestRelease) {
63-
/** New version must be valid SEMVER version. No pre-release (beta/alpha etc.) */
64-
if (!/^\d+\.\d+.\d+$/.test(NEW_VERSION)) throw new Error("Invalid version");
65-
66-
if (isNotPatch)
67-
throw new Error("Major or Minor changes can be published only from the default branch.");
68-
69-
// Push changes back to the repo
70-
try {
71-
execSync(pushCmd);
72-
} catch (e) {
73-
console.log({ e });
74-
}
75-
} else {
76-
try {
77-
execSync(pushCmd);
78-
} catch (e) {
79-
console.log({ e });
80-
}
81-
}
82-
83-
const { visibility } = JSON.parse(execSync("gh repo view --json visibility").toString());
84-
const provenance = visibility.toLowerCase() === "public" ? "--provenance" : "";
85-
86-
let LATEST_VERSION = "0.0.-1";
87-
88-
try {
89-
LATEST_VERSION = execSync(`npm view ${name} version`).toString().trim() ?? "0.0.-1";
90-
} catch {
91-
// empty
92-
}
93-
94-
const latest = LATEST_VERSION.split(".").map(parseInt);
95-
const current = NEW_VERSION.split(".").map(parseInt);
96-
97-
let isLatest = false;
98-
99-
if (latest[0] < current[0]) {
100-
isLatest = true;
101-
} else if (latest[0] === current[0] && latest[1] < current[1]) {
102-
isLatest = true;
103-
} else if (latest[0] === current[0] && latest[1] === current[1] && latest[2] < current[2]) {
104-
isLatest = true;
105-
}
106-
107-
const reTag = isLatest ? "" : ` && npm dist-tag add ${name}@${LATEST_VERSION} latest`;
108-
/** Create release */
109-
const publishCmd = `cd lib && pnpm build && npm publish ${provenance} --access public${tag && ` --tag ${tag}`}`;
110-
execSync(publishCmd + reTag);
111-
112-
/** Create GitHub release */
113-
execSync(
114-
`gh release create ${NEW_VERSION} --generate-notes${isLatestRelease ? " --latest" : ""} -n "$(sed '1,/^## /d;/^## /,$d' CHANGELOG.md)" --title "Release v${NEW_VERSION}"`,
115-
);
116-
117-
execSync("node ./scripts/lite.js");
118-
execSync(publishCmd + reTag.replace("@", "-lite@"));
119-
1+
/**
2+
* allow only patch changes from release branches.
3+
* Major and minor changes allowed only from main branch.
4+
* pre-release only from branch containing dev or alpha in the branchname
5+
*/
6+
7+
/** Let the following error be thrown by npm. There are situations where publish could have failed for different reasons. */
8+
// throws an exception if process.env.oldv === process.env.v The library version is not up to date, error(" Not able to release to the same version.
9+
10+
const { execSync } = require("child_process");
11+
const fs = require("fs");
12+
const path = require("path");
13+
14+
const BRANCH = process.env.BRANCH;
15+
const DEFAULT_BRANCH = process.env.DEFAULT_BRANCH;
16+
17+
const isLatestRelease = BRANCH === DEFAULT_BRANCH || BRANCH.includes("release-");
18+
let tag = "";
19+
20+
const { version: OLD_VERSION, name } = require("../lib/package.json");
21+
if (!isLatestRelease) {
22+
/** pre-release branch name should be the tag name (e.g., beta, canery, etc.) or tag name followed by a '-' and version or other specifiers. e.g. beta-2.0 */
23+
tag = BRANCH.split("-")[0];
24+
try {
25+
execSync(`pnpm changeset pre enter ${tag}`);
26+
} catch (e) {
27+
console.log({ e });
28+
}
29+
}
30+
/** Apply changeset */
31+
execSync("pnpm changeset version");
32+
33+
// exit pre mode -- to avoid collision with full releases
34+
try {
35+
execSync("pnpm changeset pre exit");
36+
} catch {
37+
// empty
38+
}
39+
40+
/** not requiring as require is cached by npm/node */
41+
const NEW_VERSION = JSON.parse(
42+
fs.readFileSync(path.resolve(__dirname, "..", "lib", "package.json")),
43+
).version;
44+
45+
const [newMajor, newMinor] = NEW_VERSION.split(".");
46+
const [oldMajor, oldMinor] = OLD_VERSION.split(".");
47+
48+
const isNotPatch = newMajor !== oldMajor || newMinor !== oldMinor;
49+
50+
const pushCmd = `git add . && git commit -m "Apply changesets and update CHANGELOG" && git push origin ${BRANCH}`;
51+
52+
if (isNotPatch && BRANCH === DEFAULT_BRANCH) {
53+
try {
54+
execSync(pushCmd);
55+
} catch (e) {
56+
console.log({ e });
57+
}
58+
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
59+
/** Create new release branch for every Major or Minor release */
60+
const releaseBranch = `release-${newMajor}.${newMinor}`;
61+
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
62+
} else if (isLatestRelease) {
63+
/** New version must be valid SEMVER version. No pre-release (beta/alpha etc.) */
64+
if (!/^\d+\.\d+.\d+$/.test(NEW_VERSION)) throw new Error("Invalid version");
65+
66+
if (isNotPatch)
67+
throw new Error("Major or Minor changes can be published only from the default branch.");
68+
69+
// Push changes back to the repo
70+
try {
71+
execSync(pushCmd);
72+
} catch (e) {
73+
console.log({ e });
74+
}
75+
} else {
76+
try {
77+
execSync(pushCmd);
78+
} catch (e) {
79+
console.log({ e });
80+
}
81+
}
82+
83+
const { visibility } = JSON.parse(execSync("gh repo view --json visibility").toString());
84+
const provenance = visibility.toLowerCase() === "public" ? "--provenance" : "";
85+
86+
let LATEST_VERSION = "0.0.-1";
87+
88+
try {
89+
LATEST_VERSION = execSync(`npm view ${name} version`).toString().trim() ?? "0.0.-1";
90+
} catch {
91+
// empty
92+
}
93+
94+
const latest = LATEST_VERSION.split(".").map(parseInt);
95+
const current = NEW_VERSION.split(".").map(parseInt);
96+
97+
let isLatest = false;
98+
99+
if (latest[0] < current[0]) {
100+
isLatest = true;
101+
} else if (latest[0] === current[0] && latest[1] < current[1]) {
102+
isLatest = true;
103+
} else if (latest[0] === current[0] && latest[1] === current[1] && latest[2] < current[2]) {
104+
isLatest = true;
105+
}
106+
107+
const reTag = isLatest ? "" : ` && npm dist-tag add ${name}@${LATEST_VERSION} latest`;
108+
/** Create release */
109+
const publishCmd = `cd lib && pnpm build && npm publish ${provenance} --access public${tag && ` --tag ${tag}`}`;
110+
execSync(publishCmd + reTag);
111+
112+
/** Create GitHub release */
113+
execSync(
114+
`gh release create ${NEW_VERSION} --generate-notes${isLatestRelease ? " --latest" : ""} -n "$(sed '1,/^## /d;/^## /,$d' CHANGELOG.md)" --title "Release v${NEW_VERSION}"`,
115+
);

scripts/publish.js

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,66 @@
1-
/** It is assumed that this is called only from the default branch. */
2-
const { execSync } = require("child_process");
3-
4-
const BRANCH = process.env.BRANCH;
5-
6-
// Apply changesets if any -- e.g., coming from pre-release branches
7-
try {
8-
execSync("pnpm changeset pre exit");
9-
} catch {
10-
// empty
11-
}
12-
try {
13-
execSync("pnpm changeset version");
14-
execSync(
15-
`git add . && git commit -m "Apply changesets and update CHANGELOG [skip ci]" && git push origin ${BRANCH}`,
16-
);
17-
} catch {
18-
// no changesets to be applied
19-
}
20-
21-
const { version: VERSION, name } = require("../lib/package.json");
22-
let LATEST_VERSION = "0.0.-1";
23-
24-
try {
25-
LATEST_VERSION = execSync(`npm view ${name} version`).toString().trim() ?? "0.0.-1";
26-
} catch {
27-
// empty
28-
}
29-
30-
console.log({ VERSION, LATEST_VERSION });
31-
32-
const [newMajor, newMinor] = VERSION.split(".");
33-
const [oldMajor, oldMinor] = LATEST_VERSION.split(".");
34-
35-
const isPatch = newMajor === oldMajor && newMinor === oldMinor;
36-
const releaseBranch = `release-${newMajor}.${newMinor}`;
37-
38-
if (isPatch) {
39-
// update release branch
40-
try {
41-
execSync(
42-
`git checkout ${releaseBranch} && git merge ${BRANCH} && git push origin ${releaseBranch}`,
43-
);
44-
} catch {}
45-
} else {
46-
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
47-
/** Create new release branch for every Major or Minor release */
48-
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
49-
}
50-
51-
const { visibility } = JSON.parse(execSync("gh repo view --json visibility").toString());
52-
const provenance = visibility.toLowerCase() === "public" ? "--provenance" : "";
53-
54-
/** Create release */
55-
execSync(`cd lib && pnpm build && npm publish ${provenance} --access public`);
56-
57-
/** Create GitHub release */
58-
try {
59-
execSync(
60-
`gh release create ${VERSION} --generate-notes --latest -n "$(sed '1,/^## /d;/^## /,$d' CHANGELOG.md)" --title "Release v${VERSION}"`,
61-
);
62-
} catch {
63-
execSync(`gh release create ${VERSION} --generate-notes --latest --title "Release v${VERSION}"`);
64-
}
65-
66-
execSync("node ./scripts/lite.js");
67-
execSync(`cd lib && pnpm build && npm publish ${provenance} --access public`);
68-
1+
/** It is assumed that this is called only from the default branch. */
2+
const { execSync } = require("child_process");
3+
4+
const BRANCH = process.env.BRANCH;
5+
6+
// Apply changesets if any -- e.g., coming from pre-release branches
7+
try {
8+
execSync("pnpm changeset pre exit");
9+
} catch {
10+
// empty
11+
}
12+
try {
13+
execSync("pnpm changeset version");
14+
execSync(
15+
`git add . && git commit -m "Apply changesets and update CHANGELOG [skip ci]" && git push origin ${BRANCH}`,
16+
);
17+
} catch {
18+
// no changesets to be applied
19+
}
20+
21+
const { version: VERSION, name } = require("../lib/package.json");
22+
let LATEST_VERSION = "0.0.-1";
23+
24+
try {
25+
LATEST_VERSION = execSync(`npm view ${name} version`).toString().trim() ?? "0.0.-1";
26+
} catch {
27+
// empty
28+
}
29+
30+
console.log({ VERSION, LATEST_VERSION });
31+
32+
const [newMajor, newMinor] = VERSION.split(".");
33+
const [oldMajor, oldMinor] = LATEST_VERSION.split(".");
34+
35+
const isPatch = newMajor === oldMajor && newMinor === oldMinor;
36+
const releaseBranch = `release-${newMajor}.${newMinor}`;
37+
38+
if (isPatch) {
39+
// update release branch
40+
try {
41+
execSync(
42+
`git checkout ${releaseBranch} && git merge ${BRANCH} && git push origin ${releaseBranch}`,
43+
);
44+
} catch {}
45+
} else {
46+
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
47+
/** Create new release branch for every Major or Minor release */
48+
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
49+
}
50+
51+
const { visibility } = JSON.parse(execSync("gh repo view --json visibility").toString());
52+
const provenance = visibility.toLowerCase() === "public" ? "--provenance" : "";
53+
54+
/** Create release */
55+
execSync(`cd lib && pnpm build && npm publish ${provenance} --access public`);
56+
57+
/** Create GitHub release */
58+
try {
59+
execSync(
60+
`gh release create ${VERSION} --generate-notes --latest -n "$(sed '1,/^## /d;/^## /,$d' CHANGELOG.md)" --title "Release v${VERSION}"`,
61+
);
62+
} catch {
63+
execSync(`gh release create ${VERSION} --generate-notes --latest --title "Release v${VERSION}"`);
64+
}
65+
66+
execSync(`cd lib && pnpm build && npm publish ${provenance} --access public`);

0 commit comments

Comments
 (0)