Skip to content

Commit

Permalink
add options.HandbrakeCLIPath #62
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Sep 10, 2021
1 parent ef07fe6 commit aa4375e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Spawns a HandbrakeCLI process with the supplied [options](https://handbrake.fr/d
| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>object</code> | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI |
| [options.HandbrakeCLIPath] | <code>string</code> | Override the built-in `HandbrakeCLI` binary path. |

**Example**
```js
Expand All @@ -138,6 +139,7 @@ Runs HandbrakeCLI with the supplied [options](https://handbrake.fr/docs/en/lates
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI |
| [options.HandbrakeCLIPath] | <code>string</code> | Override the built-in `HandbrakeCLI` binary path. |
| [onComplete] | <code>function</code> | If passed, `onComplete(err, stdout, stderr)` will be called on completion, `stdout` and `stderr` being strings containing the HandbrakeCLI output. |

**Example**
Expand All @@ -159,6 +161,7 @@ Identical to `hbjs.exec` except it returns a promise, rather than invoke a callb
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI |
| [options.HandbrakeCLIPath] | <code>string</code> | Override the built-in `HandbrakeCLI` binary path. |

**Example**
```js
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import cliOptions from './lib/cli-options.js'
* Spawns a HandbrakeCLI process with the supplied [options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options), returning an instance of `Handbrake` on which you can listen for events.
*
* @param {object} [options] - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @returns {module:handbrake-js~Handbrake}
* @alias module:handbrake-js.spawn
* @example
Expand All @@ -36,13 +37,12 @@ import cliOptions from './lib/cli-options.js'
* .on('output', console.log)
* ```
*/
function spawn (options, mocks) {
const handbrake = new Handbrake(mocks)
function spawn (options = {}, mocks) {
const handbrake = new Handbrake(options, mocks)

/* defer so the caller can attach event listers on the returned Handbrake instance first */
process.nextTick(function () {
try {
handbrake.options = options
handbrake._run()
} catch (error) {
const err = new Error()
Expand All @@ -59,6 +59,7 @@ function spawn (options, mocks) {
* Runs HandbrakeCLI with the supplied [options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) calling the supplied callback on completion. The exec method is best suited for short duration tasks where you can wait until completion for the output.
*
* @param options {Object} - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @param [onComplete] {Function} - If passed, `onComplete(err, stdout, stderr)` will be called on completion, `stdout` and `stderr` being strings containing the HandbrakeCLI output.
*
* @example
Expand All @@ -72,10 +73,10 @@ function spawn (options, mocks) {
* ```
* @alias module:handbrake-js.exec
*/
function exec (options, done) {
function exec (options = {}, done) {
const cmd = util.format(
'"%s" %s',
HandbrakeCLIPath,
options.HandbrakeCLIPath || HandbrakeCLIPath,
toSpawnArgs(options, { quote: true }).join(' ')
)
cp.exec(cmd, done)
Expand All @@ -84,6 +85,7 @@ function exec (options, done) {
/**
* Identical to `hbjs.exec` except it returns a promise, rather than invoke a callback. Use this when you don't need the progress events reported by `hbjs.spawn`. Fulfils with an object containing the output in two properties: `stdout` and `stderr`.
* @param options {Object} - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @returns {Promise}
* @example
* ```js
Expand Down
7 changes: 3 additions & 4 deletions lib/Handbrake.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import childProcess from 'child_process'
* @inner
*/
class Handbrake extends EventEmitter {
constructor (mocks) {
constructor (options = {}, mocks) {
super()
/**
* A `string` containing all handbrakeCLI output
Expand All @@ -34,14 +34,13 @@ class Handbrake extends EventEmitter {
* a copy of the options passed to {@link module:handbrake-js.spawn}
* @type {object}
*/
this.options = null
this.options = options

/* path to the HandbrakeCLI executable downloaded by the install script */
this.HandbrakeCLIPath = HandbrakeCLIPath
this.HandbrakeCLIPath = options.HandbrakeCLIPath || HandbrakeCLIPath

/* for test scripts */
this.cp = (mocks && mocks.cp) || childProcess
this.HandbrakeCLIPath = (mocks && mocks.HandbrakeCLIPath) || this.HandbrakeCLIPath
}

/**
Expand Down
16 changes: 15 additions & 1 deletion test/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { strict as a } from 'assert'

const tom = new TestRunner.Tom()

tom.test('exec: --preset-list', async function () {
tom.test('--preset-list', async function () {
return new Promise((resolve, reject) => {
hbjs.exec({ 'preset-list': true }, function (err, stdout, stderr) {
if (err) {
Expand All @@ -17,4 +17,18 @@ tom.test('exec: --preset-list', async function () {
})
})

tom.test('HandbrakeCLIPath', async function () {
return new Promise((resolve, reject) => {
hbjs.exec({ 'preset-list': true, HandbrakeCLIPath: 'one' }, function (err, stdout, stderr) {
if (err) {
a.ok(/command not found/.test(err.message))
a.equal(err.cmd, '"one" --preset-list --HandbrakeCLIPath "one"')
resolve()
} else {
reject(new Error("Shouldn't reach here"))
}
})
})
})

export default tom
5 changes: 2 additions & 3 deletions test/spawn-exception-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const tom = new TestRunner.Tom({ maxConcurrency: 1 })
tom.test('validation: HandbrakeCLI not found', function () {
return new Promise(function (resolve, reject) {
const handbrake = hbjs.spawn(
{ input: 'in', output: 'out' },
{ HandbrakeCLIPath: 'broken/path' }
{ input: 'in', output: 'out', HandbrakeCLIPath: 'broken/path' }
)
handbrake.on('error', function (err) {
try {
Expand All @@ -18,7 +17,7 @@ tom.test('validation: HandbrakeCLI not found', function () {
a.equal(err.HandbrakeCLIPath, 'broken/path')
a.ok(err.errno === 'ENOENT' || err.errno === -2)
a.ok(/ENOENT/.test(err.spawnmessage))
a.deepEqual(err.options, { input: 'in', output: 'out' })
a.deepEqual(err.options, { input: 'in', output: 'out', HandbrakeCLIPath: 'broken/path' })
resolve()
} catch (err) {
reject(err)
Expand Down
5 changes: 5 additions & 0 deletions test/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ tom.test('spawn: correct return type', async function () {
a.ok(handbrake instanceof Handbrake)
})

tom.test('Handbrake class: options.HandbrakeCLIPath set correctly', async function () {
const handbrake = new Handbrake({ input: 'in', output: 'out', HandbrakeCLIPath: 'one' })
a.equal(handbrake.HandbrakeCLIPath, 'one')
})

export default tom

0 comments on commit aa4375e

Please sign in to comment.