@danielx/civet 0.6.69 → 0.6.71

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);
@@ -68,6 +62,7 @@ Options:
68
62
  -o / --output XX Specify output directory and/or extension, or filename
69
63
  -c / --compile Compile input files to TypeScript (or JavaScript)
70
64
  --config XX Specify a config file (default scans for a config.civet, civet.json, civetconfig.civet or civetconfig.json file, optionally in a .config directory, or starting with a .)
65
+ --civet XX Specify civet compiler flag, as in "civet XX" prologue
71
66
  --no-config Don't scan for a config file
72
67
  --js Strip out all type annotations; default to .jsx extension
73
68
  --ast Print the AST instead of the compiled code
@@ -76,41 +71,42 @@ Options:
76
71
 
77
72
  You can use - to read from stdin or (prefixed by -o) write to stdout.
78
73
 
74
+ By default, .civet imports get rewritten to use the output extension.
75
+ You can override this behavior via: --civet rewriteCivetImports=.ext
76
+
79
77
  `);
80
78
  process.exit(0);
81
79
  }
82
80
  var encoding = "utf8";
83
- parseArgs = function(args) {
84
- var options, filenames, scriptArgs, i, endOfArgs, arg;
85
- options = {};
81
+ function parseArgs(args) {
82
+ const options = {};
86
83
  Object.defineProperty(options, "run", {
87
84
  get: function() {
88
85
  return !(this.ast || this.compile);
89
86
  }
90
87
  });
91
- filenames = [];
92
- scriptArgs = null;
93
- i = 0;
94
- endOfArgs = function(j) {
88
+ const filenames = [];
89
+ let scriptArgs = [];
90
+ let i = 0;
91
+ function endOfArgs(j) {
95
92
  i = args.length;
96
93
  if (j >= args.length) {
97
94
  return;
98
95
  }
99
96
  if (options.run) {
100
97
  filenames.push(args[j]);
101
- return scriptArgs = args.slice(j + 1);
98
+ scriptArgs = args.slice(j + 1);
102
99
  } else {
103
- return filenames.push(...args.slice(j));
100
+ filenames.push(...args.slice(j));
104
101
  }
105
- };
102
+ }
106
103
  while (i < args.length) {
107
- arg = args[i];
104
+ const arg = args[i];
108
105
  if (/^-\w{2,}$/.test(arg)) {
109
106
  args.splice(i, 1 + i - i, ...(() => {
110
- var char;
111
107
  const results = [];
112
108
  for (let ref = arg.slice(1), i1 = 0, len = ref.length; i1 < len; i1++) {
113
- char = ref[i1];
109
+ const char = ref[i1];
114
110
  results.push(`-${char}`);
115
111
  }
116
112
  return results;
@@ -136,6 +132,15 @@ parseArgs = function(args) {
136
132
  options.config = false;
137
133
  break;
138
134
  }
135
+ case "--civet": {
136
+ Object.assign(
137
+ options.parseOptions ??= {},
138
+ (0, import_main.parse)(`civet ${args[++i]}`, {
139
+ startRule: "CivetPrologueContent"
140
+ }).config
141
+ );
142
+ break;
143
+ }
139
144
  case "--ast": {
140
145
  options.ast = true;
141
146
  break;
@@ -178,14 +183,14 @@ parseArgs = function(args) {
178
183
  i++;
179
184
  }
180
185
  return { filenames, scriptArgs, options };
181
- };
182
- readFiles = async function* (filenames, options) {
183
- var filename, stdin, lines, rl, content;
186
+ }
187
+ async function* readFiles(filenames) {
184
188
  const results1 = [];
185
189
  for (let i2 = 0, len1 = filenames.length; i2 < len1; i2++) {
186
- filename = filenames[i2];
187
- stdin = filename === "-";
190
+ let filename = filenames[i2];
191
+ const stdin = filename === "-";
188
192
  try {
193
+ let content;
189
194
  if (stdin) {
190
195
  process.stdin.setEncoding(encoding);
191
196
  filename = "<stdin>";
@@ -194,24 +199,21 @@ readFiles = async function* (filenames, options) {
194
199
  } catch (e) {
195
200
  }
196
201
  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() {
202
+ const lines = [];
203
+ const rl = (await import("readline")).createInterface(process.stdin, process.stdout);
204
+ rl.on("line", (buffer) => lines.push(buffer + "\n"));
205
+ content = await new Promise((resolve, reject) => {
206
+ rl.on("SIGINT", () => {
204
207
  return reject("^C");
205
208
  });
206
- return rl.on("close", function() {
209
+ return rl.on("close", () => {
207
210
  return resolve(lines.join(""));
208
211
  });
209
212
  });
210
213
  } else {
211
214
  content = (await (async () => {
212
- var chunk;
213
215
  const results2 = [];
214
- for await (chunk of process.stdin) {
216
+ for await (const chunk of process.stdin) {
215
217
  results2.push(chunk);
216
218
  }
217
219
  return results2;
@@ -227,9 +229,8 @@ readFiles = async function* (filenames, options) {
227
229
  }
228
230
  ;
229
231
  return results1;
230
- };
231
- repl = function(options) {
232
- var nodeRepl, vm, r;
232
+ }
233
+ async function repl(options) {
233
234
  console.log(`Civet ${version()} REPL. Enter a blank line to ${(() => {
234
235
  switch (false) {
235
236
  case !options.ast: {
@@ -243,12 +244,10 @@ repl = function(options) {
243
244
  }
244
245
  }
245
246
  })()} 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({
247
+ global.quit = global.exit = () => process.exit(0);
248
+ const nodeRepl = await import("repl");
249
+ const vm = await import("vm");
250
+ const r = nodeRepl.start({
252
251
  prompt: (() => {
253
252
  switch (false) {
254
253
  case !options.ast: {
@@ -262,53 +261,54 @@ repl = function(options) {
262
261
  }
263
262
  }
264
263
  })(),
265
- writer: options.ast ? function(obj) {
264
+ writer: options.ast ? (obj) => {
266
265
  try {
267
266
  return JSON.stringify(obj, null, 2);
268
267
  } catch (e) {
269
268
  console.log(`Failed to stringify: ${e}`);
270
- return obj;
269
+ return "";
271
270
  }
272
- } : options.compile ? function(obj) {
271
+ } : options.compile ? (obj) => {
273
272
  if (typeof obj === "string") {
274
273
  return obj?.replace(/\n*$/, "");
275
274
  } else {
276
- return obj;
275
+ return "";
277
276
  }
278
277
  } : void 0,
279
278
  eval: function(input, context, filename, callback) {
280
- var output, result;
281
279
  if (input === "\n") {
282
- return callback(null);
283
- } else if (indexOf.call(["quit\n", "exit\n", "quit()\n", "exit()\n"], input) >= 0) {
280
+ return callback(null, void 0);
281
+ } else if (input in ["quit\n", "exit\n", "quit()\n", "exit()\n"]) {
284
282
  return process.exit(0);
285
283
  } else if (input.endsWith("\n\n")) {
284
+ let output;
286
285
  try {
287
286
  output = (0, import_main.compile)(input, { ...options, filename });
288
287
  } catch (error) {
289
288
  console.error(error);
290
- return callback("");
289
+ return callback(null, void 0);
291
290
  }
292
291
  if (options.compile || options.ast) {
293
292
  return callback(null, output);
294
293
  } else {
294
+ let result;
295
295
  try {
296
296
  result = vm.runInContext(output, context, { filename });
297
297
  } catch (error) {
298
- return callback(error);
298
+ return callback(error, void 0);
299
299
  }
300
300
  return callback(null, result);
301
301
  }
302
302
  } else {
303
- return callback(new nodeRepl.Recoverable("Enter a blank line to execute code."));
303
+ return callback(new nodeRepl.Recoverable(new Error("Enter a blank line to execute code.")), null);
304
304
  }
305
305
  }
306
306
  });
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)));
307
+ return r;
308
+ }
309
+ async function cli() {
310
+ const argv = process.argv;
311
+ let { filenames, scriptArgs, options } = parseArgs(argv.slice(2));
312
312
  if (options.config !== false) {
313
313
  options.config ??= await (0, import_config.findConfig)(process.cwd());
314
314
  }
@@ -333,13 +333,37 @@ cli = async function() {
333
333
  if (options.repl) {
334
334
  return repl(options);
335
335
  }
336
+ let outputDir, outputExt, outputPath;
337
+ if (options.output) {
338
+ const optionsPath = import_path.default.parse(options.output);
339
+ let stat;
340
+ try {
341
+ stat = await import_promises.default.stat(options.output);
342
+ } catch {
343
+ stat = null;
344
+ }
345
+ if (stat?.isDirectory() || options.output.endsWith(import_path.default.sep) || options.output.endsWith("/")) {
346
+ outputDir = options.output;
347
+ } else if (/^(\.[^.]+)+$/.test(optionsPath.base)) {
348
+ outputExt = optionsPath.base;
349
+ if (optionsPath.dir) {
350
+ outputDir = optionsPath.dir;
351
+ }
352
+ } else {
353
+ outputPath = optionsPath;
354
+ }
355
+ if (options.output !== "-") {
356
+ (options.parseOptions ??= {}).rewriteCivetImports ??= outputExt ?? ".civet" + (options.js ? ".jsx" : ".tsx");
357
+ }
358
+ }
336
359
  const results3 = [];
337
- for await ({ filename, error, content, stdin } of readFiles(filenames, options)) {
360
+ for await (const { filename, error, content, stdin } of readFiles(filenames)) {
338
361
  if (error) {
339
362
  console.error(`${filename} failed to load:`);
340
363
  console.error(error);
341
364
  continue;
342
365
  }
366
+ let output;
343
367
  try {
344
368
  output = (0, import_main.compile)(content, { ...options, filename });
345
369
  } catch (error2) {
@@ -352,72 +376,63 @@ cli = async function() {
352
376
  if (stdin && !options.output || options.output === "-") {
353
377
  results3.push(process.stdout.write(output));
354
378
  } else {
355
- outputPath = import_path.default.parse(filename);
356
- delete outputPath.base;
379
+ let targetPath = import_path.default.parse(filename);
380
+ delete targetPath.base;
357
381
  if (options.js) {
358
- outputPath.ext += ".jsx";
382
+ targetPath.ext += ".jsx";
359
383
  } else {
360
- outputPath.ext += ".tsx";
384
+ targetPath.ext += ".tsx";
361
385
  }
362
- if (options.output) {
363
- optionsPath = import_path.default.parse(options.output);
364
- try {
365
- stat = await import_promises.default.stat(options.output);
366
- } catch {
367
- stat = null;
368
- }
369
- if (stat?.isDirectory() || options.output.endsWith(import_path.default.sep) || options.output.endsWith("/")) {
370
- outputPath.dir = options.output;
371
- } else if (/^(\.[^.]+)+$/.test(optionsPath.base)) {
372
- outputPath.ext = optionsPath.base;
373
- if (optionsPath.dir) {
374
- outputPath.dir = optionsPath.dir;
375
- }
376
- } else {
377
- outputPath = optionsPath;
378
- }
386
+ if (outputDir != null) {
387
+ targetPath.dir = outputDir;
388
+ }
389
+ if (outputExt != null) {
390
+ targetPath.ext = outputExt;
379
391
  }
380
- if (outputPath.dir) {
381
- import_promises.default.mkdir(outputPath.dir, { recursive: true });
392
+ if (outputPath != null) {
393
+ targetPath = outputPath;
382
394
  }
383
- outputFilename = import_path.default.format(outputPath);
395
+ if (targetPath.dir) {
396
+ import_promises.default.mkdir(targetPath.dir, { recursive: true });
397
+ }
398
+ const targetFilename = import_path.default.format(targetPath);
384
399
  try {
385
- results3.push(await import_promises.default.writeFile(outputFilename, output));
400
+ results3.push(await import_promises.default.writeFile(targetFilename, output));
386
401
  } catch (error2) {
387
- console.error(`${outputFilename} failed to write:`);
402
+ console.error(`${targetFilename} failed to write:`);
388
403
  results3.push(console.error(error2));
389
404
  }
390
405
  }
391
406
  } else {
392
- esm = /^\s*(import|export)\b/m.test(output);
407
+ const esm = /^\s*(import|export)\b/m.test(output);
393
408
  if (esm) {
394
409
  if (stdin) {
395
- filename = `.stdin-${process.pid}.civet`;
410
+ const filename2 = `.stdin-${process.pid}.civet`;
396
411
  try {
397
- await import_promises.default.writeFile(filename, content, { encoding });
412
+ await import_promises.default.writeFile(filename2, content, { encoding });
398
413
  } catch (e) {
399
- console.error(`Could not write ${filename} for Civet ESM mode:`);
414
+ console.error(`Could not write ${filename2} for Civet ESM mode:`);
400
415
  console.error(e);
401
416
  process.exit(1);
402
417
  }
403
418
  }
404
419
  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);
420
+ const execArgv = ["--loader", "@danielx/civet/esm"];
421
+ const debugRe = /--debug|--inspect/;
422
+ const isDebug = typeof v8debug === "object" || debugRe.test(process.execArgv.join(" ")) || debugRe.test(process.env.NODE_OPTIONS ?? "");
408
423
  if (process.env.NODE_OPTIONS) {
409
424
  execArgv.push(process.env.NODE_OPTIONS);
410
425
  }
411
426
  if (isDebug) {
412
427
  execArgv.push("--inspect=" + (process.debugPort + 1));
413
428
  }
414
- child = fork(filename, [
429
+ const child = fork(filename, [
415
430
  ...scriptArgs
416
431
  ], {
417
432
  execArgv,
418
433
  stdio: "inherit"
419
434
  });
420
- results3.push(child.on("exit", async function(code) {
435
+ results3.push(child.on("exit", async (code) => {
421
436
  if (stdin) {
422
437
  await import_promises.default.unlink(filename);
423
438
  }
@@ -444,7 +459,7 @@ cli = async function() {
444
459
  }
445
460
  ;
446
461
  return results3;
447
- };
462
+ }
448
463
  if (require.main === module) {
449
464
  cli();
450
465
  }