@danielx/civet 0.6.86 → 0.6.88

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
@@ -6,6 +6,10 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
9
13
  var __copyProps = (to, from, except, desc) => {
10
14
  if (from && typeof from === "object" || typeof from === "function") {
11
15
  for (let key of __getOwnPropNames(from))
@@ -22,8 +26,17 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
26
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
27
  mod
24
28
  ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
30
 
26
31
  // source/cli.civet
32
+ var cli_exports = {};
33
+ __export(cli_exports, {
34
+ cli: () => cli,
35
+ parseArgs: () => parseArgs,
36
+ repl: () => repl,
37
+ version: () => version
38
+ });
39
+ module.exports = __toCommonJS(cli_exports);
27
40
  var import_main = require("./main.js");
28
41
  var import_config = require("./config.js");
29
42
  var import_unplugin = require("./unplugin");
@@ -244,6 +257,33 @@ async function* readFiles(filenames) {
244
257
  return results1;
245
258
  }
246
259
  async function repl(options) {
260
+ const vm = await import("vm");
261
+ let importModuleDynamically = vm.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER;
262
+ if (!importModuleDynamically) {
263
+ if (vm.SourceTextModule != null) {
264
+ const { pathToFileURL } = await import("url");
265
+ importModuleDynamically = (specifier) => {
266
+ if (/^\.\.?[/\\]/.test(specifier)) {
267
+ return import(pathToFileURL(import_path.default.join(process.cwd(), specifier)));
268
+ } else {
269
+ return import(specifier);
270
+ }
271
+ };
272
+ } else {
273
+ const execArgv = ["--experimental-vm-modules"];
274
+ const { register } = await import("module");
275
+ if (process.env.NODE_OPTIONS) {
276
+ execArgv.push(process.env.NODE_OPTIONS);
277
+ }
278
+ const { fork } = await import("child_process");
279
+ fork(__filename, process.argv.slice(2), {
280
+ execArgv,
281
+ stdio: "inherit"
282
+ });
283
+ return;
284
+ }
285
+ }
286
+ require("../register.js");
247
287
  console.log(`Civet ${version()} REPL. Enter a blank line to ${(() => {
248
288
  switch (false) {
249
289
  case !options.ast: {
@@ -259,7 +299,6 @@ async function repl(options) {
259
299
  })()} code.`);
260
300
  global.quit = global.exit = () => process.exit(0);
261
301
  const nodeRepl = await import("repl");
262
- const vm = await import("vm");
263
302
  const r = nodeRepl.start({
264
303
  prompt: (() => {
265
304
  switch (false) {
@@ -288,29 +327,66 @@ async function repl(options) {
288
327
  return "";
289
328
  }
290
329
  } : void 0,
291
- eval: function(input, context, filename, callback) {
330
+ eval: async function(input, context, filename, callback) {
292
331
  if (input === "\n") {
293
332
  return callback(null, void 0);
294
333
  } else if (input in ["quit\n", "exit\n", "quit()\n", "exit()\n"]) {
295
334
  return process.exit(0);
296
335
  } else if (input.endsWith("\n\n")) {
297
336
  let output;
337
+ if (options.compile || options.ast) {
338
+ try {
339
+ output = (0, import_main.compile)(input, { ...options, filename });
340
+ } catch (error) {
341
+ console.error(error);
342
+ return callback(null, void 0);
343
+ }
344
+ return callback(null, output);
345
+ }
346
+ let ast = (0, import_main.compile)(input, { ...options, filename, ast: true });
347
+ const topLevelAwait = import_main.lib.gatherRecursive(
348
+ ast,
349
+ ($) => $.type === "Await",
350
+ import_main.lib.isFunction
351
+ ).length > 0;
352
+ if (topLevelAwait) {
353
+ const [prologue, rest] = (0, import_main.parse)(input, { startRule: "ProloguePrefix" });
354
+ const prefix = input.slice(0, -rest.length);
355
+ const coffee = prologue.some((p) => p.type === "CivetPrologue" && (p.config.coffeeCompat || p.config.coffeeDo));
356
+ ast = (0, import_main.compile)(
357
+ prefix + (coffee ? "(do ->\n" : "async do\n") + rest.replace(/^/gm, " ") + (coffee ? ")" : ""),
358
+ { ...options, filename, ast: true }
359
+ );
360
+ }
361
+ const errors = [];
298
362
  try {
299
- output = (0, import_main.compile)(input, { ...options, filename });
363
+ output = (0, import_main.generate)(ast, { errors });
300
364
  } catch (error) {
301
365
  console.error(error);
302
366
  return callback(null, void 0);
303
367
  }
304
- if (options.compile || options.ast) {
305
- return callback(null, output);
306
- } else {
307
- let result;
368
+ if (errors.length) {
369
+ console.error(`Parse errors: ${errors.map(($1) => $1.message).join("\n")}`);
370
+ return callback(null, void 0);
371
+ }
372
+ let result;
373
+ try {
374
+ result = vm.runInContext(output, context, {
375
+ filename,
376
+ importModuleDynamically
377
+ });
378
+ } catch (error) {
379
+ return callback(error, void 0);
380
+ }
381
+ if (topLevelAwait) {
308
382
  try {
309
- result = vm.runInContext(output, context, { filename });
383
+ result = await result;
310
384
  } catch (error) {
311
- return callback(error, void 0);
385
+ callback(error, void 0);
312
386
  }
313
387
  return callback(null, result);
388
+ } else {
389
+ return callback(null, result);
314
390
  }
315
391
  } else {
316
392
  return callback(new nodeRepl.Recoverable(new Error("Enter a blank line to execute code.")), null);
@@ -451,7 +527,20 @@ async function cli() {
451
527
  }
452
528
  }
453
529
  const { fork } = await import("child_process");
454
- const execArgv = ["--loader", "@danielx/civet/esm"];
530
+ const { register } = await import("module");
531
+ let execArgv;
532
+ if (register) {
533
+ execArgv = ["--import", "@danielx/civet/register"];
534
+ } else {
535
+ execArgv = [
536
+ "--loader",
537
+ "@danielx/civet/esm",
538
+ // ESM
539
+ "--require",
540
+ "@danielx/civet/register"
541
+ // CJS
542
+ ];
543
+ }
455
544
  const debugRe = /--debug|--inspect/;
456
545
  const isDebug = typeof v8debug === "object" || debugRe.test(process.execArgv.join(" ")) || debugRe.test(process.env.NODE_OPTIONS ?? "");
457
546
  if (process.env.NODE_OPTIONS) {
@@ -516,3 +605,10 @@ async function cli() {
516
605
  if (require.main === module) {
517
606
  cli();
518
607
  }
608
+ // Annotate the CommonJS export names for ESM import in node:
609
+ 0 && (module.exports = {
610
+ cli,
611
+ parseArgs,
612
+ repl,
613
+ version
614
+ });
package/dist/esm.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  // source/esm.civet
2
2
  import { readFileSync } from "fs";
3
- import { createRequire } from "module";
4
3
  import { pathToFileURL, fileURLToPath } from "url";
5
4
  import sourceMapSupport from "@cspotcode/source-map-support";
6
5
  import Civet from "./main.js";
@@ -72,16 +71,6 @@ async function load(url, context, next) {
72
71
  }
73
72
  return next(url, context);
74
73
  }
75
- var require2 = createRequire(import.meta.url);
76
- require2.extensions[".civet"] = function(m, filename) {
77
- const source = readFileSync(filename, "utf8");
78
- const code = compile(source, {
79
- filename,
80
- inlineMap: true,
81
- js: true
82
- });
83
- return m._compile(code, filename);
84
- };
85
74
  export {
86
75
  load,
87
76
  resolve