-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
186 lines (166 loc) · 5.64 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import constants from "buffer";
const { MAX_STRING_LENGTH } = constants;
import meow from "meow";
import { default as minify, defaultOptions, minifyStream, defaultStreamOptions, debug as debugMinify } from "./index.js";
const cli = meow(`
Usage
$ minify-xml <input>
Options
--stream, -s Stream the input file, instead of reading it
--in-place, -i Save the minified results to the original file
--output, -o Save the minified results to a given output file
--verbose, -v Enables more verbose logging
Use prefix --no-, false or =false to disable
--remove-comments Remove comments
--remove-whitespace-between-tags Remove whitespace between tags
--consider-preserve-whitespace Consider preserving whitespace
--collapse-whitespace-in-tags Collapse whitespace in tags
--collapse-empty-elements Collapse empty elements
--collapse-whitespace-in-prolog Collapse whitespace in the prolog
--collapse-whitespace-in-doctype Collapse whitespace in the document type declaration
--remove-schema-location-attributes Remove schema location attributes
--remove-unnecessary-standalone-declaration Remove unnecessary standalone in prolog
--remove-unused-namespaces Remove any unused namespaces from tags
--remove-unused-default-namespace Remove unused default namespace declaration
--shorten-namespaces Shorten namespaces to a minimal length
--ignore-cdata Ignore any content inside of CData tags
--trim-whitespace-from-texts Remove leading and tailing whitespace in text elements
--collapse-whitespace-in-texts Collapse whitespace in text elements
--stream-max-match-length The maximum size of matches between chunks, defaults to 256 KiB
Examples
$ minify-xml sitemap.xml --stream --in-place
$ minify-xml sitemap.xml --output sitemap.min.xml
`, {
input: ["input"],
flags: {
stream: {
type: "boolean",
shortFlag: "s"
},
output: {
type: "string",
shortFlag: "o"
},
inPlace: {
type: "boolean",
shortFlag: "i",
},
streamMaxMatchLength: {
type: "number",
shortFlag: "streamMaximumMatchLength",
default: 256 * 1024 // 256 KiB
},
removeComments: {
type: "boolean"
},
removeWhitespaceBetweenTags: {
type: "string" // allows 'strict'
},
considerPreserveWhitespace: {
type: "boolean"
},
collapseWhitespaceInTags: {
type: "boolean"
},
collapseEmptyElements: {
type: "boolean"
},
trimWhitespaceFromTexts: {
type: "string" // allows 'strict'
},
collapseWhitespaceInTexts: {
type: "string" // allows 'strict'
},
collapseWhitespaceInProlog: {
type: "boolean"
},
collapseWhitespaceInDocType: {
type: "boolean",
shortFlag: "collapse-whitespace-in-doctype"
},
removeSchemaLocationAttributes: {
type: "boolean"
},
removeUnnecessaryStandaloneDeclaration: {
type: "boolean"
},
removeUnusedNamespaces: {
type: "boolean"
},
removeUnusedDefaultNamespace: {
type: "boolean"
},
shortenNamespaces: {
type: "boolean"
},
ignoreCData: {
type: "boolean",
shortFlag: "ignore-cdata"
},
verbose: {
type: "boolean",
shortFlag: "v"
},
debug: {
type: "string",
isMultiple: true
}
},
booleanDefault: undefined,
allowUnknownFlags: false,
importMeta: import.meta
});
const input = cli.input[0], debug = cli.flags.debug.length && !cli.flags.debug.includes("false");
if (!input && !debug) {
cli.showHelp(); // this exit's the process
}
const options = options => (Array.isArray(options) ? options : Object.keys(options)).reduce((options, option) => {
if (cli.flags.hasOwnProperty(option) && cli.flags[option] !== undefined) {
options[option] = String(cli.flags[option]) !== "false" ? (cli.flags[option] || true) : false;
} return options;
}, {});
let output;
if (cli.flags.inPlace || cli.flags.output) {
output = cli.flags.inPlace ? input : cli.flags.output;
cli.flags.verbose && console.info(`Writing to ${output}`);
}
let size = NaN;
try {
size = fs.statSync(input).size;
} catch(e) {
// nothing to do here
}
if (!cli.flags.stream) {
if (size > MAX_STRING_LENGTH) {
// only log to console, if output is set, otherwise the log message ends up in the stdout and might get piped to other applications
output && cli.flags.verbose && console.warn(`Files larger than ${MAX_STRING_LENGTH} bytes require to be streamed, switching to stream mode`);
cli.flags.stream = true;
}
}
if (debug) {
// if the debug flag is a string and doesn't contain --debug=true, only debug the single option(s) specified
debugMinify(input && fs.readFileSync(input, "utf8"), !cli.flags.debug.includes("true") ? {
...Object.fromEntries(Object.keys(defaultOptions).map(option => [option, false])), // set all to false
...Object.fromEntries(cli.flags.debug.map(option => option.replace(/[-_.\s](.)?/g, // set (camel cased) options specified to true
(match, char) => char?.toUpperCase() ?? String())).map(option => [option, true])),
...options(defaultOptions) // override any other given options, e.g. --debug=remove-whitespace-between-tags --remove-whitespace-between-tags=strict
} : options(defaultOptions));
} else if (cli.flags.stream) {
const stream = fs.createReadStream(input, "utf8");
if (output && size) {
let received = 0; stream.on("data", chunk => process.stdout.write(
`\rMinifying ${(received += chunk.length) / size * 100 | 0}% ...`));
}
stream.pipe(minifyStream(options(defaultStreamOptions)))
.pipe(output ? fs.createWriteStream(output, "utf8") : process.stdout);
} else {
const xml = minify(fs.readFileSync(input, "utf8"), options(defaultOptions));
if (output) {
fs.writeFileSync(output, xml, "utf8");
} else {
process.stdout.write(xml);
}
}