Skip to content

Instantly share code, notes, and snippets.

@JhonatanMedeiros
Created May 26, 2020 15:45
Show Gist options
  • Save JhonatanMedeiros/6f19eca5878bfa76478e1c173446125f to your computer and use it in GitHub Desktop.
Save JhonatanMedeiros/6f19eca5878bfa76478e1c173446125f to your computer and use it in GitHub Desktop.
Script Release
// External Libs
const { promisify } = require('util');
const inquirer = require('inquirer');
const args = require('minimist')(process.argv);
const exec = promisify(require('child_process').exec);
const { inc, valid } = require('semver');
// Project Imports
const { read, write } = require('./util');
const pkg = require('../package.json');
inquireVersion(args.v || args.version)
.then(updateVersion)
.then(generateChangelog);
async function inquireVersion(v) {
if (valid(v)) {
return v;
}
const prompt = inquirer.createPromptModule();
const { version } = await prompt({
name: 'version',
message: 'Enter a version',
default: () => inc(pkg.version, 'patch'),
validate: val => !!val.length || 'Invalid version'
});
return version;
}
async function updateVersion(version) {
await Promise.all([
run(`npm version ${version} --git-tag-version false`),
replaceInFile('src/environments/environment.ts', data => data.replace(pkg.version, version)),
]);
return version;
}
async function generateChangelog(version) {
await sequential([
() => run('npm run release:changelog')
]);
return version;
}
async function replaceInFile(file, fn) {
await write(file, fn(await read(file)));
}
async function sequential(tasks) {
await tasks.reduce((promise, task) => promise.then(task), Promise.resolve());
}
async function run(cmd) {
const {stdout, stderr} = await exec(cmd);
stdout && console.log(stdout.trim());
stderr && console.log(stderr.trim());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment