Skip to content

Commit

Permalink
Fixed options parsing and debugging logic - fixes jdan#105
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Scales committed Feb 17, 2015
1 parent f3e7291 commit 00d4243
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 44 deletions.
36 changes: 16 additions & 20 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var program = require('commander');
var pluck = require('../lib/pluck');
var Cleaver = require('../');


/**
* Helper function to use the cleaver API to create and save a new
* presentation
Expand Down Expand Up @@ -35,6 +36,18 @@ function createAndSave(files, options) {
});
}


/**
* Exposes the options sent to the program as an object
*/
function getOptions() {
return pluck(program, [
'title', 'theme', 'style', 'output', 'controls', 'progress', 'encoding',
'template', 'layout', 'debug'
]);
}


program
.version(require('../package.json').version)
.usage('[command] [file]');
Expand All @@ -53,12 +66,12 @@ program
.description('Watch for changes on markdown file')
.action(function () {
var file = program.args[0];
createAndSave([file], parsedOpts);
createAndSave([file], getOptions());

console.log('Watching for changes on ' + file + '. Ctrl-C to abort.');
fs.watchFile(file, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
createAndSave([file], parsedOpts);
createAndSave([file], getOptions());
});
});

Expand All @@ -81,23 +94,10 @@ program
return typeof arg === 'string';
});

/**
* Enable debugging
*
* For finer-tuned debugging you can ignore the --debug flag and
* instead use the DEBUG environment variable that the `debug` module
* reads automatically.
*
* $ DEBUG=[cleaver,helper] cleaver path/to/presentation.md
*/
if (program.debug) {
process.env.DEBUG = '*';
}

// Reload cleaver with the new ENV for debugging
// TODO: This seems a little janky, maybe handle this in lib/
Cleaver = require('../');
createAndSave(files, parsedOpts);
createAndSave(files, getOptions());
});

program
Expand All @@ -122,10 +122,6 @@ program

program.parse(process.argv);

var parsedOpts = pluck(program, [
'title', 'theme', 'style', 'output', 'controls', 'progress', 'encoding',
'template', 'layout', 'debug']);

if (!program.args.length) {
// TODO: custom help screen
program.help();
Expand Down
41 changes: 26 additions & 15 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Q = require('q');
var debug = require('debug')('helper');
var debug = require('debug');
var http = require('http');
var https = require('https');
var fs = require('fs');
Expand Down Expand Up @@ -85,21 +85,23 @@ function load(map, options) {
options = options || {};

for (var key in map) {
if (!map[key] || map[key].length === 0) {
continue;
}
if (map.hasOwnProperty(key)) {
if (!map[key] || map[key].length === 0) {
continue;
}

filename = map[key];
if (!options.external) {
filename = path.resolve(ROOT_DIR, filename);
}
filename = map[key];
if (!options.external) {
filename = path.resolve(ROOT_DIR, filename);
}

promises.push(loadSingle(filename)
.then((function (_key) {
return function (data) {
loaded[_key] = data;
};
})(key)));
promises.push(loadSingle(filename)
.then((function (_key) {
return function (data) {
loaded[_key] = data;
};
})(key)));
}
}

map.loaded = loaded;
Expand Down Expand Up @@ -253,7 +255,16 @@ function getUserHome() {
}


module.exports = function (inputPath) {
module.exports = function (inputPath, shouldDebug) {
// We can't rely on process.env anymore, so we use a passed-in `shouldDebug`
// variable, and manually invoke `debug.enable()`.
// This is messy because typically debug would be called with `helper` in
// the require line, but after that point we lose access to `.enable()`.
if (shouldDebug) {
debug.enable('*');
}
debug = debug('helper');

// TODO: I bet we can simply all this nonsense with a chdir()
normalizePath = function (pathname) {
if (pathname[0] === '~') {
Expand Down
14 changes: 11 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Q = require('q');
var debug = require('debug')('cleaver');
var debug = require('debug');
var path = require('path');
var marked = require('marked');
var hljs = require('highlight.js');
Expand All @@ -24,11 +24,17 @@ function Cleaver(document, options, includePath) {
this.document = document.toString();
this.options = clone(options) || {};
this.path = path.resolve(includePath || '.');

if (this.options.debug) {
debug.enable('*');
}
debug = debug('cleaver');

/**
* Require helper using `this.path` as a context for where to locate any
* specified assets
*/
helper = require('./helper')(this.path);
helper = require('./helper')(this.path, this.options.debug);

this.templates = {
layout: 'templates/layout.mustache',
Expand Down Expand Up @@ -87,7 +93,9 @@ Cleaver.prototype.renderSlides = function () {
options = yaml.safeLoad(slices[0].content) || {};
// Load any options that are not already present from the constructor
for (i in options) {
this.options[i] = this.options[i] || options[i];
if (options.hasOwnProperty(i)) {
this.options[i] = this.options[i] || options[i];
}
}

debug('parsed options');
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cleaver",
"preferGlobal": true,
"version": "0.7.2",
"version": "0.7.3",
"author": "Jordan Scales <[email protected]>",
"description": "30-second slideshows for hackers",
"keywords": [
Expand All @@ -19,13 +19,13 @@
"url": "https://github.com/jdan/cleaver.git"
},
"dependencies": {
"mustache": "0.7.0",
"q": "0.9.6",
"js-yaml": "2.1.0",
"commander": "~2.1.0",
"debug": "^2.1.1",
"highlight.js": "~7.3.0",
"js-yaml": "2.1.0",
"marked": "~0.2.9",
"commander": "~2.1.0",
"debug": "~0.8.0"
"mustache": "0.7.0",
"q": "0.9.6"
},
"devDependencies": {
"jshint": "~2.3.0"
Expand Down

0 comments on commit 00d4243

Please sign in to comment.