@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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/nunjucks.js +58 -62
  3. 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 = require("./package.json")
9
+ const my = require("./package.json")
10
10
 
11
11
  /* internal requirements */
12
- const fs = require("node:fs")
13
- const path = require("node:path")
12
+ const fs = require("node:fs")
13
+ const path = require("node:path")
14
14
 
15
15
  /* external requirements */
16
- const yargs = require("yargs")
17
- const chalk = require("chalk")
18
- const jsYAML = require("js-yaml")
19
- const nunjucks = require("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 argv = yargs
23
- .usage("Usage: nunjucks " +
24
- "[-h|--help] " +
25
- "[-V|--version] " +
26
- "[-c|--config <config-file>] " +
27
- "[-d|--defines <context-file>] " +
28
- "[-D|--define <key>=<value>] " +
29
- "[-e|--extension <module-name>] " +
30
- "[-o|--output <output-file>|-] " +
31
- "<input-file>|-")
32
- .version(false)
33
- .help("h").alias("h", "help").default("h", false).describe("h", "show usage help")
34
- .boolean("V").alias("V", "version").describe("V", "show program version information")
35
- .string("c").nargs("c", 1).alias("c", "config").describe("c", "load Nunjucks configuration YAML file")
36
- .string("d").nargs("d", 1).alias("d", "defines").describe("d", "load context definition YAML file")
37
- .string("D").nargs("D", 1).alias("D", "define").describe("D", "set context definition key/value")
38
- .array("e").nargs("e", 1).alias("e", "extension").describe("e", "load Nunjucks JavaScript extension module")
39
- .string("o").nargs("o", 1).alias("o", "output").default("o", "-").describe("o", "save output file")
40
- .strict()
41
- .showHelpOnFail(true)
42
- .demand(0)
43
- .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
+ }
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 !== 1) {
57
- console.error(chalk.red("nunjucks: ERROR: missing input file"))
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
- if (argv.define) {
106
- if (typeof argv.define === "string")
107
- argv.define = [ argv.define ]
108
- argv.define.forEach((define) => {
109
- let [ , key, val ] = define.match(/^([^=]+)(?:=(.*))?$/)
110
- if (val === undefined)
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: true,
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
- if (typeof argv.extension === "object" && argv.extension instanceof Array) {
141
- for (let extension of argv.extension) {
142
- let modpath = path.resolve(extension)
143
- if (!fs.existsSync(modpath)) {
144
- try {
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
- 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
- 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.3.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
- "yargs": "17.7.2",
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.0",
32
+ "eslint-plugin-import": "2.28.1",
33
33
  "eslint-plugin-node": "11.1.0"
34
34
  },
35
35
  "upd": [ "!chalk" ],