const shell = require('shelljs');
const pkgJSON = require('./package.json');

/******************************************************************
 * Make sure all changes are pushed prior to running this script.
 * This script uses the package version number as the latest tag.
 * Then run `yarn run publish` to push the latest
 *
 ******************************************************************/

const execCmd = cmd => {
  const execResult = shell.exec(cmd);

  if (execResult.code !== 0) {
    console.log('Error running command:', execResult);
    shell.exit(1);
  }

  return execResult.stdout.trim();
};

const gitMsg = `git log -1 --pretty=%B`;
const tagMsg = execCmd(gitMsg);
const gitTag = `git tag -a ${pkgJSON.version} -m "${tagMsg}"`;
const gitTagMsg = execCmd(gitTag);

console.log('GIT TAG CMD:', gitTag);
console.log('GIT TAG MSG:', gitTagMsg);
execCmd(`git push origin ${pkgJSON.version}`);
