Skip to content

Commit

Permalink
feat: Add $spawnOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Feb 15, 2020
1 parent e95b95d commit 0d04ec2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Those options are not provided by 7-Zip but are features of this module.
| `$bin` | `string` | Path to an other 7-Zip binary. Default: `7z` |
| `$cherryPick` | `string[]` | Some commands accepts more specific targets, see example above |
| `$raw` | `string[]` | Pass raw arguments to the `child_process.spawn()`command |
| `$spawnOptions` | [`Object`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | Pass `options` to the `child_process.spawn()`command |

### Events

Expand Down
4 changes: 3 additions & 1 deletion src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const createFactory = ({
.concat(Flags.fromOptions(options))
seven._isProgressFlag = seven._args.includes('-bsp1')
seven._stage = STAGE_HEADERS
seven._spawnOptions = options.$spawnOptions
seven._matchBodyData = Parser.fetch(options._command, 'bodyData')
seven._matchEndOfHeaders = Parser.fetch(options._command, 'endOfHeaders')
seven._matchEndOfBody = Parser.fetch(options._command, 'endOfBody')
Expand All @@ -67,7 +68,8 @@ const listenFactory = ({
}

const run = stream => {
stream._childProcess = spawn(stream._bin, stream._args, { detached: true })
let spawnOptions = Object.assign({ detached: true }, stream._spawnOptions)
stream._childProcess = spawn(stream._bin, stream._args, spawnOptions)
return stream
}

Expand Down
14 changes: 14 additions & 0 deletions test/func/add.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,18 @@ describe('Functional: add()', function () {
done()
})
})

it('should accept $spawnOptions', function (done) {
const archive = `${tmpDir}/size.7z`
const source = `${mockDir}/DirHex/`
const seven = add(archive, source, {
$spawnOptions: { cwd: '.' }
})
seven.on('end', function () {
const size = statSync(archive).size
expect(size).to.greaterThan(350)
expect(existsSync(archive)).to.equal(true)
done()
})
})
})
13 changes: 11 additions & 2 deletions test/unit/lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Unit: lifecycle.js', function () {
})

describe('run()', function () {
it('should should emit an child process error', function (done) {
it('should emit an child process error', function (done) {
const sevenFake = sevenFakeFactory()
sevenFake._bin = 'not_a_program'
sevenFake._args = []
Expand All @@ -149,14 +149,23 @@ describe('Unit: lifecycle.js', function () {
})
})

it('should should populate child process', function () {
it('should populate child process', function () {
const sevenFake = new Readable({ read () {} })
sevenFake._bin = 'npm'
sevenFake._args = []
const r = Seven.run(sevenFake)
expect(isChildProcess(r._childProcess)).to.eql(true)
})

it('should accept child process options', function () {
const sevenFake = new Readable({ read () {} })
sevenFake._bin = 'npm'
sevenFake._args = []
sevenFake._spawnOptions = { cmd: '.' }
const r = Seven.run(sevenFake)
expect(isChildProcess(r._childProcess)).to.eql(true)
})

it('should be chainable', function () {
const sevenFake = sevenFakeFactory()
sevenFake._bin = 'not_a_program'
Expand Down

0 comments on commit 0d04ec2

Please sign in to comment.