Skip to content

Commit

Permalink
feat: Add release script
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Aug 5, 2024
1 parent b847d54 commit 1551a16
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"testc": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"prettier": "npx prettier --write .",
"version": "node scripts/version.mjs"
"release": "node scripts/release.mjs"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
Expand Down
45 changes: 43 additions & 2 deletions scripts/version.mjs → scripts/release.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { exec } from 'child_process';

/**
* @typedef {{
Expand Down Expand Up @@ -32,6 +33,7 @@ function updateVersions(version) {
packages.forEach(p =>
updatePackage(getPkgRoot(p), version),
)
console.log(`Updated all packages to version ${version}`);
}

/**
Expand All @@ -54,15 +56,54 @@ function updatePackage(pkgRoot, version) {
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}

/**
*
* @param {string} cmd
*/
async function run(cmd) {
console.log(`> ${cmd}`)
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (err) {
reject(err)
process.exit(1)
}
resolve(stdout)
})
})
}

/**
*
* @param {string} version
*/
async function tag(version) {
await run(`git add -A`)
await run(`git commit -m '🐎 ci: release ${version}'`)
await run(`git tag ${version}`)
await run(`git push origin refs/tags/${version}`)
await run(`git push`)
}

async function main() {
const version = process.argv[2]
if (!version) {
console.error('Please provide a version, e.g. `npm run release 1.0.0`')
process.exit(1)
}

updateVersions(version)
console.log('Releasing ' + version)
console.log(`Releasing ${version}. Confirm?<y/n>`)

process.stdin.on('data', async (data) => {
if (data.toString().trim() === 'y') {
updateVersions(version);
await tag(version);
console.log('Released ' + version)
} else {
console.log('Aborting...')
}
process.exit(1)
})
}

await main()

0 comments on commit 1551a16

Please sign in to comment.