@rse/nunjucks-cli 1.4.0 → 1.4.2
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 +2 -1
- package/nunjucks.1 +47 -0
- package/nunjucks.js +57 -70
- package/nunjucks.md +73 -0
- package/package.json +11 -7
package/README.md
CHANGED
package/nunjucks.1
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
.TH "NUNJUCKS" "1" "August 2023" "" ""
|
|
2
|
+
.SH "NAME"
|
|
3
|
+
\fBnunjucks\fR - Template Rendering Engine
|
|
4
|
+
.SH "SYNOPSIS"
|
|
5
|
+
.P
|
|
6
|
+
\fBnunjucks\fR \[lB]\fB-h\fR|\fB--help\fR\[rB] \[lB]\fB-V\fR|\fB--version\fR\[rB] \[lB]\fB-c\fR|\fB--config\fR \fIconfig-file\fR\[rB] \[lB]\fB-C\fR|\fB--option\fR \fIkey\fR=\fIvalue\fR\[rB] \[lB]\fB-d\fR|\fB--defines\fR \fIcontext-file\fR\[rB] \[lB]\fB-D\fR|\fB--define\fR \fIkey\fR=\fIvalue\fR\[rB] \[lB]\fB-e\fR|\fB--extension\fR \fImodule-name\fR\[rB] \[lB]\fB-o\fR|\fB--output\fR \fIoutput-file\fR|\fB-\fR\[rB] \[lB]\fIinput-file\fR|\fB-\fR\[rB]
|
|
7
|
+
.SH "DESCRIPTION"
|
|
8
|
+
.P
|
|
9
|
+
\fBnunjucks\fR(1) is a small command-line utility to render templates with the rich and powerful templating language \fBMozilla Nunjucks\fR \fI\(lahttps://mozilla.github.io/nunjucks/\(ra\fR. This allows you to define your configuration in a YAML file and then render an output file based on a template input file where your configuration can be expanded. It optionally can load Nunjucks addons like the ones from the companion \fBNunjucks Addons\fR \fI\(lahttps://github.com/rse/nunjucks-addons\(ra\fR package.
|
|
10
|
+
.SH "OPTIONS"
|
|
11
|
+
.P
|
|
12
|
+
The following top-level options and arguments exist:
|
|
13
|
+
.RS 0
|
|
14
|
+
.IP \(bu 4
|
|
15
|
+
\[lB]\fB-h\fR|\fB--help\fR\[rB] Show usage help.
|
|
16
|
+
.IP \(bu 4
|
|
17
|
+
\[lB]\fB-V\fR|\fB--version\fR\[rB] show program version information.
|
|
18
|
+
.IP \(bu 4
|
|
19
|
+
\[lB]\fB-c\fR|\fB--config\fR \fIconfig-file\fR\[rB] load Nunjucks configuration YAML file.
|
|
20
|
+
.IP \(bu 4
|
|
21
|
+
\[lB]\fB-C\fR|\fB--option\fR \fIkey\fR=\fIvalue\fR\[rB] set Nunjucks configuration option.
|
|
22
|
+
.IP \(bu 4
|
|
23
|
+
\[lB]\fB-d\fR|\fB--defines\fR \fIcontext-file\fR\[rB] load context definition YAML file.
|
|
24
|
+
.IP \(bu 4
|
|
25
|
+
\[lB]\fB-D\fR|\fB--define\fR \fIkey\fR=\fIvalue\fR\[rB] set context definition key/value.
|
|
26
|
+
.IP \(bu 4
|
|
27
|
+
\[lB]\fB-e\fR|\fB--extension\fR \fImodule-name\fR\[rB] load Nunjucks JavaScript extension module (installed via NPM).
|
|
28
|
+
.IP \(bu 4
|
|
29
|
+
\[lB]\fB-o\fR|\fB--output\fR \fIoutput-file\fR|\fB-\fR\[rB] save output file (or stdout).
|
|
30
|
+
.IP \(bu 4
|
|
31
|
+
\[lB]\fB<input-file>\fR|\fB-\fR\[rB] load input file (or stdin).
|
|
32
|
+
.RE 0
|
|
33
|
+
|
|
34
|
+
.SH "EXAMPLE"
|
|
35
|
+
.P
|
|
36
|
+
.RS 2
|
|
37
|
+
.nf
|
|
38
|
+
$ echo "Hello, {{who}}!" | nunjucks -D who=world -
|
|
39
|
+
Hello, world!
|
|
40
|
+
.fi
|
|
41
|
+
.RE
|
|
42
|
+
.SH "HISTORY"
|
|
43
|
+
.P
|
|
44
|
+
The \fBnunjucks\fR(1) utility was developed in August 2023 for being able to easily generate multiple configuration files for a complex \fIDocker-Compose\fR based setup.
|
|
45
|
+
.SH "AUTHOR"
|
|
46
|
+
.P
|
|
47
|
+
Dr. Ralf S. Engelschall \fI\(larse@engelschall.com\(ra\fR
|
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/nunjucks.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
# nunjucks(1) -- Template Rendering Engine
|
|
3
|
+
|
|
4
|
+
## SYNOPSIS
|
|
5
|
+
|
|
6
|
+
`nunjucks`
|
|
7
|
+
\[`-h`|`--help`\]
|
|
8
|
+
\[`-V`|`--version`\]
|
|
9
|
+
\[`-c`|`--config` *config-file*\]
|
|
10
|
+
\[`-C`|`--option` *key*=*value*\]
|
|
11
|
+
\[`-d`|`--defines` *context-file*\]
|
|
12
|
+
\[`-D`|`--define` *key*=*value*\]
|
|
13
|
+
\[`-e`|`--extension` *module-name*\]
|
|
14
|
+
\[`-o`|`--output` *output-file*|`-`\]
|
|
15
|
+
\[*input-file*|`-`\]
|
|
16
|
+
|
|
17
|
+
## DESCRIPTION
|
|
18
|
+
|
|
19
|
+
`nunjucks`(1) is a small command-line utility to render templates with the rich
|
|
20
|
+
and powerful templating language [Mozilla Nunjucks](https://mozilla.github.io/nunjucks/).
|
|
21
|
+
This allows you to define your configuration in a YAML file and then render
|
|
22
|
+
an output file based on a template input file where your configuration can be expanded.
|
|
23
|
+
It optionally can load Nunjucks addons like the ones from the companion
|
|
24
|
+
[Nunjucks Addons](https://github.com/rse/nunjucks-addons) package.
|
|
25
|
+
|
|
26
|
+
## OPTIONS
|
|
27
|
+
|
|
28
|
+
The following top-level options and arguments exist:
|
|
29
|
+
|
|
30
|
+
- \[`-h`|`--help`\]
|
|
31
|
+
Show usage help.
|
|
32
|
+
|
|
33
|
+
- \[`-V`|`--version`\]
|
|
34
|
+
show program version information.
|
|
35
|
+
|
|
36
|
+
- \[`-c`|`--config` *config-file*\]
|
|
37
|
+
load Nunjucks configuration YAML file.
|
|
38
|
+
|
|
39
|
+
- \[`-C`|`--option` *key*=*value*\]
|
|
40
|
+
set Nunjucks configuration option.
|
|
41
|
+
|
|
42
|
+
- \[`-d`|`--defines` *context-file*\]
|
|
43
|
+
load context definition YAML file.
|
|
44
|
+
|
|
45
|
+
- \[`-D`|`--define` *key*=*value*\]
|
|
46
|
+
set context definition key/value.
|
|
47
|
+
|
|
48
|
+
- \[`-e`|`--extension` *module-name*\]
|
|
49
|
+
load Nunjucks JavaScript extension module (installed via NPM).
|
|
50
|
+
|
|
51
|
+
- \[`-o`|`--output` *output-file*|`-`\]
|
|
52
|
+
save output file (or stdout).
|
|
53
|
+
|
|
54
|
+
- \[`<input-file>`|`-`\]
|
|
55
|
+
load input file (or stdin).
|
|
56
|
+
|
|
57
|
+
## EXAMPLE
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
$ echo "Hello, {{who}}!" | nunjucks -D who=world -
|
|
61
|
+
Hello, world!
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## HISTORY
|
|
65
|
+
|
|
66
|
+
The `nunjucks`(1) utility was developed in August 2023 for being
|
|
67
|
+
able to easily generate multiple configuration files for a complex
|
|
68
|
+
*Docker-Compose* based setup.
|
|
69
|
+
|
|
70
|
+
## AUTHOR
|
|
71
|
+
|
|
72
|
+
Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
73
|
+
|
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.2",
|
|
5
5
|
"description": "Nunjucks Template Rendering Command-Line Interface",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dr. Ralf S. Engelschall",
|
|
@@ -10,11 +10,11 @@
|
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"repository": {
|
|
13
|
-
"type":
|
|
14
|
-
"url":
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git://github.com/rse/nunjucks-cli.git"
|
|
15
15
|
},
|
|
16
16
|
"bugs": {
|
|
17
|
-
"url":
|
|
17
|
+
"url": "http://github.com/rse/nunjucks-cli/issues"
|
|
18
18
|
},
|
|
19
19
|
"bin": {
|
|
20
20
|
"nunjucks": "nunjucks.js"
|
|
@@ -22,19 +22,23 @@
|
|
|
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
|
-
"eslint": "8.
|
|
29
|
+
"eslint": "8.48.0",
|
|
30
30
|
"eslint-config-standard": "17.1.0",
|
|
31
31
|
"eslint-plugin-promise": "6.1.1",
|
|
32
32
|
"eslint-plugin-import": "2.28.1",
|
|
33
|
-
"eslint-plugin-node": "11.1.0"
|
|
33
|
+
"eslint-plugin-node": "11.1.0",
|
|
34
|
+
"remark-cli": "11.0.0",
|
|
35
|
+
"remark": "14.0.3",
|
|
36
|
+
"remark-man": "8.0.1"
|
|
34
37
|
},
|
|
35
38
|
"upd": [ "!chalk" ],
|
|
36
39
|
"scripts": {
|
|
37
40
|
"lint": "eslint --config eslint.yaml nunjucks.js",
|
|
41
|
+
"man": "remark --quiet --use remark-man --output nunjucks.1 nunjucks.md",
|
|
38
42
|
"test": "echo 'Hello, {{who}}!' | node nunjucks.js -D who=world -"
|
|
39
43
|
}
|
|
40
44
|
}
|