Skip to content

Instantly share code, notes, and snippets.

@imkarimkarim
Last active December 30, 2023 23:26
Show Gist options
  • Save imkarimkarim/ed6ec44e20d09fac7656938af7865844 to your computer and use it in GitHub Desktop.
Save imkarimkarim/ed6ec44e20d09fac7656938af7865844 to your computer and use it in GitHub Desktop.
version setter script
// nodejs script that adds a new version to the project. (git(local, and remote), package.json and a commit message)
// example: node setNewVersion.js 0.0.1
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
let v = process.argv[2];
if (v.charAt(0) === 'v') {
v = v.substring(1);
}
function editJsonFile(path, key, value, callback) {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const jsonData = JSON.parse(data);
jsonData[key] = value;
fs.writeFile(path, JSON.stringify(jsonData), 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
console.log(path + ' file updated successfully.');
if (typeof callback === 'function') callback();
});
});
}
editJsonFile(path.resolve(__dirname, '/package.json'), 'version', v, () => {
exec('git tag v' + v);
exec(`git add . ; git commit -m "build: 🔖 v${v}"; git push; git push --tags`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment