@rse/nunjucks-cli 1.1.1 → 1.2.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 +1 -1
- package/nunjucks.d/jsonpath.js +25 -0
- package/nunjucks.d/pad.js +24 -0
- package/nunjucks.d/sprintf.js +21 -0
- package/nunjucks.d/uuid.js +14 -0
- package/nunjucks.js +11 -11
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ $ nunjucks
|
|
|
52
52
|
load context definition YAML file.
|
|
53
53
|
- `-D`|`--define` `<key>=<value>`:<br/>
|
|
54
54
|
set context definition key/value.
|
|
55
|
-
- `-e`|`--extension` `default|date|eval|<module-name>`:<br/>
|
|
55
|
+
- `-e`|`--extension` `default|date|eval|jsonpath|pad|sprintf|uuid|<module-name>`:<br/>
|
|
56
56
|
load Nunjucks JavaScript extension module (built-in or NPM installed).
|
|
57
57
|
- `-o`|`--output` `<output-file>`|`-`:<br/>
|
|
58
58
|
save output file (or stdout).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** nunjucks -- Nunjucks Template Rendering Command-Line Interface
|
|
3
|
+
** Copyright (c) 2019-2023 Dr. Ralf S. Engelschall <http://engelschall.com>
|
|
4
|
+
** Licensed under MIT <http://spdx.org/licenses/MIT.html>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const jsonpath = require("jsonpath")
|
|
8
|
+
|
|
9
|
+
module.exports = (env) => {
|
|
10
|
+
/* add a "jsonpath" filter */
|
|
11
|
+
env.addFilter("jsonpath", (obj, path, count) => {
|
|
12
|
+
let result
|
|
13
|
+
const errs = []
|
|
14
|
+
try {
|
|
15
|
+
result = jsonpath.query(obj, path, count)
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
errs.push(err)
|
|
19
|
+
}
|
|
20
|
+
if (errs.length)
|
|
21
|
+
return errs.join("\n")
|
|
22
|
+
return result
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** nunjucks -- Nunjucks Template Rendering Command-Line Interface
|
|
3
|
+
** Copyright (c) 2019-2023 Dr. Ralf S. Engelschall <http://engelschall.com>
|
|
4
|
+
** Licensed under MIT <http://spdx.org/licenses/MIT.html>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
module.exports = (env) => {
|
|
8
|
+
/* add a "pad" formatting filter */
|
|
9
|
+
env.addFilter("pad", (input, num, char = " ", toRight = false) => {
|
|
10
|
+
if (typeof input !== "string") {
|
|
11
|
+
if (typeof input.toString === "function")
|
|
12
|
+
input = input.toString()
|
|
13
|
+
else
|
|
14
|
+
input = input + ''
|
|
15
|
+
}
|
|
16
|
+
let result = input
|
|
17
|
+
if (result.length < num) {
|
|
18
|
+
const pad = (new Array(num - result.length + 1)).join(char)
|
|
19
|
+
result = toRight ? result + pad : pad + result
|
|
20
|
+
}
|
|
21
|
+
return result
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** nunjucks -- Nunjucks Template Rendering Command-Line Interface
|
|
3
|
+
** Copyright (c) 2019-2023 Dr. Ralf S. Engelschall <http://engelschall.com>
|
|
4
|
+
** Licensed under MIT <http://spdx.org/licenses/MIT.html>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const sprintf = require("sprintfjs")
|
|
8
|
+
|
|
9
|
+
module.exports = (env) => {
|
|
10
|
+
/* add a "sprintf" formatting filter */
|
|
11
|
+
env.addFilter("sprintf", (value, format, ...args) => {
|
|
12
|
+
return sprintf(format, value, ...args)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
/* add a "sprintf" global function */
|
|
16
|
+
env.addGlobal("sprintf", (format, ...args) => {
|
|
17
|
+
console.log(format, args)
|
|
18
|
+
return sprintf(format, ...args)
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** nunjucks -- Nunjucks Template Rendering Command-Line Interface
|
|
3
|
+
** Copyright (c) 2019-2023 Dr. Ralf S. Engelschall <http://engelschall.com>
|
|
4
|
+
** Licensed under MIT <http://spdx.org/licenses/MIT.html>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const UUID = require("pure-uuid")
|
|
8
|
+
|
|
9
|
+
module.exports = (env) => {
|
|
10
|
+
env.addGlobal("uuid", (...args) => {
|
|
11
|
+
return (new UUID(...args)).format()
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
|
package/nunjucks.js
CHANGED
|
@@ -90,7 +90,7 @@ else {
|
|
|
90
90
|
let context = {}
|
|
91
91
|
if (argv.defines) {
|
|
92
92
|
try {
|
|
93
|
-
context = jsYAML.
|
|
93
|
+
context = jsYAML.load(fs.readFileSync(argv.defines, { encoding: "utf8" }))
|
|
94
94
|
}
|
|
95
95
|
catch (ex) {
|
|
96
96
|
console.error(chalk.red(`nunjucks: ERROR: failed to load context YAML file: ${ex.toString()}`))
|
|
@@ -117,7 +117,7 @@ if (argv.define) {
|
|
|
117
117
|
let options = {}
|
|
118
118
|
if (argv.config) {
|
|
119
119
|
try {
|
|
120
|
-
options = jsYAML.
|
|
120
|
+
options = jsYAML.load(fs.readFileSync(argv.config, { encoding: "utf8" }))
|
|
121
121
|
}
|
|
122
122
|
catch (ex) {
|
|
123
123
|
console.error(chalk.red(`nunjucks: ERROR: failed to load options YAML file: ${ex.toString()}`))
|
|
@@ -139,25 +139,25 @@ const env = nunjucks.configure(inputFile, options)
|
|
|
139
139
|
/* load external extension files */
|
|
140
140
|
if (typeof argv.extension === "object" && argv.extension instanceof Array) {
|
|
141
141
|
for (let extension of argv.extension) {
|
|
142
|
-
if (extension.match(/^(?:default|date|eval)$/))
|
|
142
|
+
if (extension.match(/^(?:default|date|eval|jsonpath|pad|sprintf|uuid)$/))
|
|
143
143
|
extension = path.join(__dirname, "nunjucks.d", `${extension}.js`)
|
|
144
|
-
let
|
|
144
|
+
let modpath = null
|
|
145
145
|
try {
|
|
146
|
-
|
|
146
|
+
modpath = require.resolve(extension)
|
|
147
147
|
}
|
|
148
148
|
catch (ex) {
|
|
149
|
-
|
|
149
|
+
modpath = null
|
|
150
150
|
}
|
|
151
|
-
if (
|
|
151
|
+
if (modpath === null) {
|
|
152
152
|
console.error(chalk.red(`nunjucks: ERROR: failed to find extension module: ${extension}`))
|
|
153
153
|
process.exit(1)
|
|
154
154
|
}
|
|
155
|
-
const
|
|
156
|
-
if (!(
|
|
157
|
-
console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${
|
|
155
|
+
const mod = require(modpath)
|
|
156
|
+
if (!(mod !== null && typeof mod === "function")) {
|
|
157
|
+
console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${modpath}`))
|
|
158
158
|
process.exit(1)
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
mod(env)
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
|
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.2.1",
|
|
5
5
|
"description": "Nunjucks Template Rendering Command-Line Interface",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dr. Ralf S. Engelschall",
|
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
"yargs": "17.7.2",
|
|
19
19
|
"moment": "2.29.4",
|
|
20
20
|
"moment-timezone": "0.5.43",
|
|
21
|
+
"jsonpath": "1.1.1",
|
|
22
|
+
"sprintfjs": "1.2.17",
|
|
23
|
+
"pure-uuid": "1.7.0",
|
|
21
24
|
"js-yaml": "4.1.0"
|
|
22
25
|
},
|
|
23
26
|
"devDependencies": {
|