Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init flag creates default setup file #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
}
}
Expand Down
95 changes: 95 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -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 }