@rse/nunjucks-cli 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/nunjucks.js +57 -70
- package/package.json +2 -2
package/nunjucks.js
CHANGED
|
@@ -6,48 +6,41 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/* own information */
|
|
9
|
-
const my
|
|
9
|
+
const my = require("./package.json")
|
|
10
10
|
|
|
11
11
|
/* internal requirements */
|
|
12
|
-
const fs
|
|
13
|
-
const path
|
|
12
|
+
const fs = require("node:fs")
|
|
13
|
+
const path = require("node:path")
|
|
14
14
|
|
|
15
15
|
/* external requirements */
|
|
16
|
-
const
|
|
17
|
-
const chalk
|
|
18
|
-
const jsYAML
|
|
19
|
-
const nunjucks
|
|
16
|
+
const commander = require("commander")
|
|
17
|
+
const chalk = require("chalk")
|
|
18
|
+
const jsYAML = require("js-yaml")
|
|
19
|
+
const nunjucks = require("nunjucks")
|
|
20
20
|
|
|
21
21
|
/* parse command-line arguments */
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.
|
|
34
|
-
.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
.
|
|
41
|
-
.
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
.demand(0)
|
|
45
|
-
.parserConfiguration({
|
|
46
|
-
"short-option-groups": true,
|
|
47
|
-
"parse-numbers": true,
|
|
48
|
-
"boolean-negation": true
|
|
49
|
-
})
|
|
50
|
-
.parse(process.argv.slice(2))
|
|
22
|
+
const program = new commander.Command()
|
|
23
|
+
program.name("nunjucks")
|
|
24
|
+
.description("Nunjucks Template Rendering Command-Line Interface")
|
|
25
|
+
.showHelpAfterError("hint: use option --help for usage information")
|
|
26
|
+
.option("-h, --help", "show usage help", false)
|
|
27
|
+
.option("-V, --version", "show program version information", false)
|
|
28
|
+
.option("-c, --config <config-file>", "load Nunjucks configuration YAML file", "")
|
|
29
|
+
.option("-C, --option <key>=<value>", "set Nunjucks configuration option", (v, l) => l.concat([ v ]), [])
|
|
30
|
+
.option("-d, --defines <context-file>", "load context definition YAML file", "")
|
|
31
|
+
.option("-D, --define <key>=<value>", "set context definition key/value", (v, l) => l.concat([ v ]), [])
|
|
32
|
+
.option("-e, --extension <module-name>", "load Nunjucks JavaScript extension module", (v, l) => l.concat([ v ]), [])
|
|
33
|
+
.option("-o, --output <output-file>", "save output file", "-")
|
|
34
|
+
.argument("[<input-file>]", "input file")
|
|
35
|
+
program.parse(process.argv)
|
|
36
|
+
const argv = { ...program.opts(), _: program.args }
|
|
37
|
+
|
|
38
|
+
/* handle special help request */
|
|
39
|
+
if (argv.help) {
|
|
40
|
+
console.log(program.helpInformation())
|
|
41
|
+
console.log("Example:\n $ echo \"Hello, {{ who }}!\" | nunjucks -Dwho=World -\n")
|
|
42
|
+
process.exit(0)
|
|
43
|
+
}
|
|
51
44
|
|
|
52
45
|
/* handle special version request */
|
|
53
46
|
if (argv.version) {
|
|
@@ -60,11 +53,11 @@ if (argv.version) {
|
|
|
60
53
|
|
|
61
54
|
/* read input file */
|
|
62
55
|
let input = ""
|
|
63
|
-
if (argv._.length
|
|
64
|
-
console.error(chalk.red("nunjucks: ERROR:
|
|
56
|
+
if (argv._.length > 1) {
|
|
57
|
+
console.error(chalk.red("nunjucks: ERROR: invalid number of arguments (zero or one input file expected)"))
|
|
65
58
|
process.exit(1)
|
|
66
59
|
}
|
|
67
|
-
let inputFile = argv._[0]
|
|
60
|
+
let inputFile = argv._[0] ?? "-"
|
|
68
61
|
if (inputFile === "-") {
|
|
69
62
|
inputFile = "<stdin>"
|
|
70
63
|
process.stdin.setEncoding("utf-8")
|
|
@@ -109,14 +102,12 @@ if (argv.defines) {
|
|
|
109
102
|
context.env = process.env
|
|
110
103
|
|
|
111
104
|
/* add context defines */
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
})
|
|
119
|
-
}
|
|
105
|
+
argv.define.forEach((define) => {
|
|
106
|
+
let [ , key, val ] = define.match(/^([^=]+)(?:=(.*))?$/)
|
|
107
|
+
if (val === undefined)
|
|
108
|
+
val = true
|
|
109
|
+
context[key] = val
|
|
110
|
+
})
|
|
120
111
|
|
|
121
112
|
/* determine Nunjucks options */
|
|
122
113
|
let options = {}
|
|
@@ -129,10 +120,8 @@ if (argv.config) {
|
|
|
129
120
|
process.exit(1)
|
|
130
121
|
}
|
|
131
122
|
}
|
|
132
|
-
if (argv.
|
|
133
|
-
|
|
134
|
-
options = Object.assign(options, argv.options)
|
|
135
|
-
}
|
|
123
|
+
if (argv.option.length > 0)
|
|
124
|
+
options = Object.assign(options, argv.option)
|
|
136
125
|
options = Object.assign({}, {
|
|
137
126
|
autoescape: false,
|
|
138
127
|
throwOnUndefined: false,
|
|
@@ -146,28 +135,26 @@ options = Object.assign({}, {
|
|
|
146
135
|
const env = nunjucks.configure(inputFile, options)
|
|
147
136
|
|
|
148
137
|
/* load external extension files */
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
modpath = require.resolve(extension)
|
|
155
|
-
}
|
|
156
|
-
catch (ex) {
|
|
157
|
-
modpath = null
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (modpath === null) {
|
|
161
|
-
console.error(chalk.red(`nunjucks: ERROR: failed to find extension module: ${extension}`))
|
|
162
|
-
process.exit(1)
|
|
138
|
+
for (const extension of argv.extension) {
|
|
139
|
+
let modpath = path.resolve(extension)
|
|
140
|
+
if (!fs.existsSync(modpath)) {
|
|
141
|
+
try {
|
|
142
|
+
modpath = require.resolve(extension)
|
|
163
143
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${modpath}`))
|
|
167
|
-
process.exit(1)
|
|
144
|
+
catch (ex) {
|
|
145
|
+
modpath = null
|
|
168
146
|
}
|
|
169
|
-
mod(env)
|
|
170
147
|
}
|
|
148
|
+
if (modpath === null) {
|
|
149
|
+
console.error(chalk.red(`nunjucks: ERROR: failed to find extension module: ${extension}`))
|
|
150
|
+
process.exit(1)
|
|
151
|
+
}
|
|
152
|
+
const mod = require(modpath)
|
|
153
|
+
if (!(mod !== null && typeof mod === "function")) {
|
|
154
|
+
console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${modpath}`))
|
|
155
|
+
process.exit(1)
|
|
156
|
+
}
|
|
157
|
+
mod(env)
|
|
171
158
|
}
|
|
172
159
|
|
|
173
160
|
/* render Nunjucks template */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rse/nunjucks-cli",
|
|
3
3
|
"publishConfig": { "access": "public" },
|
|
4
|
-
"version": "1.4.
|
|
4
|
+
"version": "1.4.1",
|
|
5
5
|
"description": "Nunjucks Template Rendering Command-Line Interface",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dr. Ralf S. Engelschall",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"nunjucks": "3.2.4",
|
|
24
24
|
"chalk": "4.1.0",
|
|
25
|
-
"
|
|
25
|
+
"commander": "11.0.0",
|
|
26
26
|
"js-yaml": "4.1.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|