Last active
          October 13, 2017 20:35 
        
      - 
      
- 
        Save kevinSuttle/cdb9d9a7c26ce628eaca7fdd01ecee94 to your computer and use it in GitHub Desktop. 
    Buildstamp banner
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const git = require('git-rev-sync'); | |
| const pkg = require('../package.json'); | |
| const moment = require('moment'); | |
| const tz = require('moment-timezone'); | |
| const timeFormat = 'MMMM Do YYYY, h:mm:ss a'; | |
| const timeStamp = moment().tz('America/New_York').format(timeFormat); | |
| const childProcess = require('child_process'); | |
| const fs = require('fs'); | |
| const jsonTemplate = './src/assets/data/buildstamp.json'; | |
| const trackingBranch = 'origin/develop'; | |
| const workingBranch = process.env.CIRCLE_BRANCH || git.branch(); | |
| const gitRevList = | |
| childProcess | |
| .execSync(`git rev-list origin/develop...HEAD --left-right --count`) | |
| .toString(); | |
| const aheadNumOfCommits = parseInt(gitRevList.slice(2, -1)); | |
| let aheadMessage; | |
| const behindNumOfCommits = parseInt(gitRevList.slice(0, 1)); | |
| let behindMessage; | |
| let relativeBranchStatus; | |
| const getCommitsBehind = () => { | |
| switch (behindNumOfCommits) { | |
| case 0: | |
| break; | |
| case 1: | |
| behindMessage = '1 commit behind'; | |
| break; | |
| default: | |
| behindMessage = `${behindNumOfCommits} commits behind`; | |
| break; | |
| } | |
| return behindMessage; | |
| } | |
| function getCommitsAhead() { | |
| switch (aheadNumOfCommits) { | |
| case 0: | |
| break; | |
| case 1: | |
| aheadMessage = '1 commit ahead'; | |
| break; | |
| default: | |
| aheadMessage = `${aheadNumOfCommits} commits ahead`; | |
| break; | |
| } | |
| return aheadMessage; | |
| } | |
| function assembleStatusSentence() { | |
| if (behindNumOfCommits === 0 && aheadNumOfCommits === 0) { | |
| relativeBranchStatus = `The branch ${workingBranch} is up to date with ${trackingBranch}`; | |
| console.log(`up to date`); | |
| } else if (behindNumOfCommits === 0 && aheadNumOfCommits !== 0) { | |
| relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsAhead()} of ${trackingBranch}`; | |
| console.log(`ahead`); | |
| } else if (behindNumOfCommits !== 0 && aheadNumOfCommits === 0) { | |
| relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsBehind()} ${trackingBranch}`; | |
| console.log(`behind`); | |
| } else if (behindNumOfCommits !== 0 && aheadNumOfCommits !== 0) { | |
| relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsBehind()} and ${getCommitsAhead()} of ${trackingBranch}`; | |
| console.log(`both off`); | |
| } else { | |
| relativeBranchStatus = 'No branch tracking information available'; | |
| console.log(`wtf`); | |
| } | |
| return relativeBranchStatus; | |
| } | |
| const getCommitMessageTitle = function() { | |
| return childProcess | |
| .execSync(`git --no-pager show -s --format=%s ${git.long()}`) | |
| .toString() | |
| .trim(); | |
| }; | |
| const getCommitAuthor = function() { | |
| return childProcess | |
| .execSync(`git --no-pager show -s --format='%an'`) | |
| .toString() | |
| .trim(); | |
| }; | |
| const banner = ` | |
| [{ | |
| "buildstampTime": "${timeStamp} (EST)", | |
| "hash": "${git.long()}", | |
| "message": "${getCommitMessageTitle()}", | |
| "time": "${moment(git.date()).tz('America/New_York').format(timeFormat)} (EST)", | |
| "author": "${getCommitAuthor()}", | |
| "branch": "${workingBranch}", | |
| "tag": "${git.tag()}", | |
| "status": "${assembleStatusSentence()}", | |
| "package": "${pkg.name}", | |
| "version": "${pkg.version}", | |
| "circleBranch": "${workingBranch}", | |
| "circleBuildNumber": "${process.env.CIRCLE_BUILD_NUM}", | |
| "circleBuildUrl": "${process.env.CIRCLE_BUILD_URL}" | |
| }] | |
| `; | |
| const existingJSON = fs.readFileSync(jsonTemplate); | |
| const fileWriter = fs.openSync(jsonTemplate, 'w+'); | |
| const buffer = new Buffer(banner); | |
| childProcess.execSync('git update-index --skip-worktree ./src/assets/data/buildstamp.json'); | |
| fs.writeSync(fileWriter, buffer, 0, buffer.length, 0); | |
| fs.close(fileWriter); | |
| // tslint:disable-next-line: no-console | |
| console.info(`File written to ${jsonTemplate}`); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment