diff --git a/bin/cmd.js b/bin/cmd.js index ca39b1d..472087e 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -3,59 +3,67 @@ var doctest = require('../lib/doctest'); +var defaults = require('../src/defaults.js'); + 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 = [ '**/node_modules/**', '**/bower_components/**' ]; -function displayHelp () { +const FAIL = 1; +const SUCCESS = 0; + +function displayHelp() { const helpText = [ 'Usage: markdown-doctest [glob]', 'Options:', - ' -h, --help output help text' + ' -h, --help output help text', + ' -i, --init initialize setup file' ]; console.log(helpText.join('\n')); } -function main () { - var userGlob = process.argv[2]; - var config = {require: {}}; +function main(args) { - if (process.argv.indexOf('--help') !== -1 || process.argv.indexOf('-h') !== -1) { - displayHelp(); + args = args || process.argv + + var userGlob = args[2]; + var config = { require: {} }; - process.exitCode = 0; + if (args.indexOf('--help') !== -1 || args.indexOf('-h') !== -1) { + displayHelp(); + process.exitCode = SUCCESS; + return; + } + if (userGlob == '--init' || userGlob == '-i') { + process.exitCode = defaults.createSetupFile() return; } - if (fs.existsSync(CONFIG_FILEPATH)) { - try { - config = require(CONFIG_FILEPATH); - } catch (e) { - console.log('Error running .markdown-doctest-setup.js:'); - console.error(e); - process.exitCode = 1; - return; + if(defaults.setupFileExists()) { + config = defaults.getSetupFile() + if(!config) { + process.exitCode = FAIL; + return } } - + var ignoredDirectories = config.ignore || []; glob( userGlob || DEFAULT_GLOB, - {ignore: DEFAULT_IGNORE.concat(ignoredDirectories)}, + { ignore: DEFAULT_IGNORE.concat(ignoredDirectories) }, run ); - function run (err, files) { + function run(err, files) { if (err) { console.trace(err); } @@ -69,7 +77,7 @@ function main () { var failures = results.filter(function (result) { return result.status === 'fail'; }); if (failures.length > 0) { - process.exitCode = 1; + process.exitCode = FAIL; } } } diff --git a/src/defaults.js b/src/defaults.js new file mode 100644 index 0000000..d844458 --- /dev/null +++ b/src/defaults.js @@ -0,0 +1,95 @@ +'use strict'; + +var fs = require('fs'); + +var CONFIG_FILEPATH = process.cwd() + '/.markdown-doctest-setup.js'; +var PACKAGE_FILEPATH = process.cwd() + '/package.json'; + +const FAIL = 1; +const SUCCESS = 0; + +const ERR_SETUP_FILE_READ_FAIL = 'Error running .markdown-doctest-setup.js:'; +const ERR_SETUP_FILE_ALREADY_EXISTS = 'Error creating .markdown-doctest-setup.js: File already exists\n'; +const ERR_SETUP_FILE_CREATE_FAIL = 'Error creating .markdown-doctest-setup.js:'; +const ERR_PACKAGE_FILE_DOESNT_EXIST = 'Error finding package.json\n'; +const ERR_PACKAGE_FILE_READ_FAIL = 'Error reading package.json:'; +const SETUP_FILE_CREATED = 'Successfully created .markdown-doctest-setup.js with default values\n'; + + +function defaultSetup(name) { + var content = +` +const packageName = require('.'); + +module.exports = { + require: { + '${ name }' : packageName + }, + globals: { + '${ name }' : packageName + } +} + +` + return content; +} + +function createSetupFile() { + + if (setupFileExists()) { + console.log(ERR_SETUP_FILE_ALREADY_EXISTS); + return FAIL; + } + + if (!packageFileExists()) { + console.log(ERR_PACKAGE_FILE_DOESNT_EXIST) + return FAIL; + } + + // retrieve package name from package.json to put into defaults + var packageConfig; + + try { + packageConfig = require(PACKAGE_FILEPATH) + } catch (e) { + console.log(ERR_PACKAGE_FILE_READ_FAIL); + console.error(e); + return FAIL; + } + + try { + fs.writeFile( + CONFIG_FILEPATH, + defaultSetup(packageConfig.name) + ) + } catch (e) { + console.log(ERR_SETUP_FILE_CREATE_FAIL); + console.error(e); + return FAIL; + } + + console.log(SETUP_FILE_CREATED); + return SUCCESS; +} + +function packageFileExists() { + return fs.existsSync(PACKAGE_FILEPATH); +} + +function setupFileExists() { + return fs.existsSync(CONFIG_FILEPATH); +} + +function getSetupFile() { + + try { + return require(CONFIG_FILEPATH); + } catch (e) { + console.log(ERR_SETUP_FILE_READ_FAIL); + console.error(e); + return null; + } + +} + +module.exports = { createSetupFile, setupFileExists, getSetupFile } \ No newline at end of file