Skip to content

Commit

Permalink
Support .js views with functions in CLI (#717)
Browse files Browse the repository at this point in the history
Fixes #429
  • Loading branch information
JEStaubach authored and phillipj committed Sep 13, 2019
1 parent 96cb5ef commit 13cde04
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
23 changes: 18 additions & 5 deletions bin/mustache
Expand Up @@ -53,11 +53,20 @@ function run (/*args*/) {
}

function readView (cb) {
var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg);

streamToStr(view, function onDone (str) {
cb(parseView(str));
});
var view;
if (isJsFile(viewArg)) {
view = require(path.join(process.cwd(),viewArg));
cb(view);
} else {
if (isStdin(viewArg)) {
view = process.openStdin();
} else {
view = fs.createReadStream(viewArg);
}
streamToStr(view, function onDone (str) {
cb(parseView(str));
});
}
}

function parseView (str) {
Expand Down Expand Up @@ -121,6 +130,10 @@ function isStdin (view) {
return view === '-';
}

function isJsFile (view) {
return path.extname(view) === '.js';
}

function wasNotFound (err) {
return err.code && err.code === 'ENOENT';
}
Expand Down
8 changes: 8 additions & 0 deletions test/_files/cli_js_view_with_function.js
@@ -0,0 +1,8 @@
module.exports = {
'name': 'Tater',
'bold': function () {
return function (text, render) {
return '<b>' + render(text) + '</b>';
};
}
};
1 change: 1 addition & 0 deletions test/_files/cli_js_view_with_function.mustache
@@ -0,0 +1 @@
{{#bold}}Hi {{name}}.{{/bold}}
1 change: 1 addition & 0 deletions test/_files/cli_js_view_with_function.txt
@@ -0,0 +1 @@
<b>Hi Tater.</b>

0 comments on commit 13cde04

Please sign in to comment.