Skip to content

Instantly share code, notes, and snippets.

@omrilotan
Last active May 23, 2020 16:13
Show Gist options
  • Save omrilotan/6bd4c5d4b1f1776a695606f98a0f227a to your computer and use it in GitHub Desktop.
Save omrilotan/6bd4c5d4b1f1776a695606f98a0f227a to your computer and use it in GitHub Desktop.
Create a tag by the last commit's author with it's message (example for async getters)
const asyncExec = require('util').promisify(require('child_process').exec);
/**
* Execute command line in a child process
* @param {...String} args Commands
* @return {String}
*/
async function exec (...args) {
const { stdout, stderr } = await asyncExec(...args);
if (stderr) {
throw new Error(stderr);
}
return stdout.trim();
}
/**
* @typedef gitData
* @description Git data getters
* @type {Object}
* @property {String} author Author of the last commit
* @property {String} email Git user email
* @property {String} message Most recent commit message
*/
const gitData = Object.defineProperties({}, {
author: { get: async () => await exec('git log -1 --pretty=%an') },
email: { get: async () => await exec('git log -1 --pretty=%ae') },
message: { get: async () => await exec('git log -1 --pretty=%B') },
});
/**
* Create a tag by the last commit's author with it's message
* @param {String} tag Tag name (e.g. v1.1.0)
* no return value
*/
module.exports = async (tag) => {
const { message, author, email } = gitData;
try {
await exec(`git config --global user.name "${await author}"`);
await exec(`git config --global user.email "${await email}"`);
await exec(`git tag -a ${tag} -m "${await message}"`);
await exec(`git push origin refs/tags/${tag}`);
} catch (error) {
console.error(error);
throw error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment