From b4f58f3ff7f9d7cb9a670ab48a1871459cdc6610 Mon Sep 17 00:00:00 2001 From: Eamonn O'Brien-Strain Date: Sun, 29 Dec 2019 16:08:22 -0800 Subject: [PATCH] Allow JavaScript views to have the .cjs suffix. --- bin/mustache | 3 ++- test/_files/cli.cjs | 3 +++ test/_files/cli.js | 3 +++ test/cli-test.js | 18 ++++++++++++++++++ test/render-helper.js | 5 +++-- 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/_files/cli.cjs create mode 100644 test/_files/cli.js diff --git a/bin/mustache b/bin/mustache index 2b2250a25..6db073f5d 100755 --- a/bin/mustache +++ b/bin/mustache @@ -131,7 +131,8 @@ function isStdin (view) { } function isJsFile (view) { - return path.extname(view) === '.js'; + var extension = path.extname(view); + return extension === '.js' || extension === '.cjs'; } function wasNotFound (err) { diff --git a/test/_files/cli.cjs b/test/_files/cli.cjs new file mode 100644 index 000000000..dc090e577 --- /dev/null +++ b/test/_files/cli.cjs @@ -0,0 +1,3 @@ +module.exports = { + name: 'LeBron' +}; diff --git a/test/_files/cli.js b/test/_files/cli.js new file mode 100644 index 000000000..dc090e577 --- /dev/null +++ b/test/_files/cli.js @@ -0,0 +1,3 @@ +module.exports = { + name: 'LeBron' +}; diff --git a/test/cli-test.js b/test/cli-test.js index 28e25023e..b40917986 100644 --- a/test/cli-test.js +++ b/test/cli-test.js @@ -76,6 +76,24 @@ describe('Mustache CLI', function () { }); }); + it('can handle view written in JavaScript with .js suffix', function (done) { + exec('bin/mustache test/_files/cli.js test/_files/cli.mustache', function (err, stdout, stderr) { + assert.equal(err, null); + assert.equal(stderr, ''); + assert.equal(stdout, expectedOutput); + done(); + }); + }); + + it('can handle view written in JavaScript with .cjs suffix', function (done) { + exec('bin/mustache test/_files/cli.cjs test/_files/cli.mustache', function (err, stdout, stderr) { + assert.equal(err, null); + assert.equal(stderr, ''); + assert.equal(stdout, expectedOutput); + done(); + }); + }); + it('writes rendered template into the file specified by the third argument', function (done) { var outputFile = 'test/_files/cli_output.txt'; exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function (err, stdout, stderr) { diff --git a/test/render-helper.js b/test/render-helper.js index 4b70480ac..1f25dcf1e 100644 --- a/test/render-helper.js +++ b/test/render-helper.js @@ -13,6 +13,7 @@ function getContents (testName, ext) { function getView (testName) { var view = getContents(testName, 'js'); + if (!view) view = getContents(testName, 'cjs'); if (!view) throw new Error('Cannot find view for test "' + testName + '"'); return view; } @@ -34,9 +35,9 @@ if (testToRun) { testNames = testToRun.split(','); } else { testNames = fs.readdirSync(_files).filter(function (file) { - return (/\.js$/).test(file); + return (/\.c?js$/).test(file); }).map(function (file) { - return path.basename(file).replace(/\.js$/, ''); + return path.basename(file).replace(/\.c?js$/, ''); }); }