Skip to content
Browse files

Removed --watch support (use a makefile....)

  • Loading branch information...
1 parent 7cbe8fd commit 09b7abcf7d7c487a4492fb54e2a3f3eb8c1810e2 @tj tj committed
Showing with 174 additions and 187 deletions.
  1. +174 −187 bin/jade
View
361 bin/jade
@@ -5,6 +5,7 @@
*/
var fs = require('fs')
+ , program = require('commander')
, path = require('path')
, resolve = path.resolve
, basename = path.basename
@@ -17,190 +18,176 @@ try {
jade = require('jade');
}
-/**
- * Arguments.
- */
-
-var args = process.argv.slice(2);
-
-/**
- * Options javascript.
- */
-
-var options = {};
-
-/**
- * Destination dir.
- */
-
-var dest;
-
-/**
- * Watcher hash.
- */
-
-var watchers;
-
-/**
- * Usage information.
- */
-
-var usage = ''
- + '\n'
- + ' Usage: jade [options]\n'
- + ' [path ...]\n'
- + ' < in.jade > out.jade'
- + ' \n'
- + ' Options:\n'
- + ' -o, --options <str> JavaScript options object passed\n'
- + ' -h, --help Output help information\n'
- + ' -w, --watch Watch file(s) or folder(s) for changes and re-compile\n'
- + ' -v, --version Output jade version\n'
- + ' --out <dir> Output the compiled html to <dir>\n';
- + '\n';
-
-// Parse arguments
-
-var arg
- , files = [];
-while (args.length) {
- arg = args.shift();
- switch (arg) {
- case '-h':
- case '--help':
- console.log(usage);
- process.exit(1);
- case '-v':
- case '--version':
- console.log(jade.version);
- process.exit(1);
- case '-o':
- case '--options':
- var str = args.shift();
- if (str) {
- options = eval('(' + str + ')');
- } else {
- console.error('-o, --options requires a string.');
- process.exit(1);
- }
- break;
- case '-w':
- case '--watch':
- watchers = {};
- break;
- case '--out':
- dest = args.shift();
- break;
- default:
- files.push(arg);
- }
-}
-
-// Watching and no files passed - watch cwd
-if (watchers && !files.length) {
- fs.readdirSync(process.cwd()).forEach(processFile);
-// Process passed files
-} else if (files.length) {
- files.forEach(processFile);
-// Stdio
-} else {
- var buf = '';
- process.stdin.setEncoding('utf8');
- process.stdin.on('data', function(chunk){
- buf += chunk;
- }).on('end', function(){
- console.log(jade.render(buf, options));
- }).resume();
-}
-
-/**
- * Process the given path, compiling the jade files found.
- * Always walk the subdirectories.;
- */
-
-function processFile(path) {
- fs.lstat(path, function(err, stat) {
- if (err) throw err;
- // Found jade file
- if (stat.isFile() && path.match(/\.jade$/)) {
- renderJade(path);
- // Found directory
- } else if (stat.isDirectory()) {
- fs.readdir(path, function(err, files) {
- if (err) throw err;
- files.map(function(filename) {
- return path + '/' + filename;
- }).forEach(processFile);
- });
- }
- });
-}
-
-/**
- * Render jade
- */
-
-function renderJade(jadefile) {
- jade.renderFile(jadefile, options, function(err, html) {
- if (err) throw err;
- writeFile(jadefile, html);
- });
-}
-
-/**
- * mkdir -p implementation.
- */
-
-function mkdirs(path, fn) {
- var segs = dirname(path).split('/')
- , dir = '';
-
- (function next() {
- var seg = segs.shift();
- if (seg) {
- dir += seg + '/';
- fs.mkdir(dir, 0755, function(err){
- if (!err) return next();
- if ('EEXIST' == err.code) return next();
- });
- } else {
- fn();
- }
- })();
-}
-
-/**
- * Write the html output to a file.
- */
-
-function writeFile(src, html) {
- var path = src.replace('.jade', '.html');
- if (dest) path = dest + '/' + path;
- mkdirs(path, function(err){
- if (err) throw err;
- fs.writeFile(path, html, function(err) {
- if (err) throw err;
- console.log(' \033[90mcompiled\033[0m %s', path);
- watch(src, renderJade);
- });
- });
-}
-
-/**
- * Watch the given `file` and invoke `fn` when modified.
- */
-
-function watch(file, fn) {
- // not watching
- if (!watchers) return;
-
- // already watched
- if (watchers[file]) return;
-
- // watch the file itself
- watchers[file] = true;
- console.log(' \033[90mwatching\033[0m %s', file);
- fs.watchFile(file, { interval: 50 }, function(curr, prev){
- if (curr.mtime > prev.mtime) fn(file);
- });
-}
+// options
+
+program
+ .version(jade.version)
+ .option('-o, --options <str>', 'javascript options object')
+ .options('-O, --out <dir>', 'output the compiled html to <dir>')
+ .parse(process.argv)
+
+
+//
+// /**
+// * Usage information.
+// */
+//
+// var usage = ''
+// + '\n'
+// + ' Usage: jade [options]\n'
+// + ' [path ...]\n'
+// + ' < in.jade > out.jade'
+// + ' \n'
+// + ' Options:\n'
+// + ' -o, --options <str> JavaScript options object passed\n'
+// + ' -h, --help Output help information\n'
+// + ' -w, --watch Watch file(s) or folder(s) for changes and re-compile\n'
+// + ' -v, --version Output jade version\n'
+// + ' --out <dir> Output the compiled html to <dir>\n';
+// + '\n';
+//
+// // Parse arguments
+//
+// var arg
+// , files = [];
+// while (args.length) {
+// arg = args.shift();
+// switch (arg) {
+// case '-h':
+// case '--help':
+// console.log(usage);
+// process.exit(1);
+// case '-v':
+// case '--version':
+// console.log(jade.version);
+// process.exit(1);
+// case '-o':
+// case '--options':
+// var str = args.shift();
+// if (str) {
+// options = eval('(' + str + ')');
+// } else {
+// console.error('-o, --options requires a string.');
+// process.exit(1);
+// }
+// break;
+// case '-w':
+// case '--watch':
+// watchers = {};
+// break;
+// case '--out':
+// dest = args.shift();
+// break;
+// default:
+// files.push(arg);
+// }
+// }
+//
+// // Watching and no files passed - watch cwd
+// if (watchers && !files.length) {
+// fs.readdirSync(process.cwd()).forEach(processFile);
+// // Process passed files
+// } else if (files.length) {
+// files.forEach(processFile);
+// // Stdio
+// } else {
+// var buf = '';
+// process.stdin.setEncoding('utf8');
+// process.stdin.on('data', function(chunk){
+// buf += chunk;
+// }).on('end', function(){
+// console.log(jade.render(buf, options));
+// }).resume();
+// }
+//
+// /**
+// * Process the given path, compiling the jade files found.
+// * Always walk the subdirectories.;
+// */
+//
+// function processFile(path) {
+// fs.lstat(path, function(err, stat) {
+// if (err) throw err;
+// // Found jade file
+// if (stat.isFile() && path.match(/\.jade$/)) {
+// renderJade(path);
+// // Found directory
+// } else if (stat.isDirectory()) {
+// fs.readdir(path, function(err, files) {
+// if (err) throw err;
+// files.map(function(filename) {
+// return path + '/' + filename;
+// }).forEach(processFile);
+// });
+// }
+// });
+// }
+//
+// /**
+// * Render jade
+// */
+//
+// function renderJade(jadefile) {
+// jade.renderFile(jadefile, options, function(err, html) {
+// if (err) throw err;
+// writeFile(jadefile, html);
+// });
+// }
+//
+// /**
+// * mkdir -p implementation.
+// */
+//
+// function mkdirs(path, fn) {
+// var segs = dirname(path).split('/')
+// , dir = '';
+//
+// (function next() {
+// var seg = segs.shift();
+// if (seg) {
+// dir += seg + '/';
+// fs.mkdir(dir, 0755, function(err){
+// if (!err) return next();
+// if ('EEXIST' == err.code) return next();
+// });
+// } else {
+// fn();
+// }
+// })();
+// }
+//
+// /**
+// * Write the html output to a file.
+// */
+//
+// function writeFile(src, html) {
+// var path = src.replace('.jade', '.html');
+// if (dest) path = dest + '/' + path;
+// mkdirs(path, function(err){
+// if (err) throw err;
+// fs.writeFile(path, html, function(err) {
+// if (err) throw err;
+// console.log(' \033[90mcompiled\033[0m %s', path);
+// watch(src, renderJade);
+// });
+// });
+// }
+//
+// /**
+// * Watch the given `file` and invoke `fn` when modified.
+// */
+//
+// function watch(file, fn) {
+// // not watching
+// if (!watchers) return;
+//
+// // already watched
+// if (watchers[file]) return;
+//
+// // watch the file itself
+// watchers[file] = true;
+// console.log(' \033[90mwatching\033[0m %s', file);
+// fs.watchFile(file, { interval: 50 }, function(curr, prev){
+// if (curr.mtime > prev.mtime) fn(file);
+// });
+// }

0 comments on commit 09b7abc

Please sign in to comment.
Something went wrong with that request. Please try again.