Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from npm/one-way-or-another
Browse files Browse the repository at this point in the history
feat: use BUILD_HASH if present; otherwise git rev-parse
  • Loading branch information
joeledwards committed Sep 5, 2018
2 parents 330fe5c + bbe093e commit 32bb147
Show file tree
Hide file tree
Showing 3 changed files with 880 additions and 813 deletions.
26 changes: 22 additions & 4 deletions index.js
Expand Up @@ -11,6 +11,10 @@ let contributor = null
module.exports = function startMonitor (port, callback) {
const monitor = http.createServer()
let commitHash = ''
let listening = () => {}
let ready = new Promise(resolve => {
listening = resolve
})

monitor.on('request', (req, res) => {
if (req.url !== '/_monitor/ping') {
Expand Down Expand Up @@ -56,15 +60,29 @@ module.exports = function startMonitor (port, callback) {
res.end('')
})

exec('git rev-parse --short HEAD', function (err, stdout, stderr) {
if (!err && stdout) commitHash = stdout.trim()
monitor.listen(port, callback)
function gitHash () {
return new Promise(resolve => {
exec('git rev-parse --short HEAD', function (err, stdout, stderr) {
resolve((!err && stdout) ? stdout.trim() : undefined)
})
})
}

Promise.resolve(process.env.BUILD_HASH)
.then(envHash => envHash || gitHash())
.then(hash => {
commitHash = hash
monitor.listen(port, () => {
listening()
callback()
})
})

return Object.assign(monitor, {
contribute: (_contributor) => {
assert(typeof _contributor === 'function', 'contributor must be a function')
contributor = _contributor
}
},
stop: () => ready.then(() => new Promise(resolve => monitor.close(resolve)))
})
}

0 comments on commit 32bb147

Please sign in to comment.