diff --git a/README.md b/README.md index 9be7d7b..237db1c 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Spawns a HandbrakeCLI process with the supplied [options](https://handbrake.fr/d | Param | Type | Description | | --- | --- | --- | | [options] | object | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI | +| [options.HandbrakeCLIPath] | string | Override the built-in `HandbrakeCLI` binary path. | **Example** ```js @@ -138,6 +139,7 @@ Runs HandbrakeCLI with the supplied [options](https://handbrake.fr/docs/en/lates | Param | Type | Description | | --- | --- | --- | | options | Object | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI | +| [options.HandbrakeCLIPath] | string | Override the built-in `HandbrakeCLI` binary path. | | [onComplete] | function | If passed, `onComplete(err, stdout, stderr)` will be called on completion, `stdout` and `stderr` being strings containing the HandbrakeCLI output. | **Example** @@ -159,6 +161,7 @@ Identical to `hbjs.exec` except it returns a promise, rather than invoke a callb | Param | Type | Description | | --- | --- | --- | | options | Object | [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI | +| [options.HandbrakeCLIPath] | string | Override the built-in `HandbrakeCLI` binary path. | **Example** ```js diff --git a/index.js b/index.js index 29ca970..0ef27d7 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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() @@ -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 @@ -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) @@ -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 diff --git a/lib/Handbrake.js b/lib/Handbrake.js index f157ac4..5b13fcc 100644 --- a/lib/Handbrake.js +++ b/lib/Handbrake.js @@ -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 @@ -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 } /** diff --git a/test/exec.js b/test/exec.js index c0ad0e3..44b1335 100644 --- a/test/exec.js +++ b/test/exec.js @@ -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) { @@ -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 diff --git a/test/spawn-exception-handling.js b/test/spawn-exception-handling.js index f5fd9a5..2982d76 100644 --- a/test/spawn-exception-handling.js +++ b/test/spawn-exception-handling.js @@ -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 { @@ -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) diff --git a/test/spawn.js b/test/spawn.js index 45c796a..b0a2c0d 100644 --- a/test/spawn.js +++ b/test/spawn.js @@ -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