From 13cde0442e3598bd7a12ade57ec42f77c1b44df8 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 13 Sep 2019 06:57:30 -0400 Subject: [PATCH] Support .js views with functions in CLI (#717) Fixes https://github.com/janl/mustache.js/issues/429 --- bin/mustache | 23 +++++++++++++++---- test/_files/cli_js_view_with_function.js | 8 +++++++ .../_files/cli_js_view_with_function.mustache | 1 + test/_files/cli_js_view_with_function.txt | 1 + 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 test/_files/cli_js_view_with_function.js create mode 100644 test/_files/cli_js_view_with_function.mustache create mode 100644 test/_files/cli_js_view_with_function.txt diff --git a/bin/mustache b/bin/mustache index 430e8b937..2b2250a25 100755 --- a/bin/mustache +++ b/bin/mustache @@ -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) { @@ -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'; } diff --git a/test/_files/cli_js_view_with_function.js b/test/_files/cli_js_view_with_function.js new file mode 100644 index 000000000..29d1a2766 --- /dev/null +++ b/test/_files/cli_js_view_with_function.js @@ -0,0 +1,8 @@ +module.exports = { + 'name': 'Tater', + 'bold': function () { + return function (text, render) { + return '' + render(text) + ''; + }; + } +}; \ No newline at end of file diff --git a/test/_files/cli_js_view_with_function.mustache b/test/_files/cli_js_view_with_function.mustache new file mode 100644 index 000000000..a73ebbe6a --- /dev/null +++ b/test/_files/cli_js_view_with_function.mustache @@ -0,0 +1 @@ +{{#bold}}Hi {{name}}.{{/bold}} \ No newline at end of file diff --git a/test/_files/cli_js_view_with_function.txt b/test/_files/cli_js_view_with_function.txt new file mode 100644 index 000000000..3f6eb97b7 --- /dev/null +++ b/test/_files/cli_js_view_with_function.txt @@ -0,0 +1 @@ +Hi Tater. \ No newline at end of file