-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcmd.js
executable file
·90 lines (72 loc) · 2.39 KB
/
cmd.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
#!/usr/bin/env node
const fs = require('node:fs');
const parseArgs = require('minimist');
const cleaner = require('./index.js');
const argv = parseArgs(process.argv.slice(2));
const filename = argv['_'][0];
const inPlace = getOptAsBool(argv['in-place']);
const options = {
'allow-attributes-without-values': getOptAsBool(argv['allow-attributes-without-values']),
'break-around-comments': getOptAsBool(argv['break-around-comments']),
'break-around-tags': getOptAsArray(argv['break-around-tags']),
'decode-entities': getOptAsBool(argv['decode-entities']),
'indent': argv['indent'],
'lower-case-tags': getOptAsBool(argv['lower-case-tags']),
'lower-case-attribute-names': getOptAsBool(argv['lower-case-attribute-names']),
'preserve-tags': getOptAsArray(argv['preserve-tags']),
'remove-attributes': getOptAsArray(argv['remove-attributes']),
'remove-comments': getOptAsBool(argv['remove-comments']),
'remove-empty-tags': getOptAsArray(argv['remove-empty-tags']),
'remove-tags': getOptAsArray(argv['remove-tags']),
'wrap': getOptAsInt(argv['wrap']),
'add-break-around-tags': getOptAsArray(argv['add-break-around-tags']),
'add-remove-attributes': getOptAsArray(argv['add-remove-attributes']),
'add-remove-tags': getOptAsArray(argv['add-remove-tags'])
};
function getOptAsArray(opt) {
if (opt === undefined) {
return undefined;
}
if (Array.isArray(opt)) {
return opt
.map(o => o.split(','))
.reduce((prev, curr) => prev.concat(curr));
}
return opt.split(',');
}
function getOptAsBool(opt) {
if (opt === undefined) {
return undefined;
}
return opt === true || opt === 'true';
}
function getOptAsInt(opt) {
if (opt === undefined) {
return undefined;
}
const val = parseInt(opt);
return isNaN(val) ? undefined : val;
}
function read(filename, callback) {
return fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
throw err;
}
callback(data);
});
}
function write(html, filename) {
return fs.writeFile(filename, html + '\n', err => {
if (err) {
throw err;
}
});
}
read(filename || process.stdin.fd, data => {
cleaner.clean(data, options, html => {
if (filename && inPlace) {
return write(html, filename);
}
write(html, process.stdout.fd);
});
});