|
| 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/baseai/package.json'); |
| 64 | +bumpVersion('./packages/core/package.json'); |
| 65 | + |
| 66 | +// Version and tag the snapshot release |
| 67 | +run(`pnpm changeset version --snapshot ${SHORT_SHA}`); |
| 68 | + |
| 69 | +// Build and publish the snapshot release |
| 70 | +run('pnpm build:pkgs'); |
| 71 | +run('pnpm changeset publish --no-git-tag --tag snapshot'); |
| 72 | + |
| 73 | +// Reset Git changes |
| 74 | +console.log('Git commit and push changes...'); |
| 75 | +run('git add . && git commit -m "π¦ NEW: snapshot release" && git push'); |
| 76 | + |
| 77 | +console.log('All changes have been reset. Snapshot release process complete!'); |
0 commit comments