jscrewit 2.41.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/screw.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- /* eslint-env node */
4
-
5
3
  'use strict';
6
4
 
5
+ const cli = require('./tools/cli');
6
+
7
7
  function fail()
8
8
  {
9
9
  process.exit(1);
@@ -11,21 +11,21 @@ function fail()
11
11
 
12
12
  function getBasename()
13
13
  {
14
- var path = require('path');
15
- var basename = path.basename(process.argv[1]);
14
+ const path = require('node:path');
15
+ const basename = path.basename(process.argv[1]);
16
16
  return basename;
17
17
  }
18
18
 
19
19
  function printErrorMessage(errorMessage)
20
20
  {
21
- var basename = getBasename();
21
+ const basename = getBasename();
22
22
  console.error
23
23
  ('%s: %s.\nTry "%s --help" for more information.', basename, errorMessage, basename);
24
24
  }
25
25
 
26
26
  function printHelpMessage()
27
27
  {
28
- var message =
28
+ const message =
29
29
  'Usage: %s [OPTION]... [SOURCE [DESTINATION]]\n' +
30
30
  'Encodes JavaScript with JScrewIt.\n' +
31
31
  '\n' +
@@ -55,14 +55,14 @@ function printHelpMessage()
55
55
  ' none (none available)\n' +
56
56
  '\n' +
57
57
  'See the JScrewIt feature documentation for a list of all supported features.\n';
58
- var basename = getBasename();
58
+ const basename = getBasename();
59
59
  console.log(message, basename);
60
60
  }
61
61
 
62
62
  function printVersion()
63
63
  {
64
- var version = require('./package.json').version;
65
- console.log('JScrewIt ' + version);
64
+ const { version } = require('./package.json');
65
+ console.log(`JScrewIt ${version}`);
66
66
  }
67
67
 
68
68
  function prompt()
@@ -70,114 +70,98 @@ function prompt()
70
70
  process.stdout.write('SCREW> ');
71
71
  }
72
72
 
73
- (function ()
73
+ let command;
74
+ try
74
75
  {
75
- var cli = require('./tools/cli');
76
-
77
- var command;
78
- try
79
- {
80
- command = cli.parseCommandLine(process.argv);
81
- }
82
- catch (error)
83
- {
84
- printErrorMessage(error.message);
85
- fail();
86
- }
87
- if (command === 'help')
88
- {
89
- printHelpMessage();
90
- return;
91
- }
92
- if (command === 'version')
93
- {
94
- printVersion();
95
- return;
96
- }
97
-
98
- var inputFileName = command.inputFileName;
99
- var outputFileName = command.outputFileName;
100
- var options = command.options;
76
+ command = cli.parseCommandLine(process.argv);
77
+ }
78
+ catch (error)
79
+ {
80
+ printErrorMessage(error.message);
81
+ fail();
82
+ }
83
+ if (command === 'help')
84
+ {
85
+ printHelpMessage();
86
+ return;
87
+ }
88
+ if (command === 'version')
89
+ {
90
+ printVersion();
91
+ return;
92
+ }
101
93
 
102
- var JScrewIt = require('./'); // '.' doesn't work in Node.js 0.10.
94
+ const { inputFileName, outputFileName, options } = command;
95
+ const JScrewIt = require('.');
103
96
 
104
- if (inputFileName == null)
105
- {
106
- var tryEncode =
107
- function (input)
108
- {
109
- var output;
110
- try
111
- {
112
- output = JScrewIt.encode(input, options);
113
- }
114
- catch (error)
115
- {
116
- console.error('%s', error.message);
117
- }
118
- return output;
119
- };
120
- if (tryEncode('') == null) // validate options
121
- fail();
122
- var readline = require('readline');
123
- var rl = readline.createInterface({ input: process.stdin, terminal: false });
124
- rl.on
125
- (
126
- 'line',
127
- function (input)
128
- {
129
- if (input)
130
- {
131
- var output = tryEncode(input);
132
- if (output != null)
133
- console.log(output);
134
- }
135
- prompt();
136
- }
137
- );
138
- prompt();
139
- }
140
- else
97
+ if (inputFileName == null)
98
+ {
99
+ const tryEncode =
100
+ input =>
141
101
  {
142
- var fs = require('fs');
143
- var timeUtils = require('./tools/time-utils');
144
-
145
- var input;
146
- var output;
147
- var encodingTime;
102
+ let output;
148
103
  try
149
104
  {
150
- input = fs.readFileSync(inputFileName);
151
- encodingTime =
152
- timeUtils.timeThis
153
- (
154
- function ()
155
- {
156
- output = JScrewIt.encode(input, options);
157
- }
158
- );
159
- if (outputFileName != null)
160
- fs.writeFileSync(outputFileName, output);
161
- else
162
- console.log(output);
105
+ output = JScrewIt.encode(input, options);
163
106
  }
164
107
  catch (error)
165
108
  {
166
109
  console.error('%s', error.message);
167
- fail();
168
110
  }
169
- if (outputFileName != null)
111
+ return output;
112
+ };
113
+ if (tryEncode('') == null) // validate options
114
+ fail();
115
+ const { createInterface } = require('node:readline');
116
+ const rl = createInterface({ input: process.stdin, terminal: false });
117
+ rl.on
118
+ (
119
+ 'line',
120
+ input =>
170
121
  {
171
- var perfInfo = options.perfInfo;
172
- var perfLog = perfInfo && perfInfo.perfLog;
173
- if (perfLog)
122
+ if (input)
174
123
  {
175
- var diagnosticReport = cli.createDiagnosticReport(perfLog);
176
- console.log(diagnosticReport);
124
+ const output = tryEncode(input);
125
+ if (output != null)
126
+ console.log(output);
177
127
  }
178
- var report = cli.createReport(input.length, output.length, encodingTime);
179
- console.log(report);
128
+ prompt();
129
+ },
130
+ );
131
+ prompt();
132
+ }
133
+ else
134
+ {
135
+ const fs = require('node:fs');
136
+ const timeUtils = require('./tools/time-utils');
137
+
138
+ let input;
139
+ let output;
140
+ let encodingTime;
141
+ try
142
+ {
143
+ input = fs.readFileSync(inputFileName);
144
+ encodingTime =
145
+ timeUtils.timeThis(() => { output = JScrewIt.encode(input, options); });
146
+ if (outputFileName != null)
147
+ fs.writeFileSync(outputFileName, output);
148
+ else
149
+ console.log(output);
150
+ }
151
+ catch (error)
152
+ {
153
+ console.error('%s', error.message);
154
+ fail();
155
+ }
156
+ if (outputFileName != null)
157
+ {
158
+ const perfLog = options.perfInfo?.perfLog;
159
+ if (perfLog)
160
+ {
161
+ const diagnosticReport = cli.createDiagnosticReport(perfLog);
162
+ console.log(diagnosticReport);
180
163
  }
164
+ const report = cli.createReport(input.length, output.length, encodingTime);
165
+ console.log(report);
181
166
  }
182
167
  }
183
- )();
package/tools/cli.js CHANGED
@@ -1,66 +1,56 @@
1
- /* eslint-env node */
2
- /* global padLeft, padRight, repeat */
3
-
4
1
  'use strict';
5
2
 
6
- require('./text-utils');
7
- var timeUtils = require('./time-utils');
3
+ const { parseArgs } = require('node:util');
4
+ const timeUtils = require('./time-utils');
8
5
 
9
6
  function byteCount(size, width)
10
7
  {
11
- var str = padLeft(size, width) + (size === 1 ? ' byte' : ' bytes');
8
+ const str = `${String(size).padStart(width)}${size === 1 ? ' byte' : ' bytes'}`;
12
9
  return str;
13
10
  }
14
11
 
15
12
  function createDiagnosticReport(perfLog)
16
13
  {
17
- var report =
18
- '\nStrategy Status Length Time (ms)\n' +
19
- repeat('─', 60) + '\n' +
20
- perfLog.reduce
21
- (
22
- function (str, perfInfoList)
23
- {
24
- str += formatPerfInfoList(perfInfoList, '', ['', '']);
25
- return str;
26
- },
27
- ''
28
- );
14
+ const reportParts =
15
+ perfLog.map(perfInfoList => formatPerfInfoList(perfInfoList, '', ['', '']));
16
+ reportParts.unshift
17
+ (`\nStrategy Status Length Time (ms)\n${'─'.repeat(60)}\n`);
18
+ const report = reportParts.join('');
29
19
  return report;
30
20
  }
31
21
 
32
22
  function createReport(originalSize, screwedSize, encodingTime)
33
23
  {
34
- var width = Math.max(widthOf(originalSize), widthOf(screwedSize));
35
- var expansionFactorStr = originalSize ? (screwedSize / originalSize).toFixed(2) : '-';
36
- var encodingTimeStr = timeUtils.formatDuration(encodingTime);
37
- var report =
38
- 'Original size: ' + byteCount(originalSize, width) +
39
- '\nScrewed size: ' + byteCount(screwedSize, width) +
40
- '\nExpansion factor: ' + expansionFactorStr +
41
- '\nEncoding time: ' + encodingTimeStr;
24
+ const width = Math.max(widthOf(originalSize), widthOf(screwedSize));
25
+ const expansionFactorStr = originalSize ? (screwedSize / originalSize).toFixed(2) : '-';
26
+ const encodingTimeStr = timeUtils.formatDuration(encodingTime);
27
+ const report =
28
+ `Original size: ${byteCount(originalSize, width)
29
+ }\nScrewed size: ${byteCount(screwedSize, width)
30
+ }\nExpansion factor: ${expansionFactorStr
31
+ }\nEncoding time: ${encodingTimeStr}`;
42
32
  return report;
43
33
  }
44
34
 
45
35
  function formatCodingLog(perfLog, padding, nextCodingLog)
46
36
  {
47
37
  padding += nextCodingLog ? '│' : ' ';
48
- var str = '';
49
- var count = perfLog.length;
50
- for (var index = 0; index < count; ++index)
38
+ let str = '';
39
+ const count = perfLog.length;
40
+ for (let index = 0; index < count; ++index)
51
41
  {
52
- var perfInfoList = perfLog[index];
53
- var nextPerfInfoList = index < count - 1;
42
+ const perfInfoList = perfLog[index];
43
+ const nextPerfInfoList = index < count - 1;
54
44
  str += formatPerfInfoList(perfInfoList, padding, nextPerfInfoList ? '├│' : '└ ');
55
45
  }
56
46
  if (nextCodingLog)
57
- str += padding + '\n';
47
+ str += `${padding}\n`;
58
48
  return str;
59
49
  }
60
50
 
61
51
  function formatInt(int)
62
52
  {
63
- var str = int === undefined ? '-' : int;
53
+ const str = int === undefined ? '-' : String(int);
64
54
  return str;
65
55
  }
66
56
 
@@ -68,141 +58,82 @@ function formatPerfInfoList(perfInfoList, padding, paddingChars)
68
58
  {
69
59
  // In the current implementation, perfInfoList.name can be either undefined, a unit path or
70
60
  // "legend".
71
- var str = padding + paddingChars[0] + (perfInfoList.name || '(default)') + '\n';
61
+ let str = `${padding}${paddingChars[0]}${perfInfoList.name || '(default)'}\n`;
72
62
  padding += paddingChars[1];
73
- var count = perfInfoList.length;
74
- var paddingLength = padding.length;
75
- var perfLog;
76
- for (var index = 0; index < count; ++index)
63
+ const count = perfInfoList.length;
64
+ const paddingLength = padding.length;
65
+ for (let index = 0; index < count; ++index)
77
66
  {
78
- var perfInfo = perfInfoList[index];
79
- var next = index < count - 1;
67
+ const perfInfo = perfInfoList[index];
68
+ const next = index < count - 1;
80
69
  str +=
81
- padding + (next ? '├' : '└') +
82
- padRight(perfInfo.strategyName, 27 - paddingLength) +
83
- padRight(perfInfo.status, 10) +
84
- padLeft(formatInt(perfInfo.outputLength), 11) +
85
- padLeft(formatInt(perfInfo.time), 11) +
86
- '\n';
87
- perfLog = perfInfo.perfLog;
70
+ `${padding
71
+ }${next ? '├' : '└'
72
+ }${perfInfo.strategyName.padEnd(27 - paddingLength)
73
+ }${perfInfo.status.padEnd(10)
74
+ }${formatInt(perfInfo.outputLength).padStart(11)
75
+ }${formatInt(perfInfo.time).padStart(11)
76
+ }\n`;
77
+ const { perfLog } = perfInfo;
88
78
  if (perfLog)
89
79
  str += formatCodingLog(perfLog, padding, next);
90
80
  }
91
81
  return str;
92
82
  }
93
83
 
94
- function parseCommandLine(argv)
84
+ function parseCommandLine([,, ...args])
95
85
  {
96
- function parseFeatures()
97
- {
98
- var arg2 = argv[++index];
99
- if (arg2 === undefined)
100
- throw Error('option ' + quote(arg) + ' requires an argument');
101
- options.features = arg2.trim().split(/(?:\s+|\s*,\s*)/);
102
- }
103
-
104
- function parseFlag(char)
105
- {
106
- switch (char)
107
- {
108
- case 'c':
109
- case 'w':
110
- wrapMode = 'call';
111
- break;
112
- case 'd':
113
- options.perfInfo = { };
114
- break;
115
- case 'e':
116
- wrapMode = 'eval';
117
- break;
118
- case 't':
119
- options.trimCode = true;
120
- break;
121
- case 'x':
122
- express = true;
123
- break;
124
- default:
125
- throw Error('unrecognized flag ' + quote(char));
126
- }
127
- }
128
-
129
- function parseRunAs()
130
- {
131
- var arg2 = argv[++index];
132
- if (arg2 === undefined)
133
- throw Error('option ' + quote(arg) + ' requires an argument');
134
- options.runAs = arg2;
135
- }
136
-
137
- var inputFileName;
138
- var outputFileName;
139
- var options = { };
140
- var arg;
141
- var express;
142
- var wrapMode;
143
-
144
- for (var index = 2; index < argv.length; ++index)
145
- {
146
- arg = argv[index];
147
- var flag;
148
- if (/^--/.test(arg))
86
+ const parsed =
87
+ parseArgs
88
+ (
149
89
  {
150
- flag = arg.slice(2);
151
- switch (flag)
90
+ args,
91
+ options:
152
92
  {
153
- case 'diagnostic':
154
- options.perfInfo = { };
155
- break;
156
- case 'features':
157
- parseFeatures();
158
- break;
159
- case 'help':
160
- case 'version':
161
- return flag;
162
- case 'run-as':
163
- case 'wrap-with':
164
- parseRunAs();
165
- break;
166
- case 'trim-code':
167
- options.trimCode = true;
168
- break;
169
- default:
170
- throw Error('unrecognized option ' + quote(arg));
171
- }
172
- }
173
- else if (/^-/.test(arg))
174
- {
175
- flag = arg.slice(1);
176
- if (flag === 'f')
177
- parseFeatures();
178
- else if (flag === 'r')
179
- parseRunAs();
180
- else
181
- flag.split('').forEach(parseFlag);
182
- }
183
- else
184
- {
185
- if (outputFileName != null)
186
- throw Error('unexpected argument ' + quote(arg));
187
- if (inputFileName != null)
188
- outputFileName = arg;
189
- else
190
- inputFileName = arg;
191
- }
192
- }
193
- if (!options.runAs)
93
+ 'call': { type: 'boolean', short: 'c' },
94
+ 'diagnostic': { type: 'boolean', short: 'd' },
95
+ 'eval-flag': { type: 'boolean', short: 'e' },
96
+ 'express': { type: 'boolean', short: 'x' },
97
+ 'features': { type: 'string', short: 'f' },
98
+ 'help': { type: 'boolean' },
99
+ 'run-as': { type: 'string', short: 'r' },
100
+ 'trim-code': { type: 'boolean', short: 't' },
101
+ 'version': { type: 'boolean', short: 'v' },
102
+ 'wrap': { type: 'boolean', short: 'w' },
103
+ },
104
+ allowPositionals: true,
105
+ strict: true,
106
+ },
107
+ );
108
+ const { values, positionals } = parsed;
109
+ if (values.help)
110
+ return 'help';
111
+ if (values.version)
112
+ return 'version';
113
+ if (positionals.length > 2)
114
+ throw Error(`Unexpected argument "${positionals[2]}"`);
115
+ const [inputFileName, outputFileName] = positionals;
116
+ const options = { };
117
+ if (values.diagnostic)
118
+ options.perfInfo = { };
119
+ const { features } = values;
120
+ if (features != null)
121
+ options.features = features.trim().split(/(?:\s+|\s*,\s*)/);
122
+ if (values['trim-code'])
123
+ options.trimCode = true;
124
+ const runAs = values['run-as'];
125
+ if (runAs != null)
126
+ options.runAs = runAs;
127
+ else
194
128
  {
195
- var runAs = (express ? ['express'] : []).concat(wrapMode || []).join('-');
129
+ const { express } = values;
130
+ const wrapMode =
131
+ values.call ?? values.wrap ? 'call' : values['eval-flag'] ? 'eval' : undefined;
132
+ const runAs = (express ? ['express'] : []).concat(wrapMode ?? []).join('-');
196
133
  if (runAs)
197
134
  options.runAs = runAs;
198
135
  }
199
- var result = { inputFileName: inputFileName, outputFileName: outputFileName, options: options };
200
- return result;
201
- }
202
-
203
- function quote(arg)
204
- {
205
- return '"' + arg + '"';
136
+ return { inputFileName, outputFileName, options };
206
137
  }
207
138
 
208
139
  function widthOf(size)
@@ -210,9 +141,4 @@ function widthOf(size)
210
141
  return String(size).length;
211
142
  }
212
143
 
213
- module.exports =
214
- {
215
- createDiagnosticReport: createDiagnosticReport,
216
- createReport: createReport,
217
- parseCommandLine: parseCommandLine,
218
- };
144
+ module.exports = { createDiagnosticReport, createReport, parseCommandLine };
@@ -1,44 +1,28 @@
1
- /* eslint-env node */
2
- /* global Promise */
3
-
4
1
  'use strict';
5
2
 
6
3
  exports.formatDuration =
7
4
  function (duration)
8
5
  {
9
- var str = duration < 5e-3 ? '< 0.01 s' : duration.toFixed(2) + ' s';
6
+ const str = duration < 5e-3 ? '< 0.01 s' : `${duration.toFixed(2)} s`;
10
7
  return str;
11
8
  };
12
9
 
13
10
  exports.timeThis =
14
11
  function (fn)
15
12
  {
16
- var begin = process.hrtime();
13
+ const begin = process.hrtime();
17
14
  fn();
18
- var time = process.hrtime(begin);
19
- var duration = time[0] + time[1] / 1e9;
15
+ const time = process.hrtime(begin);
16
+ const duration = time[0] + time[1] / 1e9;
20
17
  return duration;
21
18
  };
22
19
 
23
20
  exports.timeThisAsync =
24
- function (fn)
21
+ async function (fn)
25
22
  {
26
- var begin = process.hrtime();
27
- var executor =
28
- function (resolve, reject)
29
- {
30
- fn()
31
- .then
32
- (
33
- function ()
34
- {
35
- var time = process.hrtime(begin);
36
- var duration = time[0] + time[1] / 1e9;
37
- resolve(duration);
38
- }
39
- )
40
- .catch(reject);
41
- };
42
- var promise = new Promise(executor);
43
- return promise;
23
+ const begin = process.hrtime();
24
+ await fn();
25
+ const time = process.hrtime(begin);
26
+ const duration = time[0] + time[1] / 1e9;
27
+ return duration;
44
28
  };