-
Notifications
You must be signed in to change notification settings - Fork 13
/
screw.js
executable file
·183 lines (169 loc) · 4.92 KB
/
screw.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
#!/usr/bin/env node
/* eslint-env node */
'use strict';
function fail()
{
process.exit(1);
}
function getBasename()
{
var path = require('path');
var basename = path.basename(process.argv[1]);
return basename;
}
function printErrorMessage(errorMessage)
{
var basename = getBasename();
console.error
('%s: %s.\nTry "%s --help" for more information.', basename, errorMessage, basename);
}
function printHelpMessage()
{
var message =
'Usage: %s [OPTION]... [SOURCE [DESTINATION]]\n' +
'Encodes JavaScript with JScrewIt.\n' +
'\n' +
' -d, --diagnostic print diagnostic report\n' +
' -f, --features FEATURES use a list of comma separated features\n' +
' -t, --trim-code strip leading and trailing blanks and comments\n' +
' -r, --run-as METHOD control generated code type\n' +
' --help display this help and exit\n' +
' --version print version information and exit\n' +
'\n' +
'If no destination file is specified, the output is written to the console and\n' +
'no reports are printed (-d is ignored).\n' +
'If no source or destination file is specified, the command runs in interactive\n' +
'mode until interrupted with ^C.\n' +
'\n' +
'--run-as expects an argument out of the values for the option runAs described\n' +
'in the documentation of JScrewIt.encode.\n' +
'Most of these methods also have a short flag syntax associated.\n' +
'\n' +
' RunAs Method Short Flags\n' +
' ────────────────────── ────────────────\n' +
' call -c, -w\n' +
' eval -e\n' +
' express -x\n' +
' express-call -xc, -xw\n' +
' express-eval (default) -xe\n' +
' none (none available)\n' +
'\n' +
'See the JScrewIt feature documentation for a list of all supported features.\n';
var basename = getBasename();
console.log(message, basename);
}
function printVersion()
{
var version = require('./package.json').version;
console.log('JScrewIt ' + version);
}
function prompt()
{
process.stdout.write('SCREW> ');
}
(function ()
{
var cli = require('./tools/cli');
var command;
try
{
command = cli.parseCommandLine(process.argv);
}
catch (error)
{
printErrorMessage(error.message);
fail();
}
if (command === 'help')
{
printHelpMessage();
return;
}
if (command === 'version')
{
printVersion();
return;
}
var inputFileName = command.inputFileName;
var outputFileName = command.outputFileName;
var options = command.options;
var JScrewIt = require('./'); // '.' doesn't work in Node.js 0.10.
if (inputFileName == null)
{
var tryEncode =
function (input)
{
var output;
try
{
output = JScrewIt.encode(input, options);
}
catch (error)
{
console.error('%s', error.message);
}
return output;
};
if (tryEncode('') == null) // validate options
fail();
var readline = require('readline');
var rl = readline.createInterface({ input: process.stdin, terminal: false });
rl.on
(
'line',
function (input)
{
if (input)
{
var output = tryEncode(input);
if (output != null)
console.log(output);
}
prompt();
}
);
prompt();
}
else
{
var fs = require('fs');
var timeUtils = require('./tools/time-utils');
var input;
var output;
var encodingTime;
try
{
input = fs.readFileSync(inputFileName);
encodingTime =
timeUtils.timeThis
(
function ()
{
output = JScrewIt.encode(input, options);
}
);
if (outputFileName != null)
fs.writeFileSync(outputFileName, output);
else
console.log(output);
}
catch (error)
{
console.error('%s', error.message);
fail();
}
if (outputFileName != null)
{
var perfInfo = options.perfInfo;
var perfLog = perfInfo && perfInfo.perfLog;
if (perfLog)
{
var diagnosticReport = cli.createDiagnosticReport(perfLog);
console.log(diagnosticReport);
}
var report = cli.createReport(input.length, output.length, encodingTime);
console.log(report);
}
}
}
)();