Skip to content

Commit

Permalink
Fix precompile bin TypeError: name.replace is not a function
Browse files Browse the repository at this point in the history
fixes #1295
  • Loading branch information
fdintino committed Jul 9, 2020
1 parent 1736334 commit 7087fa9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Changelog
[#1279](https://github.com/mozilla/nunjucks/pull/1279); fixes
[#282](https://github.com/mozilla/nunjucks/issues/282). Thanks
[ogonkov](https://github.com/ogonkovv)!

* Fix precompile binary script `TypeError: name.replace is not a function`.
Fixes [#1295](https://github.com/mozilla/nunjucks/issues/1295).

3.2.1 (Mar 17 2020)
-------------------
Expand Down
28 changes: 17 additions & 11 deletions bin/precompile
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#!/usr/bin/env node
var commander = require('commander');
var {program} = require('commander');
var precompile = require('../src/precompile').precompile;
var Environment = require('../src/environment').Environment;
var lib = require('../src/lib');

var cmdpath = null;

commander
program
.storeOptionsAsProperties(false)
.passCommandToAction(false);

program
.name('precompile')
.usage('[-f|--force] [-a|--filters <filters>] [-n|--name <name>] [-i|--include <regex>] [-x|--exclude <regex>] [-w|--wrapper <wrapper>] <path>')
.arguments('<path>')
Expand All @@ -27,26 +31,28 @@ function concat(value, previous) {
}

if (cmdpath == null) {
commander.outputHelp();
program.outputHelp();
console.error('\nerror: no path given');
process.exit(1);
}

var env = new Environment([]);

lib.each([].concat(commander.filters).join(',').split(','), function (name) {
const opts = program.opts();

lib.each([].concat(opts.filters).join(',').split(','), function (name) {
env.addFilter(name.trim(), function () {}, true);
});

if (commander.wrapper) {
commander.wrapper = require('nunjucks-' + commander.wrapper).wrapper;
if (opts.wrapper) {
opts.wrapper = require('nunjucks-' + opts.wrapper).wrapper;
}

console.log(precompile(cmdpath, {
env : env,
force : commander.force,
name : commander.name,
wrapper: commander.wrapper,
include : [].concat(commander.include),
exclude : [].concat(commander.exclude)
force : opts.force,
name : opts.name,
wrapper: opts.wrapper,
include : [].concat(opts.include),
exclude : [].concat(opts.exclude)
}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"a-sync-waterfall": "^1.0.0",
"asap": "^2.0.3",
"commander": "^3.0.2"
"commander": "^5.1.0"
},
"browser": "./browser/nunjucks.js",
"devDependencies": {
Expand Down
40 changes: 29 additions & 11 deletions scripts/lib/runtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@ var utils = require('./utils');
var lookup = utils.lookup;
var promiseSequence = utils.promiseSequence;

function mochaRun() {
function mochaRun({cliTest = false} = {}) {
// We need to run the cli test without nyc because of weird behavior
// with spawn-wrap
const bin = lookup((cliTest) ? '.bin/mocha' : '.bin/nyc', true);
const runArgs = (cliTest)
? []
: [
'--require', '@babel/register',
'--exclude',
'tests/**',
'--silent',
'--no-clean',
require.resolve('mocha/bin/mocha'),
];

const mochaArgs = (cliTest)
? ['tests/cli.js']
: ['--grep', 'precompile cli', '--invert', 'tests'];

return new Promise((resolve, reject) => {
try {
const proc = spawn(lookup('.bin/nyc', true), [
'--require', '@babel/register',
'--exclude',
'tests/**',
'--silent',
'--no-clean',
require.resolve('mocha/bin/mocha'),
const proc = spawn(bin, [
...runArgs,
'-R', 'spec',
'-r', 'tests/setup',
'-r', '@babel/register',
'tests'
...mochaArgs,
], {
cwd: path.join(__dirname, '../..'),
env: process.env
Expand All @@ -47,7 +60,13 @@ function mochaRun() {
function runtests() {
return new Promise((resolve, reject) => {
var server;
return mochaRun().then(() => {

const mochaPromise = promiseSequence([
() => mochaRun({cliTest: false}),
() => mochaRun({cliTest: true}),
]);

return mochaPromise.then(() => {
return getStaticServer().then((args) => {
server = args[0];
const port = args[1];
Expand All @@ -68,4 +87,3 @@ function runtests() {
}

module.exports = runtests;

48 changes: 48 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(function() {
'use strict';

var path = require('path');
var execFile = require('child_process').execFile;
var expect = require('expect.js');

var rootDir = path.resolve(path.join(__dirname, '..'));
var precompileBin = path.join(rootDir, 'bin', 'precompile');

if (process.platform === 'win32') {
precompileBin += '.cmd';
}

function execPrecompile(args, cb) {
execFile(precompileBin, args, {cwd: rootDir}, cb);
}

describe('precompile cli', function() {
it('should echo a compiled template to stdout', function(done) {
execPrecompile(['tests/templates/item.njk'], function(err, stdout, stderr) {
if (err) {
done(err);
return;
}
expect(stdout).to.contain('window.nunjucksPrecompiled');
expect(stderr).to.equal('');
done();
});
});

it('should support --name', function(done) {
var args = [
'--name', 'item.njk',
'tests/templates/item.njk',
];
execPrecompile(args, function(err, stdout, stderr) {
if (err) {
done(err);
return;
}
expect(stdout).to.contain('"item.njk"');
expect(stderr).to.equal('');
done();
});
});
});
}());

0 comments on commit 7087fa9

Please sign in to comment.