@rse/nunjucks-cli 1.3.1 → 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/README.md +4 -1
- package/nunjucks.js +58 -62
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ Options
|
|
|
48
48
|
$ nunjucks
|
|
49
49
|
[-h|--help] [-V|--version]
|
|
50
50
|
[-c|--config <config-file>]
|
|
51
|
+
[-C|--option <key>=<value>]
|
|
51
52
|
[-d|--defines <context-file>]
|
|
52
53
|
[-D|--define <key>=<value>]
|
|
53
54
|
[-e|--extension <module-name>]
|
|
@@ -60,7 +61,9 @@ $ nunjucks
|
|
|
60
61
|
- `-V`|`--version`:<br/>
|
|
61
62
|
show program version information.
|
|
62
63
|
- `-c`|`--config` `<config-file>`:<br/>
|
|
63
|
-
load Nunjucks configuration YAML file
|
|
64
|
+
load Nunjucks configuration YAML file.
|
|
65
|
+
- `-C`|`--option` `<key>=<value>`:<br/>
|
|
66
|
+
set Nunjucks configuration option.
|
|
64
67
|
- `-d`|`--defines` `<context-file>`:<br/>
|
|
65
68
|
load context definition YAML file.
|
|
66
69
|
- `-D`|`--define` `<key>=<value>`:<br/>
|
package/nunjucks.js
CHANGED
|
@@ -6,41 +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
|
-
|
|
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
|
+
}
|
|
44
44
|
|
|
45
45
|
/* handle special version request */
|
|
46
46
|
if (argv.version) {
|
|
@@ -53,11 +53,11 @@ if (argv.version) {
|
|
|
53
53
|
|
|
54
54
|
/* read input file */
|
|
55
55
|
let input = ""
|
|
56
|
-
if (argv._.length
|
|
57
|
-
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)"))
|
|
58
58
|
process.exit(1)
|
|
59
59
|
}
|
|
60
|
-
let inputFile = argv._[0]
|
|
60
|
+
let inputFile = argv._[0] ?? "-"
|
|
61
61
|
if (inputFile === "-") {
|
|
62
62
|
inputFile = "<stdin>"
|
|
63
63
|
process.stdin.setEncoding("utf-8")
|
|
@@ -102,16 +102,12 @@ if (argv.defines) {
|
|
|
102
102
|
context.env = process.env
|
|
103
103
|
|
|
104
104
|
/* add context defines */
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
val = true
|
|
112
|
-
context[key] = val
|
|
113
|
-
})
|
|
114
|
-
}
|
|
105
|
+
argv.define.forEach((define) => {
|
|
106
|
+
let [ , key, val ] = define.match(/^([^=]+)(?:=(.*))?$/)
|
|
107
|
+
if (val === undefined)
|
|
108
|
+
val = true
|
|
109
|
+
context[key] = val
|
|
110
|
+
})
|
|
115
111
|
|
|
116
112
|
/* determine Nunjucks options */
|
|
117
113
|
let options = {}
|
|
@@ -124,8 +120,10 @@ if (argv.config) {
|
|
|
124
120
|
process.exit(1)
|
|
125
121
|
}
|
|
126
122
|
}
|
|
123
|
+
if (argv.option.length > 0)
|
|
124
|
+
options = Object.assign(options, argv.option)
|
|
127
125
|
options = Object.assign({}, {
|
|
128
|
-
autoescape:
|
|
126
|
+
autoescape: false,
|
|
129
127
|
throwOnUndefined: false,
|
|
130
128
|
trimBlocks: true,
|
|
131
129
|
lstripBlocks: true,
|
|
@@ -137,28 +135,26 @@ options = Object.assign({}, {
|
|
|
137
135
|
const env = nunjucks.configure(inputFile, options)
|
|
138
136
|
|
|
139
137
|
/* load external extension files */
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
modpath = require.resolve(extension)
|
|
146
|
-
}
|
|
147
|
-
catch (ex) {
|
|
148
|
-
modpath = null
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (modpath === null) {
|
|
152
|
-
console.error(chalk.red(`nunjucks: ERROR: failed to find extension module: ${extension}`))
|
|
153
|
-
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)
|
|
154
143
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${modpath}`))
|
|
158
|
-
process.exit(1)
|
|
144
|
+
catch (ex) {
|
|
145
|
+
modpath = null
|
|
159
146
|
}
|
|
160
|
-
mod(env)
|
|
161
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)
|
|
162
158
|
}
|
|
163
159
|
|
|
164
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
|
+
"version": "1.4.1",
|
|
5
5
|
"description": "Nunjucks Template Rendering Command-Line Interface",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dr. Ralf S. Engelschall",
|
|
@@ -22,14 +22,14 @@
|
|
|
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": {
|
|
29
29
|
"eslint": "8.47.0",
|
|
30
30
|
"eslint-config-standard": "17.1.0",
|
|
31
31
|
"eslint-plugin-promise": "6.1.1",
|
|
32
|
-
"eslint-plugin-import": "2.28.
|
|
32
|
+
"eslint-plugin-import": "2.28.1",
|
|
33
33
|
"eslint-plugin-node": "11.1.0"
|
|
34
34
|
},
|
|
35
35
|
"upd": [ "!chalk" ],
|