@danielx/civet 0.6.68 → 0.6.70

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/dist/civet CHANGED
@@ -28,15 +28,9 @@ var import_main = require("./main.js");
28
28
  var import_config = require("./config.js");
29
29
  var import_promises = __toESM(require("fs/promises"));
30
30
  var import_path = __toESM(require("path"));
31
- var version;
32
- var parseArgs;
33
- var readFiles;
34
- var repl;
35
- var cli;
36
- var indexOf = [].indexOf;
37
- version = function() {
31
+ function version() {
38
32
  return require("../package.json").version;
39
- };
33
+ }
40
34
  if (process.argv.includes("--version")) {
41
35
  console.log(version());
42
36
  process.exit(0);
@@ -80,37 +74,35 @@ You can use - to read from stdin or (prefixed by -o) write to stdout.
80
74
  process.exit(0);
81
75
  }
82
76
  var encoding = "utf8";
83
- parseArgs = function(args) {
84
- var options, filenames, scriptArgs, i, endOfArgs, arg;
85
- options = {};
77
+ function parseArgs(args) {
78
+ const options = {};
86
79
  Object.defineProperty(options, "run", {
87
80
  get: function() {
88
81
  return !(this.ast || this.compile);
89
82
  }
90
83
  });
91
- filenames = [];
92
- scriptArgs = null;
93
- i = 0;
94
- endOfArgs = function(j) {
84
+ const filenames = [];
85
+ let scriptArgs = [];
86
+ let i = 0;
87
+ function endOfArgs(j) {
95
88
  i = args.length;
96
89
  if (j >= args.length) {
97
90
  return;
98
91
  }
99
92
  if (options.run) {
100
93
  filenames.push(args[j]);
101
- return scriptArgs = args.slice(j + 1);
94
+ scriptArgs = args.slice(j + 1);
102
95
  } else {
103
- return filenames.push(...args.slice(j));
96
+ filenames.push(...args.slice(j));
104
97
  }
105
- };
98
+ }
106
99
  while (i < args.length) {
107
- arg = args[i];
100
+ const arg = args[i];
108
101
  if (/^-\w{2,}$/.test(arg)) {
109
102
  args.splice(i, 1 + i - i, ...(() => {
110
- var char;
111
103
  const results = [];
112
104
  for (let ref = arg.slice(1), i1 = 0, len = ref.length; i1 < len; i1++) {
113
- char = ref[i1];
105
+ const char = ref[i1];
114
106
  results.push(`-${char}`);
115
107
  }
116
108
  return results;
@@ -178,14 +170,14 @@ parseArgs = function(args) {
178
170
  i++;
179
171
  }
180
172
  return { filenames, scriptArgs, options };
181
- };
182
- readFiles = async function* (filenames, options) {
183
- var filename, stdin, lines, rl, content;
173
+ }
174
+ async function* readFiles(filenames) {
184
175
  const results1 = [];
185
176
  for (let i2 = 0, len1 = filenames.length; i2 < len1; i2++) {
186
- filename = filenames[i2];
187
- stdin = filename === "-";
177
+ let filename = filenames[i2];
178
+ const stdin = filename === "-";
188
179
  try {
180
+ let content;
189
181
  if (stdin) {
190
182
  process.stdin.setEncoding(encoding);
191
183
  filename = "<stdin>";
@@ -194,24 +186,21 @@ readFiles = async function* (filenames, options) {
194
186
  } catch (e) {
195
187
  }
196
188
  if (process.stdin.isTTY) {
197
- lines = [];
198
- rl = require("readline").createInterface(process.stdin, process.stdout);
199
- rl.on("line", function(buffer) {
200
- return lines.push(buffer + "\n");
201
- });
202
- content = await new Promise(function(resolve, reject) {
203
- rl.on("SIGINT", function() {
189
+ const lines = [];
190
+ const rl = (await import("readline")).createInterface(process.stdin, process.stdout);
191
+ rl.on("line", (buffer) => lines.push(buffer + "\n"));
192
+ content = await new Promise((resolve, reject) => {
193
+ rl.on("SIGINT", () => {
204
194
  return reject("^C");
205
195
  });
206
- return rl.on("close", function() {
196
+ return rl.on("close", () => {
207
197
  return resolve(lines.join(""));
208
198
  });
209
199
  });
210
200
  } else {
211
201
  content = (await (async () => {
212
- var chunk;
213
202
  const results2 = [];
214
- for await (chunk of process.stdin) {
203
+ for await (const chunk of process.stdin) {
215
204
  results2.push(chunk);
216
205
  }
217
206
  return results2;
@@ -227,9 +216,8 @@ readFiles = async function* (filenames, options) {
227
216
  }
228
217
  ;
229
218
  return results1;
230
- };
231
- repl = function(options) {
232
- var nodeRepl, vm, r;
219
+ }
220
+ async function repl(options) {
233
221
  console.log(`Civet ${version()} REPL. Enter a blank line to ${(() => {
234
222
  switch (false) {
235
223
  case !options.ast: {
@@ -243,12 +231,10 @@ repl = function(options) {
243
231
  }
244
232
  }
245
233
  })()} code.`);
246
- global.quit = global.exit = function() {
247
- return process.exit(0);
248
- };
249
- nodeRepl = require("repl");
250
- vm = require("vm");
251
- return r = nodeRepl.start({
234
+ global.quit = global.exit = () => process.exit(0);
235
+ const nodeRepl = await import("repl");
236
+ const vm = await import("vm");
237
+ const r = nodeRepl.start({
252
238
  prompt: (() => {
253
239
  switch (false) {
254
240
  case !options.ast: {
@@ -262,53 +248,54 @@ repl = function(options) {
262
248
  }
263
249
  }
264
250
  })(),
265
- writer: options.ast ? function(obj) {
251
+ writer: options.ast ? (obj) => {
266
252
  try {
267
253
  return JSON.stringify(obj, null, 2);
268
254
  } catch (e) {
269
255
  console.log(`Failed to stringify: ${e}`);
270
- return obj;
256
+ return "";
271
257
  }
272
- } : options.compile ? function(obj) {
258
+ } : options.compile ? (obj) => {
273
259
  if (typeof obj === "string") {
274
260
  return obj?.replace(/\n*$/, "");
275
261
  } else {
276
- return obj;
262
+ return "";
277
263
  }
278
264
  } : void 0,
279
265
  eval: function(input, context, filename, callback) {
280
- var output, result;
281
266
  if (input === "\n") {
282
- return callback(null);
283
- } else if (indexOf.call(["quit\n", "exit\n", "quit()\n", "exit()\n"], input) >= 0) {
267
+ return callback(null, void 0);
268
+ } else if (input in ["quit\n", "exit\n", "quit()\n", "exit()\n"]) {
284
269
  return process.exit(0);
285
270
  } else if (input.endsWith("\n\n")) {
271
+ let output;
286
272
  try {
287
273
  output = (0, import_main.compile)(input, { ...options, filename });
288
274
  } catch (error) {
289
275
  console.error(error);
290
- return callback("");
276
+ return callback(null, void 0);
291
277
  }
292
278
  if (options.compile || options.ast) {
293
279
  return callback(null, output);
294
280
  } else {
281
+ let result;
295
282
  try {
296
283
  result = vm.runInContext(output, context, { filename });
297
284
  } catch (error) {
298
- return callback(error);
285
+ return callback(error, void 0);
299
286
  }
300
287
  return callback(null, result);
301
288
  }
302
289
  } else {
303
- return callback(new nodeRepl.Recoverable("Enter a blank line to execute code."));
290
+ return callback(new nodeRepl.Recoverable(new Error("Enter a blank line to execute code.")), null);
304
291
  }
305
292
  }
306
293
  });
307
- };
308
- cli = async function() {
309
- var argv, filenames, scriptArgs, options, filename, error, content, stdin, output, outputPath, outputFilename, optionsPath, stat, esm, execArgv, debugRe, isDebug, child;
310
- argv = process.argv;
311
- ({ filenames, scriptArgs, options } = parseArgs(argv.slice(2)));
294
+ return r;
295
+ }
296
+ async function cli() {
297
+ const argv = process.argv;
298
+ let { filenames, scriptArgs, options } = parseArgs(argv.slice(2));
312
299
  if (options.config !== false) {
313
300
  options.config ??= await (0, import_config.findConfig)(process.cwd());
314
301
  }
@@ -334,12 +321,13 @@ cli = async function() {
334
321
  return repl(options);
335
322
  }
336
323
  const results3 = [];
337
- for await ({ filename, error, content, stdin } of readFiles(filenames, options)) {
324
+ for await (const { filename, error, content, stdin } of readFiles(filenames)) {
338
325
  if (error) {
339
326
  console.error(`${filename} failed to load:`);
340
327
  console.error(error);
341
328
  continue;
342
329
  }
330
+ let output;
343
331
  try {
344
332
  output = (0, import_main.compile)(content, { ...options, filename });
345
333
  } catch (error2) {
@@ -352,7 +340,7 @@ cli = async function() {
352
340
  if (stdin && !options.output || options.output === "-") {
353
341
  results3.push(process.stdout.write(output));
354
342
  } else {
355
- outputPath = import_path.default.parse(filename);
343
+ let outputPath = import_path.default.parse(filename);
356
344
  delete outputPath.base;
357
345
  if (options.js) {
358
346
  outputPath.ext += ".jsx";
@@ -360,7 +348,8 @@ cli = async function() {
360
348
  outputPath.ext += ".tsx";
361
349
  }
362
350
  if (options.output) {
363
- optionsPath = import_path.default.parse(options.output);
351
+ const optionsPath = import_path.default.parse(options.output);
352
+ let stat;
364
353
  try {
365
354
  stat = await import_promises.default.stat(options.output);
366
355
  } catch {
@@ -380,7 +369,7 @@ cli = async function() {
380
369
  if (outputPath.dir) {
381
370
  import_promises.default.mkdir(outputPath.dir, { recursive: true });
382
371
  }
383
- outputFilename = import_path.default.format(outputPath);
372
+ const outputFilename = import_path.default.format(outputPath);
384
373
  try {
385
374
  results3.push(await import_promises.default.writeFile(outputFilename, output));
386
375
  } catch (error2) {
@@ -389,35 +378,35 @@ cli = async function() {
389
378
  }
390
379
  }
391
380
  } else {
392
- esm = /^\s*(import|export)\b/m.test(output);
381
+ const esm = /^\s*(import|export)\b/m.test(output);
393
382
  if (esm) {
394
383
  if (stdin) {
395
- filename = `.stdin-${process.pid}.civet`;
384
+ const filename2 = `.stdin-${process.pid}.civet`;
396
385
  try {
397
- await import_promises.default.writeFile(filename, content, { encoding });
386
+ await import_promises.default.writeFile(filename2, content, { encoding });
398
387
  } catch (e) {
399
- console.error(`Could not write ${filename} for Civet ESM mode:`);
388
+ console.error(`Could not write ${filename2} for Civet ESM mode:`);
400
389
  console.error(e);
401
390
  process.exit(1);
402
391
  }
403
392
  }
404
393
  const { fork } = await import("child_process");
405
- execArgv = ["--loader", "@danielx/civet/esm"];
406
- debugRe = /--debug|--inspect/;
407
- isDebug = typeof v8debug === "object" || debugRe.test(process.execArgv.join(" ")) || debugRe.test(process.env.NODE_OPTIONS);
394
+ const execArgv = ["--loader", "@danielx/civet/esm"];
395
+ const debugRe = /--debug|--inspect/;
396
+ const isDebug = typeof v8debug === "object" || debugRe.test(process.execArgv.join(" ")) || debugRe.test(process.env.NODE_OPTIONS ?? "");
408
397
  if (process.env.NODE_OPTIONS) {
409
398
  execArgv.push(process.env.NODE_OPTIONS);
410
399
  }
411
400
  if (isDebug) {
412
401
  execArgv.push("--inspect=" + (process.debugPort + 1));
413
402
  }
414
- child = fork(filename, [
403
+ const child = fork(filename, [
415
404
  ...scriptArgs
416
405
  ], {
417
406
  execArgv,
418
407
  stdio: "inherit"
419
408
  });
420
- results3.push(child.on("exit", async function(code) {
409
+ results3.push(child.on("exit", async (code) => {
421
410
  if (stdin) {
422
411
  await import_promises.default.unlink(filename);
423
412
  }
@@ -444,7 +433,7 @@ cli = async function() {
444
433
  }
445
434
  ;
446
435
  return results3;
447
- };
436
+ }
448
437
  if (require.main === module) {
449
438
  cli();
450
439
  }