Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #429 - support .js views with functions. #717

Merged
merged 6 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions bin/mustache
Original file line number Diff line number Diff line change
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 (isJson(viewArg)) {
JEStaubach marked this conversation as resolved.
Show resolved Hide resolved
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 isJson (view) {
JEStaubach marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#bold}}Hi {{name}}.{{/bold}}
1 change: 1 addition & 0 deletions test/_files/cli_js_view_with_function.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<b>Hi Tater.</b>