Skip to content

Commit b483747

Browse files
committed
add publish script
1 parent 23191e1 commit b483747

File tree

2 files changed

+104
-121
lines changed

2 files changed

+104
-121
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ jobs:
2929
- name: Test
3030
run: yarn test
3131

32-
# - name: Publish
33-
# env:
34-
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
35-
# run: |
36-
# echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
37-
# node scripts/publish.js
32+
- name: Publish
33+
env:
34+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
35+
run: |
36+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
37+
node scripts/publish.js

scripts/publish.js

Lines changed: 98 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,98 @@
1-
// const path = require("path");
2-
// const jsonfile = require("jsonfile");
3-
// const semver = require("semver");
4-
// const execSync = require("child_process").execSync;
5-
// const rootDir = path.resolve(__dirname, "..");
6-
7-
// const args = process.argv.slice(2);
8-
9-
// /**
10-
// * @param {*} cond
11-
// * @param {string} message
12-
// * @returns {asserts cond}
13-
// */
14-
// function invariant(cond, message) {
15-
// if (!cond) throw new Error(message);
16-
// }
17-
18-
// /**
19-
// * @returns {string}
20-
// */
21-
// function getTaggedVersion() {
22-
// let output = execSync("git tag --list --points-at HEAD").toString();
23-
// return output.replace(/^v|\n+$/g, "");
24-
// }
25-
26-
// /**
27-
// *
28-
// * @param {SemverRelease} [semverRelease]
29-
// */
30-
// function bumpVersion(semverRelease = "patch") {
31-
// // lerna version --no-push --exact
32-
// execSync(`yarn lerna version ${semverRelease} --no-push --exact --yes`);
33-
// }
34-
35-
// /**
36-
// * @param {string} packageName
37-
// * @param {string|number} version
38-
// */
39-
// async function ensureBuildVersion(packageName, version) {
40-
// let file = path.join(rootDir, "packages", packageName, "package.json");
41-
// let json = await jsonfile.readFile(file);
42-
// invariant(
43-
// json.version === version,
44-
// `Package ${packageName} is on version ${json.version}, but should be on ${version}`
45-
// );
46-
// }
47-
48-
// /**
49-
// * @param {string} packageName
50-
// * @param {string} tag
51-
// */
52-
// function publishBuild(packageName, tag) {
53-
// let buildDir = path.join(rootDir, "packages", packageName);
54-
// console.log();
55-
// console.log(` npm publish ${buildDir} --tag ${tag}`);
56-
// console.log();
57-
// execSync(`npm publish ${buildDir} --tag ${tag}`, { stdio: "inherit" });
58-
// }
59-
60-
// /**
61-
// * @param {SemverRelease|null} [semverRelease]
62-
// * @returns {Promise<1 | 0>}
63-
// */
64-
// async function run(semverRelease) {
65-
// try {
66-
// // 0. Ensure we are in CI. We don't do this manually
67-
// invariant(
68-
// process.env.CI,
69-
// `You should always run the publish script from the CI environment!`
70-
// );
71-
72-
// // 1. Update the release version with a git tag + commit
73-
// bumpVersion(semverRelease);
74-
75-
// // 2. Get the current tag, which has the release version number
76-
// let version = getTaggedVersion();
77-
// invariant(
78-
// version !== "",
79-
// "Missing release version. Run the version script first."
80-
// );
81-
82-
// // 3. Determine the appropriate npm tag to use
83-
// let tag = semver.prerelease(version) == null ? "latest" : null;
84-
85-
// console.log();
86-
// console.log(
87-
// ` Publishing version ${version} to npm` + tag ? ` with tag "${tag}"` : ""
88-
// );
89-
90-
// // 3. Ensure build versions match the release version
91-
// await ensureBuildVersion("react-router", version);
92-
// await ensureBuildVersion("react-router-dom", version);
93-
// await ensureBuildVersion("react-router-native", version);
94-
95-
// // 4. Publish to npm
96-
// publishBuild("react-router", tag);
97-
// publishBuild("react-router-dom", tag);
98-
// publishBuild("react-router-native", tag);
99-
// } catch (error) {
100-
// console.log();
101-
// console.error(` ${error.message}`);
102-
// console.log();
103-
// return 1;
104-
// }
105-
106-
// return 0;
107-
// }
108-
109-
// run().then(code => {
110-
// process.exit(code);
111-
// });
112-
113-
// /**
114-
// * @typedef {'major'|'minor'|'patch'|'premajor'|'preminor'|'prepatch'|'prerelease'} SemverRelease
115-
// */
1+
const path = require("path");
2+
const execSync = require("child_process").execSync;
3+
const jsonfile = require("jsonfile");
4+
const semver = require("semver");
5+
6+
const rootDir = path.resolve(__dirname, "..");
7+
8+
/**
9+
* @param {*} cond
10+
* @param {string} message
11+
* @returns {asserts cond}
12+
*/
13+
function invariant(cond, message) {
14+
if (!cond) throw new Error(message);
15+
}
16+
17+
/**
18+
* @returns {string}
19+
*/
20+
function getTaggedVersion() {
21+
let output = execSync("git tag --list --points-at HEAD").toString();
22+
return output.replace(/^v|\n+$/g, "");
23+
}
24+
25+
/**
26+
* @param {string} packageName
27+
* @param {string|number} version
28+
*/
29+
async function ensureBuildVersion(packageName, version) {
30+
let file = path.join(rootDir, "packages", packageName, "package.json");
31+
let json = await jsonfile.readFile(file);
32+
invariant(
33+
json.version === version,
34+
`Package ${packageName} is on version ${json.version}, but should be on ${version}`
35+
);
36+
}
37+
38+
/**
39+
* @param {string} packageName
40+
* @param {string|null} tag
41+
*/
42+
function publishBuild(packageName, tag) {
43+
let buildDir = path.join(rootDir, "packages", packageName);
44+
let command = `npm publish ${buildDir}` + tag ? ` --tag ${tag}` : "";
45+
console.log();
46+
console.log(` ${command}`);
47+
console.log();
48+
execSync(command, { stdio: "inherit" });
49+
}
50+
51+
/**
52+
* @returns {Promise<1 | 0>}
53+
*/
54+
async function run() {
55+
try {
56+
// 0. Ensure we are in CI. We don't do this manually
57+
invariant(
58+
process.env.CI,
59+
`You should always run the publish script from the CI environment!`
60+
);
61+
62+
// 1. Get the current tag, which has the release version number
63+
let version = getTaggedVersion();
64+
invariant(
65+
version !== "",
66+
"Missing release version. Run the version script first."
67+
);
68+
69+
// 2. Determine the appropriate npm tag to use
70+
let tag = semver.prerelease(version) == null ? "latest" : null;
71+
72+
console.log();
73+
console.log(
74+
` Publishing version ${version} to npm` + tag ? ` with tag "${tag}"` : ""
75+
);
76+
77+
// 3. Ensure build versions match the release version
78+
await ensureBuildVersion("react-router", version);
79+
await ensureBuildVersion("react-router-dom", version);
80+
await ensureBuildVersion("react-router-native", version);
81+
82+
// 4. Publish to npm
83+
publishBuild("react-router", tag);
84+
publishBuild("react-router-dom", tag);
85+
publishBuild("react-router-native", tag);
86+
} catch (error) {
87+
console.log();
88+
console.error(` ${error.message}`);
89+
console.log();
90+
return 1;
91+
}
92+
93+
return 0;
94+
}
95+
96+
run().then(code => {
97+
process.exit(code);
98+
});

0 commit comments

Comments
 (0)