feat: add flag to customize package version during pre-release staging (#155)

This commit is contained in:
Brandon Keiji
2025-04-24 20:02:49 +00:00
committed by GitHub
parent d97d2a4f7b
commit d394a9f39f
5 changed files with 36 additions and 15 deletions

View File

@@ -6,6 +6,9 @@
import fs from 'node:fs';
import path from 'node:path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { execSync } from 'node:child_process';
// Assuming script is run from a package directory (e.g., packages/cli)
const packageDir = process.cwd();
@@ -17,16 +20,32 @@ function getBaseVersion() {
const rootPackage = JSON.parse(fs.readFileSync(rootPackageJsonPath, 'utf8'));
let baseVersion = rootPackage.version;
// Append nightly suffix
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
const dd = String(today.getDate()).padStart(2, '0');
const nightlySuffix = `-nightly-${yyyy}${mm}${dd}`;
return `${baseVersion}${nightlySuffix}`;
// Get latest commit hash
const commitHash = execSync('git rev-parse --short HEAD', {
encoding: 'utf8',
}).trim();
// Append dev suffix with commit hash
const devSuffix = `-dev-${commitHash}.0`;
return `${baseVersion}${devSuffix}`;
}
const argv = yargs(hideBin(process.argv))
.option('pkg-version', {
type: 'string',
description: 'Set the package version',
})
.parse();
const newVersion = argv['pkg-version'] ?? getBaseVersion();
if (argv['pkg-version']) {
console.log(`Using provided package version (--pkg-version): ${newVersion}`);
} else {
console.log(
`Using base version with dev suffix and commit hash: ${newVersion}`,
);
}
const newVersion = getBaseVersion();
console.log(`Setting package version to: ${newVersion}`);
const packageJsonPath = path.join(packageDir, 'package.json');