-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathbefore-beta-release.cjs
More file actions
35 lines (27 loc) · 1.31 KB
/
before-beta-release.cjs
File metadata and controls
35 lines (27 loc) · 1.31 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
const { execSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const PKG_JSON_PATH = path.join(__dirname, '..', '..', 'package.json');
// eslint-disable-next-line import/no-dynamic-require
const pkgJson = require(PKG_JSON_PATH);
const PACKAGE_NAME = pkgJson.name;
const VERSION = pkgJson.version;
const nextVersion = getNextVersion(VERSION);
// eslint-disable-next-line no-console
console.log(`before-deploy: Setting version to ${nextVersion}`);
pkgJson.version = nextVersion;
fs.writeFileSync(PKG_JSON_PATH, `${JSON.stringify(pkgJson, null, 2)}\n`);
function getNextVersion(version) {
const versionString = execSync(`npm show ${PACKAGE_NAME} versions --json`, { encoding: 'utf8' });
const versions = JSON.parse(versionString);
if (versions.some((v) => v === VERSION)) {
// eslint-disable-next-line no-console
console.error(`before-deploy: A release with version ${VERSION} already exists. Please increment version accordingly.`);
process.exit(1);
}
const prereleaseNumbers = versions
.filter((v) => (v.startsWith(VERSION) && v.includes('-')))
.map((v) => Number(v.match(/\.(\d+)$/)[1]));
const lastPrereleaseNumber = Math.max(-1, ...prereleaseNumbers);
return `${version}-beta.${lastPrereleaseNumber + 1}`;
}