1 | #!/usr/bin/env node
|
---|
2 | const fs = require('fs');
|
---|
3 | const cssesc = require('../cssesc.js');
|
---|
4 | const strings = process.argv.splice(2);
|
---|
5 | const stdin = process.stdin;
|
---|
6 | const options = {};
|
---|
7 | const log = console.log;
|
---|
8 |
|
---|
9 | const main = function() {
|
---|
10 | const option = strings[0];
|
---|
11 |
|
---|
12 | if (/^(?:-h|--help|undefined)$/.test(option)) {
|
---|
13 | log(
|
---|
14 | 'cssesc v%s - https://mths.be/cssesc',
|
---|
15 | cssesc.version
|
---|
16 | );
|
---|
17 | log([
|
---|
18 | '\nUsage:\n',
|
---|
19 | '\tcssesc [string]',
|
---|
20 | '\tcssesc [-i | --identifier] [string]',
|
---|
21 | '\tcssesc [-s | --single-quotes] [string]',
|
---|
22 | '\tcssesc [-d | --double-quotes] [string]',
|
---|
23 | '\tcssesc [-w | --wrap] [string]',
|
---|
24 | '\tcssesc [-e | --escape-everything] [string]',
|
---|
25 | '\tcssesc [-v | --version]',
|
---|
26 | '\tcssesc [-h | --help]',
|
---|
27 | '\nExamples:\n',
|
---|
28 | '\tcssesc \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
---|
29 | '\tcssesc --identifier \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
---|
30 | '\tcssesc --escape-everything \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
---|
31 | '\tcssesc --double-quotes --wrap \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
|
---|
32 | '\techo \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\' | cssesc'
|
---|
33 | ].join('\n'));
|
---|
34 | return process.exit(1);
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (/^(?:-v|--version)$/.test(option)) {
|
---|
38 | log('v%s', cssesc.version);
|
---|
39 | return process.exit(1);
|
---|
40 | }
|
---|
41 |
|
---|
42 | strings.forEach(function(string) {
|
---|
43 | // Process options
|
---|
44 | if (/^(?:-i|--identifier)$/.test(string)) {
|
---|
45 | options.isIdentifier = true;
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | if (/^(?:-s|--single-quotes)$/.test(string)) {
|
---|
49 | options.quotes = 'single';
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | if (/^(?:-d|--double-quotes)$/.test(string)) {
|
---|
53 | options.quotes = 'double';
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | if (/^(?:-w|--wrap)$/.test(string)) {
|
---|
57 | options.wrap = true;
|
---|
58 | return;
|
---|
59 | }
|
---|
60 | if (/^(?:-e|--escape-everything)$/.test(string)) {
|
---|
61 | options.escapeEverything = true;
|
---|
62 | return;
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Process string(s)
|
---|
66 | let result;
|
---|
67 | try {
|
---|
68 | result = cssesc(string, options);
|
---|
69 | log(result);
|
---|
70 | } catch (exception) {
|
---|
71 | log(exception.message + '\n');
|
---|
72 | log('Error: failed to escape.');
|
---|
73 | log('If you think this is a bug in cssesc, please report it:');
|
---|
74 | log('https://github.com/mathiasbynens/cssesc/issues/new');
|
---|
75 | log(
|
---|
76 | '\nStack trace using cssesc@%s:\n',
|
---|
77 | cssesc.version
|
---|
78 | );
|
---|
79 | log(exception.stack);
|
---|
80 | return process.exit(1);
|
---|
81 | }
|
---|
82 | });
|
---|
83 | // Return with exit status 0 outside of the `forEach` loop, in case
|
---|
84 | // multiple strings were passed in.
|
---|
85 | return process.exit(0);
|
---|
86 |
|
---|
87 | };
|
---|
88 |
|
---|
89 | if (stdin.isTTY) {
|
---|
90 | // handle shell arguments
|
---|
91 | main();
|
---|
92 | } else {
|
---|
93 | let timeout;
|
---|
94 | // Either the script is called from within a non-TTY context, or `stdin`
|
---|
95 | // content is being piped in.
|
---|
96 | if (!process.stdout.isTTY) {
|
---|
97 | // The script was called from a non-TTY context. This is a rather uncommon
|
---|
98 | // use case we don’t actively support. However, we don’t want the script
|
---|
99 | // to wait forever in such cases, so…
|
---|
100 | timeout = setTimeout(function() {
|
---|
101 | // …if no piped data arrived after a whole minute, handle shell
|
---|
102 | // arguments instead.
|
---|
103 | main();
|
---|
104 | }, 60000);
|
---|
105 | }
|
---|
106 | let data = '';
|
---|
107 | stdin.on('data', function(chunk) {
|
---|
108 | clearTimeout(timeout);
|
---|
109 | data += chunk;
|
---|
110 | });
|
---|
111 | stdin.on('end', function() {
|
---|
112 | strings.push(data.trim());
|
---|
113 | main();
|
---|
114 | });
|
---|
115 | stdin.resume();
|
---|
116 | }
|
---|