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

Allow the CLI to use JavaScript views when the project has ES6 modules enabled. #733

Merged
merged 1 commit into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion bin/mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions test/_files/cli.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
name: 'LeBron'
};
3 changes: 3 additions & 0 deletions test/_files/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
name: 'LeBron'
};
18 changes: 18 additions & 0 deletions test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions test/render-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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$/, '');
});
}

Expand Down