1
|
#!/usr/bin/env node
|
2
|
(function() {
|
3
|
|
4
|
var fs = require('fs');
|
5
|
var stringEscape = require('../jsesc.js');
|
6
|
var strings = process.argv.splice(2);
|
7
|
var stdin = process.stdin;
|
8
|
var data;
|
9
|
var timeout;
|
10
|
var isObject = false;
|
11
|
var options = {};
|
12
|
var log = console.log;
|
13
|
|
14
|
var main = function() {
|
15
|
var option = strings[0];
|
16
|
|
17
|
if (/^(?:-h|--help|undefined)$/.test(option)) {
|
18
|
log(
|
19
|
'jsesc v%s - https://mths.be/jsesc',
|
20
|
stringEscape.version
|
21
|
);
|
22
|
log([
|
23
|
'\nUsage:\n',
|
24
|
'\tjsesc [string]',
|
25
|
'\tjsesc [-s | --single-quotes] [string]',
|
26
|
'\tjsesc [-d | --double-quotes] [string]',
|
27
|
'\tjsesc [-w | --wrap] [string]',
|
28
|
'\tjsesc [-e | --escape-everything] [string]',
|
29
|
'\tjsesc [-t | --escape-etago] [string]',
|
30
|
'\tjsesc [-6 | --es6] [string]',
|
31
|
'\tjsesc [-l | --lowercase-hex] [string]',
|
32
|
'\tjsesc [-j | --json] [string]',
|
33
|
'\tjsesc [-o | --object] [stringified_object]', // `JSON.parse()` the argument
|
34
|
'\tjsesc [-p | --pretty] [string]', // `compact: false`
|
35
|
'\tjsesc [-v | --version]',
|
36
|
'\tjsesc [-h | --help]',
|
37
|
'\nExamples:\n',
|
38
|
'\tjsesc \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
39
|
'\tjsesc --json \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
40
|
'\tjsesc --json --escape-everything \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
41
|
'\tjsesc --double-quotes --wrap \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
42
|
'\techo \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\' | jsesc'
|
43
|
].join('\n'));
|
44
|
return process.exit(1);
|
45
|
}
|
46
|
|
47
|
if (/^(?:-v|--version)$/.test(option)) {
|
48
|
log('v%s', stringEscape.version);
|
49
|
return process.exit(1);
|
50
|
}
|
51
|
|
52
|
strings.forEach(function(string) {
|
53
|
// Process options
|
54
|
if (/^(?:-s|--single-quotes)$/.test(string)) {
|
55
|
options.quotes = 'single';
|
56
|
return;
|
57
|
}
|
58
|
if (/^(?:-d|--double-quotes)$/.test(string)) {
|
59
|
options.quotes = 'double';
|
60
|
return;
|
61
|
}
|
62
|
if (/^(?:-w|--wrap)$/.test(string)) {
|
63
|
options.wrap = true;
|
64
|
return;
|
65
|
}
|
66
|
if (/^(?:-e|--escape-everything)$/.test(string)) {
|
67
|
options.escapeEverything = true;
|
68
|
return;
|
69
|
}
|
70
|
if (/^(?:-t|--escape-etago)$/.test(string)) {
|
71
|
options.escapeEtago = true;
|
72
|
return;
|
73
|
}
|
74
|
if (/^(?:-6|--es6)$/.test(string)) {
|
75
|
options.es6 = true;
|
76
|
return;
|
77
|
}
|
78
|
if (/^(?:-l|--lowercase-hex)$/.test(string)) {
|
79
|
options.lowercaseHex = true;
|
80
|
return;
|
81
|
}
|
82
|
if (/^(?:-j|--json)$/.test(string)) {
|
83
|
options.json = true;
|
84
|
return;
|
85
|
}
|
86
|
if (/^(?:-o|--object)$/.test(string)) {
|
87
|
isObject = true;
|
88
|
return;
|
89
|
}
|
90
|
if (/^(?:-p|--pretty)$/.test(string)) {
|
91
|
isObject = true;
|
92
|
options.compact = false;
|
93
|
return;
|
94
|
}
|
95
|
|
96
|
// Process string(s)
|
97
|
var result;
|
98
|
try {
|
99
|
if (isObject) {
|
100
|
string = JSON.parse(string);
|
101
|
}
|
102
|
result = stringEscape(string, options);
|
103
|
log(result);
|
104
|
} catch(error) {
|
105
|
log(error.message + '\n');
|
106
|
log('Error: failed to escape.');
|
107
|
log('If you think this is a bug in jsesc, please report it:');
|
108
|
log('https://github.com/mathiasbynens/jsesc/issues/new');
|
109
|
log(
|
110
|
'\nStack trace using jsesc@%s:\n',
|
111
|
stringEscape.version
|
112
|
);
|
113
|
log(error.stack);
|
114
|
return process.exit(1);
|
115
|
}
|
116
|
});
|
117
|
// Return with exit status 0 outside of the `forEach` loop, in case
|
118
|
// multiple strings were passed in.
|
119
|
return process.exit(0);
|
120
|
|
121
|
};
|
122
|
|
123
|
if (stdin.isTTY) {
|
124
|
// handle shell arguments
|
125
|
main();
|
126
|
} else {
|
127
|
// Either the script is called from within a non-TTY context,
|
128
|
// or `stdin` content is being piped in.
|
129
|
if (!process.stdout.isTTY) { // called from a non-TTY context
|
130
|
timeout = setTimeout(function() {
|
131
|
// if no piped data arrived after a while, handle shell arguments
|
132
|
main();
|
133
|
}, 250);
|
134
|
}
|
135
|
|
136
|
data = '';
|
137
|
stdin.on('data', function(chunk) {
|
138
|
clearTimeout(timeout);
|
139
|
data += chunk;
|
140
|
});
|
141
|
stdin.on('end', function() {
|
142
|
strings.push(data.trim());
|
143
|
main();
|
144
|
});
|
145
|
stdin.resume();
|
146
|
}
|
147
|
|
148
|
}());
|