Skip to content

Commit 9e92f34

Browse files
committed
chore(cli): tweak version inlining
1 parent 68aacd6 commit 9e92f34

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
[
6363
"@semantic-release/exec",
6464
{
65-
"publishCmd": "node scripts/inline-version.mjs ${nextRelease.version}"
65+
"publishCmd": "test -f scripts/inline-version.mjs && node scripts/inline-version.mjs --verbose ${nextRelease.version}"
6666
}
6767
],
6868
"@semantic-release/npm",

packages/cli/scripts/inline-version.mjs

+46-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,51 @@ import { fileURLToPath } from 'node:url';
44
import { join } from 'node:path';
55

66
const cwd = join(fileURLToPath(import.meta.url), '../..');
7+
const filepath = join(cwd, 'src/version.ts');
78

8-
const version =
9-
process.argv.length === 3 ? process.argv[2] : JSON.parse(fs.readFileSync(join(cwd, 'package.json'), 'utf8')).version;
9+
const version = getVersionFromArgv() ?? JSON.parse(fs.readFileSync(join(cwd, 'package.json'), 'utf8')).version;
1010

11-
fs.writeFileSync(join(cwd, 'src/version.ts'), `export const VERSION = '${version}';\n`);
11+
try {
12+
const existingVersion = /VERSION = '([^']+)';/.exec(fs.readFileSync(filepath, 'utf8'));
13+
if (existingVersion !== null && !isNewerVersion(existingVersion[1], version)) {
14+
log(`Skipping inlining. Next version is not newer than the existing one: ${existingVersion[1]}.`);
15+
process.exit(0);
16+
}
17+
} catch (ex) {
18+
// no-op
19+
}
20+
21+
fs.writeFileSync(filepath, `export const VERSION = '${version}';\n`);
22+
23+
log(`Inlined ${version} version.`);
24+
25+
function isNewerVersion(current, next) {
26+
const [curMajor, curMinor, curPatch] = current.split('.').map(Number);
27+
const [nextMajor, nextMinor, nextPatch] = next.split('.').map(Number);
28+
29+
return (
30+
nextMajor > curMajor ||
31+
(curMajor === nextMajor && (nextMinor > curMinor || (curMinor <= nextMinor && nextPatch > curPatch)))
32+
);
33+
}
34+
35+
function log(message) {
36+
if (process.argv.includes('--verbose') || process.env.CI) {
37+
process.stdout.write(`${message}\n`);
38+
}
39+
}
40+
41+
function getVersionFromArgv() {
42+
if (process.argv.length < 3) return null;
43+
44+
const r = /^([0-9]+\.){2}[0-9]+$/;
45+
46+
for (let i = 2; i < process.argv.length; i++) {
47+
const value = process.argv[i];
48+
if (r.exec(value)) {
49+
return value;
50+
}
51+
}
52+
53+
return null;
54+
}

0 commit comments

Comments
 (0)