Skip to content

Commit

Permalink
fixed watch command; refactored default command to use wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Sep 6, 2014
1 parent 8721064 commit 5367749
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var path = require('path');
var fs = require('fs');
var program = require('commander');
var pluck = require('../lib/pluck');
var Cleaver;
var Cleaver = require('../');

/**
* Helper function to use the cleaver API to create and save a new
Expand All @@ -13,6 +13,7 @@ var Cleaver;
function createAndSave(files, options) {
files.forEach(function (file) {
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) throw err;
var presentation = new Cleaver(contents, options, path.resolve(path.dirname(file)));

presentation.run()
Expand All @@ -35,6 +36,15 @@ program
.version(require('../package.json').version)
.usage('[command] [file]');


/**
* Command to watch for changes on a given file
* This command only uses the first argument passed in, meaning you cannot
* watch multiple files.
*
* Usage:
* cleaver watch file --some-options
*/
program
.command('watch')
.description('Watch for changes on markdown file')
Expand All @@ -49,6 +59,44 @@ program
});
});


/**
* The default command, running cleaver on one or more files.
*
* Usage:
* cleaver file1 file2 --some-options
*/
program
.command('*')
.description('')
.action(function () {
/**
* Extract the files out of `program.args`. This is a total workaround for
* commander.
*/
var files = program.args.filter(function (arg) {
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);
});

program
.option('--title <title>', 'The title of the rendered document ' +
'(default: Untitled')
Expand Down Expand Up @@ -78,22 +126,4 @@ var parsedOpts = pluck(program, [
if (!program.args.length) {
// TODO: custom help screen
program.help();
} else {
/**
* 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 = '*';
}

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

0 comments on commit 5367749

Please sign in to comment.