Skip to content

Commit 732f108

Browse files
committed
📦 NEW: Snapshot script
1 parent 6a5633a commit 732f108

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* This script creates a snapshot release by performing the following steps:
3+
* 1. Ensures the script is running from the project root directory.
4+
* 2. Defines a function to execute shell commands and log their output.
5+
* 3. Defines a function to update the version in a given package.json file.
6+
* - If the current version is already a snapshot, it increments the snapshot number.
7+
* - If the current version is not a snapshot, it increments the patch version and sets the snapshot number to 0.
8+
* 4. Retrieves the current commit short SHA.
9+
* 5. Bumps the version in the specified package.json files.
10+
* 6. Runs a series of commands to version, build, and publish the packages as a snapshot release.
11+
*
12+
* @requires child_process
13+
* @requires path
14+
* @requires fs
15+
*/
16+
const {execSync} = require('child_process');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
// Ensure we're in the project root
21+
process.chdir(path.resolve(__dirname, '../..'));
22+
23+
// Function to execute commands and log output
24+
function run(command) {
25+
console.log(`Running: ${command}`);
26+
try {
27+
execSync(command, {stdio: 'inherit'});
28+
} catch (error) {
29+
console.error(`Error executing command: ${command}`);
30+
console.error(error);
31+
process.exit(1);
32+
}
33+
}
34+
35+
// Function to update version in package.json
36+
function bumpVersion(packagePath) {
37+
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
38+
const currentVersion = pkg.version;
39+
let [major, minor, patch, snapshot] = currentVersion
40+
.split(/[-.]/)
41+
.map(v => (isNaN(parseInt(v)) ? v : parseInt(v)));
42+
43+
if (snapshot === 'snapshot') {
44+
// If already a snapshot, increment the snapshot number
45+
snapshot = parseInt(pkg.version.split('-snapshot.')[1]) + 1;
46+
} else {
47+
// If not a snapshot, increment patch and set snapshot to 0
48+
patch += 1;
49+
snapshot = 0;
50+
}
51+
52+
pkg.version = `${major}.${minor}.${patch}-snapshot.${snapshot}`;
53+
fs.writeFileSync(packagePath, JSON.stringify(pkg, null, 2));
54+
console.log(`Updated ${packagePath} to version ${pkg.version}`);
55+
}
56+
57+
// Get the current commit short SHA
58+
const SHORT_SHA = execSync('git rev-parse --short HEAD').toString().trim();
59+
60+
console.log('Creating snapshot release...');
61+
62+
// Bump versions
63+
bumpVersion('./packages/langbase/package.json');
64+
65+
// Version and tag the snapshot release
66+
run(`pnpm changeset version --snapshot ${SHORT_SHA}`);
67+
68+
// Build and publish the snapshot release
69+
run('pnpm build');
70+
run('pnpm changeset publish --no-git-tag --tag snapshot');
71+
72+
// Reset Git changes
73+
console.log('Git commit and push changes...');
74+
75+
console.log('All changes have been reset. Snapshot release process complete!');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"devv": "turbo dev --no-cache --concurrency 16 --continue",
2424
"ci:release": "turbo clean && turbo build && changeset publish",
2525
"clean-examples": "node .github/scripts/cleanup-examples-changesets.mjs",
26-
"ci:version": "changeset version && node .github/scripts/cleanup-examples-changesets.mjs && pnpm install --no-frozen-lockfile"
26+
"ci:version": "changeset version && node .github/scripts/cleanup-examples-changesets.mjs && pnpm install --no-frozen-lockfile",
27+
"snapshot": "node .github/scripts/release-snapshot.js"
2728
},
2829
"devDependencies": {
2930
"@langbase/eslint-config": "workspace:*",

0 commit comments

Comments
 (0)