Browse files

Fixes for variable scope and wildcard issues in Hulk

1 parent c421bca commit 26a4f05ce4347f3c3ce2f9aee5c7ced3fa208d57 @kruegerb kruegerb committed Aug 31, 2012
Showing with 13 additions and 4 deletions.
  1. +13 −4 bin/hulk
View
17 bin/hulk
@@ -15,13 +15,15 @@
* limitations under the License.
*/
+
// dependencies
var hogan = require('../lib/hogan.js')
, path = require('path')
, nopt = require('nopt')
, mkderp = require('mkdirp')
, fs = require('fs');
+
// locals
var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\']
, specialsRegExp = new RegExp('(\\' + specials.join('|\\') + ')', 'g')
@@ -43,6 +45,7 @@ var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}',
}
, templates;
+
// options
options = nopt(options, shortHand);
@@ -88,15 +91,16 @@ function extractFiles(args) {
return files = files.concat(
fs.readdirSync(arg[0] || '.')
.map(function (f) {
- return new RegExp(esc(arg[1]) + '$').test(f) && path.join(arg[0], f);
+ var file = path.join(arg[0], f);
+ return new RegExp(esc(arg[1]) + '$').test(f) && fs.statSync(file).isFile() && file;
})
.filter(function (f) {
return f;
})
);
}
- files.push(arg);
+ if (fs.statSync(arg).isFile()) files.push(arg);
})
@@ -126,16 +130,19 @@ function wrap(file, name, openedFile) {
}
}
+
// write the directory
if (options.outputdir) {
mkderp.sync(options.outputdir);
}
+
// Prepend namespace to template name
function namespace(name) {
return (options.namespace || '') + name;
}
+
// write a template foreach file that matches template extension
templates = extractFiles(options.argv.remain)
.map(function (file) {
@@ -145,7 +152,9 @@ templates = extractFiles(options.argv.remain)
openedFile = removeByteOrderMark(openedFile.trim());
openedFile = wrap(file, name, openedFile);
if (!options.outputdir) return openedFile;
- fs.writeFileSync(path.join(options.outputdir, name + '.js'), openedFile);
+ var vn = options.variable || 'templates';
+ fs.writeFileSync(path.join(options.outputdir, name + '.js')
+ , 'if (!!!' + vn + ') var ' + vn + ' = {};\n' + openedFile);
})
.filter(function (t) {
return t;
@@ -156,6 +165,6 @@ templates = extractFiles(options.argv.remain)
if (!templates.length || options.outputdir) process.exit(0);
if (!options.wrapper) {
var vn = options.variable || 'templates';
- console.log('if (!!!' + vn + ') ' + vn + ' = {};');
+ console.log('if (!!!' + vn + ') var ' + vn + ' = {};');
}
console.log(templates.join('\n'));

0 comments on commit 26a4f05

Please sign in to comment.