@rse/nunjucks-cli 1.1.0 → 1.2.0

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 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|<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).
@@ -64,12 +64,13 @@ Example
64
64
 
65
65
  ```sh
66
66
  $ echo "Hello, {{who}}!" | nunjucks -D who=world -
67
+ Hello, world!
67
68
  ```
68
69
 
69
70
  License
70
71
  -------
71
72
 
72
- Copyright (c) 2019-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)
73
+ Copyright &copy; 2019-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)
73
74
 
74
75
  Permission is hereby granted, free of charge, to any person obtaining
75
76
  a copy of this software and associated documentation files (the
@@ -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
+
package/nunjucks.js CHANGED
@@ -90,7 +90,7 @@ else {
90
90
  let context = {}
91
91
  if (argv.defines) {
92
92
  try {
93
- context = jsYAML.safeLoad(fs.readFileSync(argv.defines, { encoding: "utf8" }))
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.safeLoad(fs.readFileSync(argv.config, { encoding: "utf8" }))
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)$/))
143
143
  extension = path.join(__dirname, "nunjucks.d", `${extension}.js`)
144
- let path = null
144
+ let modpath = null
145
145
  try {
146
- path = require.resolve(extension)
146
+ modpath = require.resolve(extension)
147
147
  }
148
148
  catch (ex) {
149
- path = null
149
+ modpath = null
150
150
  }
151
- if (path === null) {
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 ext = require(extension)
156
- if (!(ext !== null && typeof ext === "function")) {
157
- console.error(chalk.red(`nunjucks: ERROR: failed to call extension file: ${extension}`))
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
- ext(env)
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.1.0",
4
+ "version": "1.2.0",
5
5
  "description": "Nunjucks Template Rendering Command-Line Interface",
6
6
  "author": {
7
7
  "name": "Dr. Ralf S. Engelschall",
@@ -18,6 +18,7 @@
18
18
  "yargs": "17.7.2",
19
19
  "moment": "2.29.4",
20
20
  "moment-timezone": "0.5.43",
21
+ "jsonpath": "1.1.1",
21
22
  "js-yaml": "4.1.0"
22
23
  },
23
24
  "devDependencies": {