-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy up command line parsing Add basic tests for cli.js
- Loading branch information
1 parent
751c87d
commit d5a1e12
Showing
5 changed files
with
150 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
require('@gustavnikolaj/async-main-wrap')(require('./subfont'))( | ||
require('./parseCommandLineOptions')(), | ||
console | ||
); | ||
const { yargs, help, ...options } = require('./parseCommandLineOptions')(); | ||
|
||
require('@gustavnikolaj/async-main-wrap')(require('./subfont'), { | ||
processError(err) { | ||
yargs.showHelp(); | ||
if (err.constructor === SyntaxError) { | ||
// Avoid rendering a stack trace for the wrong usage errors | ||
err.customOutput = err.message; | ||
} | ||
return err; | ||
} | ||
})(options, console); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
const pathModule = require('path'); | ||
const expect = require('unexpected').clone(); | ||
|
||
const childProcess = require('child_process'); | ||
|
||
function consumeStream(stream) { | ||
return new Promise((resolve, reject) => { | ||
const buffers = []; | ||
stream | ||
.on('data', buffer => buffers.push(buffer)) | ||
.on('end', () => resolve(Buffer.concat(buffers))) | ||
.on('error', reject); | ||
}); | ||
} | ||
|
||
async function run(commandAndArgs, stdin) { | ||
if (typeof commandAndArgs !== 'undefined' && !Array.isArray(commandAndArgs)) { | ||
commandAndArgs = [commandAndArgs]; | ||
} | ||
|
||
const proc = childProcess.spawn(commandAndArgs[0], commandAndArgs.slice(1)); | ||
|
||
const promises = { | ||
exit: new Promise((resolve, reject) => { | ||
proc.on('error', reject).on('exit', exitCode => { | ||
if (exitCode === 0) { | ||
resolve(); | ||
} else { | ||
const err = new Error(`Child process exited with ${exitCode}`); | ||
err.exitCode = exitCode; | ||
reject(err); | ||
} | ||
}); | ||
}), | ||
stdin: new Promise((resolve, reject) => { | ||
proc.stdin.on('error', reject).on('close', resolve); | ||
}), | ||
stdout: consumeStream(proc.stdout), | ||
stderr: consumeStream(proc.stderr) | ||
}; | ||
|
||
if (typeof stdin === 'undefined') { | ||
proc.stdin.end(); | ||
} else { | ||
proc.stdin.end(stdin); | ||
} | ||
|
||
try { | ||
await Promise.all(Object.values(promises)); | ||
return [await promises.stdout, await promises.stderr]; | ||
} catch (err) { | ||
err.stdout = await promises.stdout; | ||
err.stderr = await promises.stderr; | ||
throw err; | ||
} | ||
} | ||
|
||
async function runSubfont(...args) { | ||
const proc = childProcess.spawn( | ||
pathModule.resolve(__dirname, '..', 'lib', 'cli.js'), | ||
args | ||
); | ||
|
||
const promises = { | ||
exit: new Promise((resolve, reject) => { | ||
proc.on('error', reject).on('exit', exitCode => { | ||
if (exitCode === 0) { | ||
resolve(); | ||
} else { | ||
const err = new Error(`Child process exited with ${exitCode}`); | ||
err.exitCode = exitCode; | ||
reject(err); | ||
} | ||
}); | ||
}), | ||
stdin: new Promise((resolve, reject) => { | ||
proc.stdin.on('error', reject).on('close', resolve); | ||
}), | ||
stdout: consumeStream(proc.stdout), | ||
stderr: consumeStream(proc.stderr) | ||
}; | ||
|
||
proc.stdin.end(); | ||
|
||
let err; | ||
try { | ||
await Promise.all(Object.values(promises)); | ||
} catch (_err) { | ||
err = _err; | ||
} | ||
return { | ||
err, | ||
stdout: (await promises.stdout).toString('utf-8'), | ||
stderr: (await promises.stderr).toString('utf-8') | ||
}; | ||
} | ||
|
||
describe('cli', function() { | ||
it('should display usage info if --help is passed', async function() { | ||
const { err, stdout } = await runSubfont('--help'); | ||
expect(err, 'to be falsy'); | ||
expect(stdout, 'to contain', 'Options:'); | ||
expect(stdout, 'not to contain', 'No input files'); | ||
}); | ||
|
||
it('should display usage info if an error is encountered', async function() { | ||
const { err, stderr } = await runSubfont('i-do-not-exist.html'); | ||
expect(err, 'to have property', 'exitCode', 1); | ||
expect(stderr, 'to contain', 'Options:'); | ||
}); | ||
|
||
it('should a wrong usage error without a stack trace', async function() { | ||
const { err, stderr } = await runSubfont('https://example.com'); | ||
expect(err, 'to have property', 'exitCode', 1); | ||
expect( | ||
stderr, | ||
'to contain', | ||
'--output has to be specified when using non-file input urls' | ||
); | ||
expect(stderr, 'not to match', /^\s+at/m); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters