-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path83.js
More file actions
147 lines (128 loc) · 3.66 KB
/
Copy path83.js
File metadata and controls
147 lines (128 loc) · 3.66 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env node
var fs = require('fs')
var path = require('path')
var nunjucks = require('nunjucks')
var chokidar = require('chokidar')
var mkdirp = require('mkdirp')
var chalk = require('chalk')
var glob = require("glob")
var argv = require('yargs')
.usage('Usage: nunjucks <file|glob> [context] [options]')
.example('nunjucks foo.tpl data.json', 'Compile foo.tpl to dist/foo.html')
.example('nunjucks *.tpl -w -p src -o dist',
'Watch .tpl files in src folder, compile them to dist folder')
.demand(1)
.option('path', {
alias: 'p',
string: true,
requiresArg: true,
nargs: 1,
describe: 'Path where templates live'
})
.option('out', {
alias: 'o',
string: true,
requiresArg: true,
nargs: 1,
describe: 'Output folder'
})
.option('watch',{
alias: 'w',
boolean: true,
describe: 'Watch files change, except files starting by "_"'
})
.option('options', {
alias: 'O',
string: true,
requiresArg: true,
nargs: 1,
describe: 'Nunjucks options file'
})
.option('unsafe', {
alias: 'u',
boolean: true,
describe: 'Allow use of .html as source files'
})
.help()
.alias('help', 'h')
.epilogue('For more information on Nunjucks: https://mozilla.github.io/nunjucks/api.html')
.argv
// Set defaults
var opts = {}
opts.dirIn = argv.path || ''
opts.dirOut = argv.out || null
opts.nunjucks = (argv.options) ? JSON.parse(fs.readFileSync(argv.options, 'utf8')) : {
trimBlocks: true,
lstripBlocks: true,
noCache: true
}
// Set Nunjucks environnement
var env = nunjucks.configure(path.resolve(process.cwd(), opts.dirIn), opts.nunjucks)
// Parse second argument as data context if any
opts.context = (argv._[1]) ? JSON.parse(fs.readFileSync(argv._[1], 'utf8')) : {}
// Set glob options
opts.glob = {
strict: true,
cwd: path.resolve(process.cwd(), opts.dirIn),
ignore: '**/_*.*',
nonull: true
}
// Match glob pattern and render files accordingly
glob(argv._[0], opts.glob, function(err, files) {
if (err) return console.error(chalk.red(err))
renderAll(files, opts.context, opts.dirOut)
})
// Watcher
if (argv.watch) {
opts.chokidar = {
persistent: true,
cwd: path.resolve(process.cwd(), opts.dirIn),
awaitWriteFinish: {
stabilityThreshold: 300,
pollInterval: 50
}
}
var watcher = chokidar.watch(argv._[0], opts.chokidar)
var layouts = []
var templates = []
watcher.on('ready', function() {
console.log(chalk.gray('Watching templates...'))
})
// Sort files to not render partials/layouts
watcher.on('add', function(file) {
if (path.basename(file).indexOf('_') === 0) {
layouts.push(file)
} else {
templates.push(file)
}
})
// if the file is a layout/partial, render all other files instead
watcher.on('change', function(file) {
if (layouts.indexOf(file) > -1) {
renderAll(templates, opts.context, opts.dirOut)
} else {
render(file, opts.context, opts.dirOut)
}
})
}
// Render one file
function render(file, data, outputDir) {
if (!argv.unsafe && path.extname(file) === '.html')
return console.error(chalk.red(file + ': To use .html as source files, add --unsafe/-u flag'))
env.render(file, data, function(err, res) {
if (err) return console.error(chalk.red(err))
var outputFile = file.replace(/\.\w+$/, '') + '.html'
if (outputDir) {
outputFile = path.resolve(outputDir, outputFile)
mkdirp.sync(path.dirname(outputFile))
}
console.log(chalk.blue('Rendering: ' + file))
fs.writeFileSync(outputFile, res)
})
}
// Render multiple files
function renderAll(files, data, outputDir) {
for (var i = 0; i < files.length; i++) {
render(files[i], data, outputDir)
}
}