| 1 |
#!/usr/bin/env node |
| 2 |
var {program} = require('commander'); |
| 3 |
var precompile = require('../src/precompile').precompile; |
| 4 |
var Environment = require('../src/environment').Environment; |
| 5 |
var lib = require('../src/lib'); |
| 6 |
|
| 7 |
var cmdpath = null; |
| 8 |
|
| 9 |
program |
| 10 |
.storeOptionsAsProperties(false) |
| 11 |
.passCommandToAction(false); |
| 12 |
|
| 13 |
program |
| 14 |
.name('precompile') |
| 15 |
.usage('[-f|--force] [-a|--filters <filters>] [-n|--name <name>] [-i|--include <regex>] [-x|--exclude <regex>] [-w|--wrapper <wrapper>] <path>') |
| 16 |
.arguments('<path>') |
| 17 |
.helpOption('-?, -h, --help', 'Display this help message') |
| 18 |
.option('-f, --force', 'Force compilation to continue on error') |
| 19 |
.option('-a, --filters <filters>', 'Give the compiler a comma-delimited list of asynchronous filters, required for correctly generating code') |
| 20 |
.option('-n, --name <name>', 'Specify the template name when compiling a single file') |
| 21 |
.option('-i, --include <regex>', 'Include a file or folder which match the regex but would otherwise be excluded. You can use this flag multiple times', concat, ['\\.html$', '\\.jinja$']) |
| 22 |
.option('-x, --exclude <regex>', 'Exclude a file or folder which match the regex but would otherwise be included. You can use this flag multiple times', concat, []) |
| 23 |
.option('-w, --wrapper <wrapper>', 'Load a external plugin to change the output format of the precompiled templates (for example, "-w custom" will load a module named "nunjucks-custom")') |
| 24 |
.action(function (path) { |
| 25 |
cmdpath = path; |
| 26 |
}) |
| 27 |
.parse(process.argv); |
| 28 |
|
| 29 |
function concat(value, previous) { |
| 30 |
return previous.concat(value); |
| 31 |
} |
| 32 |
|
| 33 |
if (cmdpath == null) { |
| 34 |
program.outputHelp(); |
| 35 |
console.error('\nerror: no path given'); |
| 36 |
process.exit(1); |
| 37 |
} |
| 38 |
|
| 39 |
var env = new Environment([]); |
| 40 |
|
| 41 |
const opts = program.opts(); |
| 42 |
|
| 43 |
lib.each([].concat(opts.filters).join(',').split(','), function (name) { |
| 44 |
env.addFilter(name.trim(), function () {}, true); |
| 45 |
}); |
| 46 |
|
| 47 |
if (opts.wrapper) { |
| 48 |
opts.wrapper = require('nunjucks-' + opts.wrapper).wrapper; |
| 49 |
} |
| 50 |
|
| 51 |
console.log(precompile(cmdpath, { |
| 52 |
env : env, |
| 53 |
force : opts.force, |
| 54 |
name : opts.name, |
| 55 |
wrapper: opts.wrapper, |
| 56 |
include : [].concat(opts.include), |
| 57 |
exclude : [].concat(opts.exclude) |
| 58 |
})); |