-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Simonwep/master
Use commander as CLI Library and add --config option
- Loading branch information
Showing
4 changed files
with
85 additions
and
83 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,77 +1,73 @@ | ||
#! /usr/bin/env node | ||
'use strict'; | ||
|
||
var doctest = require('..'); | ||
|
||
var fs = require('fs'); | ||
|
||
var glob = require('glob'); | ||
|
||
var CONFIG_FILEPATH = process.cwd() + '/.markdown-doctest-setup.js'; | ||
var DEFAULT_GLOB = '**/*.+(md|markdown)'; | ||
var DEFAULT_IGNORE = [ | ||
const {version} = require('../package'); | ||
const doctest = require('..'); | ||
const program = require('commander'); | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const fs = require('fs'); | ||
|
||
const DEFAULT_GLOB = '**/*.+(md|markdown)'; | ||
const DEFAULT_IGNORE = [ | ||
'**/node_modules/**', | ||
'**/bower_components/**' | ||
]; | ||
|
||
function displayHelp () { | ||
const helpText = [ | ||
'Usage: markdown-doctest [glob]', | ||
'Options:', | ||
' -h, --help output help text' | ||
]; | ||
|
||
console.log(helpText.join('\n')); | ||
} | ||
|
||
function main () { | ||
var userGlob = process.argv[2]; | ||
var config = {require: {}}; | ||
|
||
if (process.argv.indexOf('--help') !== -1 || process.argv.indexOf('-h') !== -1) { | ||
displayHelp(); | ||
|
||
process.exitCode = 0; | ||
|
||
return; | ||
} | ||
|
||
if (fs.existsSync(CONFIG_FILEPATH)) { | ||
// Config | ||
const config = { | ||
require: {}, | ||
globals: {}, | ||
ignore: [] | ||
}; | ||
|
||
// Setup commander | ||
program | ||
.name('markdown-doctest') | ||
.description('Test all the code in your markdown docs!') | ||
.version(version, '-v, --version', 'output the current version') | ||
.helpOption('-h, --help', 'output usage informations') | ||
.option('-c, --config <path>', 'custom config location', path.join(process.cwd(), '/.markdown-doctest-setup.js')) | ||
.parse(process.argv); | ||
|
||
// Parse config file | ||
if (program.config) { | ||
const configPath = path.resolve(program.config); | ||
|
||
if (fs.existsSync(configPath)) { | ||
try { | ||
config = require(CONFIG_FILEPATH); | ||
|
||
// Apply custom settings | ||
Object.assign(config, require(configPath)); | ||
} catch (e) { | ||
console.log('Error running .markdown-doctest-setup.js:'); | ||
console.error(e); | ||
process.exitCode = 1; | ||
return; | ||
console.error(`Cannot resolve "${configPath}"`); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
var ignoredDirectories = config.ignore || []; | ||
|
||
glob( | ||
userGlob || DEFAULT_GLOB, | ||
{ignore: DEFAULT_IGNORE.concat(ignoredDirectories)}, | ||
run | ||
); | ||
// Resolve files | ||
glob( | ||
program.args[0] || DEFAULT_GLOB, | ||
{ | ||
ignore: [...config.ignore, ...DEFAULT_IGNORE] | ||
}, | ||
(err, files) => { | ||
|
||
function run (err, files) { | ||
if (err) { | ||
console.trace(err); | ||
} | ||
|
||
var results = doctest.runTests(files, config); | ||
// Run tests | ||
const results = doctest.runTests(files, config); | ||
|
||
console.log('\n'); | ||
|
||
doctest.printResults(results); | ||
|
||
var failures = results.filter(function (result) { return result.status === 'fail'; }); | ||
|
||
// Exit with error-code if any test failed | ||
const failures = results.filter(result => result.status === 'fail'); | ||
if (failures.length > 0) { | ||
process.exitCode = 1; | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
main(); | ||
); |
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 |
---|---|---|
|
@@ -1316,6 +1316,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422" | ||
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ== | ||
|
||
commander@^4.0.1: | ||
version "4.0.1" | ||
resolved "https://registry.yarnpkg.com/commander/-/commander-4.0.1.tgz#b67622721785993182e807f4883633e6401ba53c" | ||
integrity sha512-IPF4ouhCP+qdlcmCedhxX4xiGBPyigb8v5NeUp+0LyhwLgxMqyp3S0vl7TAPfS/hiP7FC3caI/PB9lTmP8r1NA== | ||
|
||
component-emitter@^1.2.1: | ||
version "1.3.0" | ||
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0" | ||
|