Skip to content

Commit

Permalink
Update bl to v4.0.0, rename a few vars, use template literals. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored and rvagg committed Oct 2, 2019
1 parent 51ad38d commit 77d91ea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**A specialised child process spawn for `git` commands**

_Compatible with Node.js v4+_
_Compatible with Node.js 8.x_

Note: while there's nothing currently preventing this from being used to execute arbitrary bash commands, be warned that it's use is focused on git and the API may evolve to be more specific into the future. You're welcome to fork or copy the patterns used if you need similar functionality for other uses.

Expand Down
31 changes: 20 additions & 11 deletions gitexec.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
'use strict'

const spawn = require('child_process').spawn
const bl = require('bl')
const { spawn } = require('child_process')
const { BufferListStream } = require('bl')

function exec (repoPath, gitcmd) {
const child = spawn(gitcmd, { env: process.env, cwd: repoPath, shell: true })

child.stderr.pipe(bl((err, _data) => {
if (err) { return child.stdout.emit('error', err) }
child.stderr.pipe(BufferListStream((err, data) => {
if (err) {
return child.stdout.emit('error', err)
}

if (_data.length) { process.stderr.write(_data) }
if (data.length) {
process.stderr.write(data)
}
}))

child.on('close', (code) => {
if (!code) { return }
if (!code) {
return
}

child.stdout.emit(
'error'
, new Error('git command [' + gitcmd + '] exited with code ' + code)
'error',
new Error(`git command [${gitcmd}] exited with code ${code}`)
)
})

return child.stdout
}

function execCollect (repoPath, gitcmd, callback) {
exec(repoPath, gitcmd).pipe(bl((err, _data) => {
if (err) { return callback(err) }
exec(repoPath, gitcmd).pipe(BufferListStream((err, data) => {
if (err) {
return callback(err)
}

callback(null, _data.toString())
callback(null, data.toString())
}))
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"url": "https://github.com/rvagg/gitexec.git"
},
"dependencies": {
"bl": "~1.0.0"
"bl": "^4.0.0"
},
"devDependencies": {
"standard": "~14.3.1"
"standard": "^14.3.1"
}
}
18 changes: 11 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
'use strict'

const assert = require('assert')
const bl = require('bl')
const gitexec = require('./')
const { BufferListStream } = require('bl')
const gitexec = require('.')

gitexec.exec(__dirname, 'git log --format="%H %cd"')
.pipe(bl((err, data) => {
if (err) { throw err }
.pipe(BufferListStream((err, data) => {
if (err) {
throw err
}

data = data.toString()

console.log('------------------\n', data, '------------------------')
console.log(`------------------\n${data}------------------------`)
const commits = data.split(/[\r\n]+/).filter(Boolean)
assert.strictEqual('4525f40007bd7200d2a6c8d1e4b742f3567e83c3 Mon Dec 14 21:34:07 2015 +1100', commits[commits.length - 1])
}))

gitexec.exec(__dirname, 'git log --format="%H %cd"', (err, data) => {
if (err) { throw err }
if (err) {
throw err
}

console.log('------------------\n', data, '------------------------')
console.log(`------------------\n${data}------------------------`)
const commits = data.split(/[\r\n]+/).filter(Boolean)
assert.strictEqual('4525f40007bd7200d2a6c8d1e4b742f3567e83c3 Mon Dec 14 21:34:07 2015 +1100', commits[commits.length - 1])
})

0 comments on commit 77d91ea

Please sign in to comment.