Skip to content

Commit

Permalink
fix: util.promisify(child_process.exec)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jul 29, 2018
1 parent 7fb1afc commit e460b95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/common/asar.js
Expand Up @@ -3,6 +3,7 @@
const {Buffer} = require('buffer')
const childProcess = require('child_process')
const path = require('path')
const util = require('util')

const hasProp = {}.hasOwnProperty

Expand Down Expand Up @@ -680,18 +681,27 @@
// called by `childProcess.{exec,execSync}`, causing
// Electron to consider the full command as a single path
// to an archive.
['exec', 'execSync'].forEach(function (functionName) {
const old = childProcess[functionName]
childProcess[functionName] = function () {
const processNoAsarOriginalValue = process.noAsar
process.noAsar = true
try {
return old.apply(this, arguments)
} finally {
process.noAsar = processNoAsarOriginalValue
for (const functionName of ['exec', 'execSync']) {
childProcess[functionName] = wrap(childProcess[functionName], function (func) {
return function () {
const processNoAsarOriginalValue = process.noAsar
process.noAsar = true
try {
return func.apply(this, arguments)
} finally {
process.noAsar = processNoAsarOriginalValue
}
}
})
}

function wrap (func, wrapper) {
const wrapped = wrapper(func)
if (func[util.promisify.custom]) {
wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
}
})
return wrapped
}

overrideAPI(fs, 'open')
overrideAPI(childProcess, 'execFile')
Expand Down
8 changes: 8 additions & 0 deletions spec/asar-spec.js
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert')
const ChildProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const util = require('util')
const {closeWindow} = require('./window-helpers')

const nativeImage = require('electron').nativeImage
Expand Down Expand Up @@ -644,6 +645,13 @@ describe('asar package', function () {
done()
})
})

it('can be promisified', (done) => {
util.promisify(ChildProcess.exec)('echo ' + echo + ' foo bar').then(({ stdout }) => {
assert.equal(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
done()
})
})
})

describe('child_process.execSync', function () {
Expand Down

0 comments on commit e460b95

Please sign in to comment.