-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,52 @@ | ||
#!/usr/bin/env node | ||
var path = require('path'); | ||
var commander = require('commander'); | ||
var precompile = require('../src/precompile').precompile; | ||
var Environment = require('../src/environment').Environment; | ||
var lib = require('../src/lib'); | ||
|
||
var yargs = require('yargs') | ||
|
||
.usage('$0 [-f|--force] [-a|--filters <filters>] [-n|--name <name>] [-i|--include <regex>] [-x|--exclude <regex>] [-w|--wrapper <wrapper>] <path>') | ||
.wrap(80) | ||
|
||
.describe('help', 'Display this help message') | ||
.boolean('help') | ||
.alias('h', 'help') | ||
.alias('?', 'help') | ||
|
||
.describe('force', 'Force compilation to continue on error') | ||
.boolean('force') | ||
.alias('f', 'force') | ||
|
||
.describe('filters', 'Give the compiler a comma-delimited list of asynchronous filters, required for correctly generating code') | ||
.string('filters') | ||
.alias('a', 'filters') | ||
|
||
.describe('name', 'Specify the template name when compiling a single file') | ||
.string('name') | ||
.alias('n', 'n') | ||
|
||
.describe('include', 'Include a file or folder which match the regex but would otherwise be excluded. You can use this flag multiple times') | ||
.string('include' ) | ||
.default('include', ['\\.html$', '\\.jinja$']) | ||
.alias('i', 'include') | ||
|
||
.describe('exclude', 'Exclude a file or folder which match the regex but would otherwise be included. You can use this flag multiple times') | ||
.string('exclude' ) | ||
.default('exclude', []) | ||
.alias('x', 'exclude') | ||
|
||
.describe('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")') | ||
.string('wrapper') | ||
.alias('w', 'wrapper') | ||
|
||
.demand(1); | ||
|
||
var argv = yargs.argv; | ||
var cmdpath = null; | ||
|
||
commander | ||
.name('precompile') | ||
.usage('[-f|--force] [-a|--filters <filters>] [-n|--name <name>] [-i|--include <regex>] [-x|--exclude <regex>] [-w|--wrapper <wrapper>] <path>') | ||
.arguments('<path>') | ||
.helpOption('-?, -h, --help', 'Display this help message') | ||
.option('-f, --force', 'Force compilation to continue on error') | ||
.option('-a, --filters <filters>', 'Give the compiler a comma-delimited list of asynchronous filters, required for correctly generating code') | ||
.option('-n, --name <name>', 'Specify the template name when compiling a single file') | ||
.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$']) | ||
.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, []) | ||
.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")') | ||
.action(function (path) { | ||
cmdpath = path; | ||
}) | ||
.parse(process.argv); | ||
|
||
function concat(value, previous) { | ||
return previous.concat(value); | ||
} | ||
|
||
if (argv.help) { | ||
yargs.showHelp(); | ||
process.exit(1); | ||
if (cmdpath == null) { | ||
commander.outputHelp(); | ||
console.error('\nerror: no path given'); | ||
process.exit(1); | ||
} | ||
|
||
var env = new Environment([]); | ||
|
||
lib.each([].concat(argv.filters).join(',').split(','), function(name) { | ||
env.addFilter(name.trim(), function() {}, true); | ||
lib.each([].concat(commander.filters).join(',').split(','), function (name) { | ||
env.addFilter(name.trim(), function () {}, true); | ||
}); | ||
|
||
if(argv.wrapper) { | ||
argv.wrapper = require('nunjucks-' + argv.wrapper).wrapper; | ||
if (commander.wrapper) { | ||
commander.wrapper = require('nunjucks-' + commander.wrapper).wrapper; | ||
} | ||
|
||
console.log(precompile(argv._[0], { | ||
env : env, | ||
force : argv.force, | ||
name : argv.name, | ||
wrapper: argv.wrapper, | ||
|
||
include : [].concat(argv.include), | ||
exclude : [].concat(argv.exclude) | ||
console.log(precompile(cmdpath, { | ||
env : env, | ||
force : commander.force, | ||
name : commander.name, | ||
wrapper: commander.wrapper, | ||
include : [].concat(commander.include), | ||
exclude : [].concat(commander.exclude) | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters