This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
/
bin.js
executable file
·63 lines (51 loc) · 1.81 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node
var commander = require('commander');
var chokidar = require('chokidar');
var insertassets = require('./index');
var path = require('path');
var pkg = require(path.join(__dirname, 'package.json'));
var DEFAULTS = {
source: null,
output: null,
dir: './',
watch: false,
pattern: '{dir}/{base}',
encoding: 'utf-8',
referenceGlobs: null,
inlineGlobs: null,
tags: '{{,}}',
};
function splitGlobs(globString) {
return globString.split(';');
}
commander
.version(pkg.version)
.option('-s, --source <path to template>', 'template file path, default: stdin')
.option('-o, --output <path>', 'result output path, default: stdout')
.option('-g, --reference-globs <globs...>', 'globs for files to be inject as references', splitGlobs)
.option('-G, --inline-globs <globs...>', 'globs for files to be inlined', splitGlobs)
.option('-d, --dir <assets folder>', 'injected assets directory, default: "./"')
.option('-p, --pattern <string>', 'use this pattern to generate paths, default {dir}/{base}')
.option('-t, --tags <string>', 'specify open/close tags. default: "{{,}}"')
.option('-w, --watch', 'run on every source file change')
.option('-e, --encoding <string>', 'read/write encoding, encoding "utf-8"');
commander.parse(process.argv);
var options = Object.keys(DEFAULTS).reduce(function(options, key){
options[key] = commander[key] || DEFAULTS[key];
return options;
}, {});
var run = insertassets.bind(null, options);
if (options.watch) {
var watchedGlobs = []
.concat(options.referenceGlobs)
.concat(options.inlineGlobs)
.filter(function identity(x) { return x;})
.map(prefixGlobWithDir);
chokidar.watch(watchedGlobs, {
ignoreInitial: true
}).on('all', run);
}
run();
function prefixGlobWithDir(glob){
return path.join(options.dir, glob);
}