@rse/nunjucks-cli 1.4.0 → 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 (2) hide show
  1. package/nunjucks.js +57 -70
  2. package/package.json +2 -2
package/nunjucks.js CHANGED
@@ -6,48 +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
- "[-C|--option <key>=<value>] " +
28
- "[-d|--defines <context-file>] " +
29
- "[-D|--define <key>=<value>] " +
30
- "[-e|--extension <module-name>] " +
31
- "[-o|--output <output-file>|-] " +
32
- "<input-file>|-")
33
- .version(false)
34
- .help("h").alias("h", "help").default("h", false).describe("h", "show usage help")
35
- .boolean("V").alias("V", "version").describe("V", "show program version information")
36
- .string("c").nargs("c", 1).alias("c", "config").describe("c", "load Nunjucks configuration YAML file")
37
- .array("C").nargs("C", 1).alias("C", "option").describe("C", "set Nunjucks configuration option")
38
- .string("d").nargs("d", 1).alias("d", "defines").describe("d", "load context definition YAML file")
39
- .array("D").nargs("D", 1).alias("D", "define").describe("D", "set context definition key/value")
40
- .array("e").nargs("e", 1).alias("e", "extension").describe("e", "load Nunjucks JavaScript extension module")
41
- .string("o").nargs("o", 1).alias("o", "output").default("o", "-").describe("o", "save output file")
42
- .strict()
43
- .showHelpOnFail(true)
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 !== 1) {
64
- 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)"))
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
- if (argv.define) {
113
- argv.define.forEach((define) => {
114
- let [ , key, val ] = define.match(/^([^=]+)(?:=(.*))?$/)
115
- if (val === undefined)
116
- val = true
117
- context[key] = val
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.options) {
133
- console.log(argv.options)
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
- if (typeof argv.extension === "object" && argv.extension instanceof Array) {
150
- for (let extension of argv.extension) {
151
- let modpath = path.resolve(extension)
152
- if (!fs.existsSync(modpath)) {
153
- try {
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
- const mod = require(modpath)
165
- if (!(mod !== null && typeof mod === "function")) {
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rse/nunjucks-cli",
3
3
  "publishConfig": { "access": "public" },
4
- "version": "1.4.0",
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,7 +22,7 @@
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": {