x3d-tidy 2.1.26 → 2.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.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/package.json +2 -1
  3. package/src/main.js +43 -6
package/README.md CHANGED
@@ -33,6 +33,10 @@ This tool is particularly useful for developers, 3D artists, and researchers wor
33
33
 
34
34
  Set double precision, default is 15.
35
35
 
36
+ ### -e *extension*
37
+
38
+ Set output file extension(s), e.g. ".x3dv". The output file will have the same basename as the input file.
39
+
36
40
  ### -f *integer*
37
41
 
38
42
  Set float precision, default is 7.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x3d-tidy",
3
- "version": "2.1.26",
3
+ "version": "2.2.0",
4
4
  "description": "X3D Converter, Beautifier and Minimizer",
5
5
  "main": "src/main.js",
6
6
  "bin": {
@@ -53,6 +53,7 @@
53
53
  }
54
54
  ],
55
55
  "dependencies": {
56
+ "colors": "^1.4.0",
56
57
  "x_ite": "^12.0.7",
57
58
  "x_ite-node": "^1.1.0",
58
59
  "x3d-traverse": "^1.0.13",
package/src/main.js CHANGED
@@ -11,6 +11,7 @@ const
11
11
  path = require ("path"),
12
12
  fs = require ("fs"),
13
13
  zlib = require ("zlib"),
14
+ colors = require ("colors"),
14
15
  DEBUG = false;
15
16
 
16
17
  main ();
@@ -41,7 +42,7 @@ async function convert ()
41
42
  .alias ("v", "version")
42
43
  .fail ((msg, error, yargs) =>
43
44
  {
44
- console .error (msg);
45
+ console .error (colors .red (msg));
45
46
  process .exit (1);
46
47
  })
47
48
  .option ("double",
@@ -51,6 +52,17 @@ async function convert ()
51
52
  description: "Set double precision, default is 15.",
52
53
  array: true,
53
54
  default: [15],
55
+ requiresArg: true,
56
+ })
57
+ .option ("extension",
58
+ {
59
+ type: "string",
60
+ alias: "e",
61
+ description: `Set output file extension(s), e.g. ".x3dv". The output file will have the same basename as the input file.`,
62
+ array: true,
63
+ requiresArg: true,
64
+ implies: "input",
65
+ conflicts: "output",
54
66
  })
55
67
  .option ("float",
56
68
  {
@@ -59,6 +71,7 @@ async function convert ()
59
71
  description: "Set float precision, default is 7.",
60
72
  array: true,
61
73
  default: [7],
74
+ requiresArg: true,
62
75
  })
63
76
  .option ("input",
64
77
  {
@@ -66,8 +79,8 @@ async function convert ()
66
79
  alias: "i",
67
80
  description: "Set input file(s). If there are less input files than output files, the last input file is used for the remaining output files.",
68
81
  array: true,
69
- default: [ ],
70
- implies: "output",
82
+ requiresArg: true,
83
+ demandOption: true,
71
84
  })
72
85
  .option ("metadata",
73
86
  {
@@ -83,8 +96,9 @@ async function convert ()
83
96
  alias: "o",
84
97
  description: `Set output file(s). To output it to stdout use only the extension, e.g. ".x3dv".`,
85
98
  array: true,
86
- default: [ ],
99
+ requiresArg: true,
87
100
  implies: "input",
101
+ conflicts: "extension",
88
102
  })
89
103
  .option ("infer",
90
104
  {
@@ -101,6 +115,7 @@ async function convert ()
101
115
  description: `Set output style, default is "TIDY". "TIDY" results in a good readable file, but with larger size, whereas "CLEAN" result in the smallest size possible by removing all redundant whitespaces. The other values are somewhere in between.`,
102
116
  choices: ["TIDY", "COMPACT", "SMALL", "CLEAN"],
103
117
  array: true,
118
+ requiresArg: true,
104
119
  default: ["TIDY"],
105
120
  })
106
121
  .example ([
@@ -113,9 +128,16 @@ async function convert ()
113
128
  "Convert an XML encoded file to a VRML encoded file and a JSON encoded file with smallest size possible by removing redundant whitespaces"
114
129
  ],
115
130
  ])
131
+ .requiresArg (["extension", "output"])
116
132
  .help ()
117
133
  .alias ("help", "h") .argv;
118
134
 
135
+ if (!args .output && !args .extension)
136
+ {
137
+ console .error (colors .red ("Missing argument output or extension."));
138
+ process .exit (1);
139
+ }
140
+
119
141
  // Fixes an issue with URL, if it matches a drive letter.
120
142
  args .input = args .input .map (input => input .replace (/^([A-Za-z]:)/, "file://$1"));
121
143
 
@@ -134,7 +156,9 @@ async function convert ()
134
156
  if (!args .input .length)
135
157
  console .warn ("No input files specified.");
136
158
 
137
- for (const i of args .output .keys ())
159
+ const argc = Math .max (args .input .length, args .output ?.length ?? args .extension ?.length);
160
+
161
+ for (let i = 0; i < argc; ++ i)
138
162
  {
139
163
  const
140
164
  input = new URL (arg (args .input, i), url .pathToFileURL (path .join (process .cwd (), "/"))),
@@ -161,7 +185,20 @@ async function convert ()
161
185
  doublePrecision: arg (args .double, i),
162
186
  };
163
187
 
164
- const output = path .resolve (process .cwd (), args .output [i]);
188
+ let output;
189
+
190
+ if (args .output)
191
+ {
192
+ output = path .resolve (process .cwd (), arg (args .output, i));
193
+ }
194
+ else if (args .extension)
195
+ {
196
+ const
197
+ filename = url .fileURLToPath (input),
198
+ extension = arg (args .extension, i);
199
+
200
+ output = `${filename .slice (0, -path. extname (filename) .length)}${extension}`;
201
+ }
165
202
 
166
203
  if (path .extname (output))
167
204
  fs .writeFileSync (output, getContents ({ ... options, type: path .extname (output) }));