Skip to content

Instantly share code, notes, and snippets.

@brentonhouse
Forked from omrilotan/git-tag.js
Created May 23, 2020 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentonhouse/dda673fa02aa4be2717978a7e4250ba6 to your computer and use it in GitHub Desktop.
Save brentonhouse/dda673fa02aa4be2717978a7e4250ba6 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