Created
January 23, 2020 10:25
-
-
Save fatso83/ce9963d117a56ceb75be59e957f3dddb to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
/* eslint-disable no-console */ | |
const appInfoFile = `${__dirname}/../src/appinfo.js`; | |
const async = require('async'); | |
const cp = require('child_process'); | |
const fs = require('fs'); | |
const exec = cmd => cb => cp.exec(cmd, (err, stdout, _stderr) => cb(err, ('' || stdout).trim())); | |
const buildOutput = results => { | |
const data = { | |
commitHash: results.commit, | |
branch: results.branch, | |
changeDate: results.changeDate | |
}; | |
if (process.argv[2] === '--add-builddate') { | |
data.buildDate = new Date().toISOString(); | |
} | |
// make the format returned from git the same as from node | |
data.changeDate = new Date(data.changeDate).toISOString(); | |
return 'const data = ' + JSON.stringify(data, null, ' ') + ';\n' + 'export default data;'; | |
}; | |
const gitcmd = 'git --git-dir ../.git --work-tree ..'; | |
async.parallel( | |
{ | |
branch: exec(gitcmd + ' rev-parse --abbrev-ref HEAD'), | |
commit: exec(gitcmd + ' rev-parse HEAD'), | |
changeDate: exec(gitcmd + ' show -s --format=%ci HEAD') // iso-like string. %cI (strict iso) is only supported in newer git versions (2.6+?), and the server has git 2.1 | |
}, | |
(err, results) => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
fs.writeFile(appInfoFile, buildOutput(results), err => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment