@danielx/civet 0.7.1 → 0.7.2

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
@@ -78,6 +78,7 @@ Options:
78
78
  -c / --compile Compile input files to TypeScript (or JavaScript)
79
79
  --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 .)
80
80
  --civet XX Specify civet compiler flag, as in "civet XX" prologue
81
+ --comptime Enable execution of code during compilation via comptime
81
82
  --no-config Don't scan for a config file
82
83
  --js Strip out all type annotations; default to .jsx extension
83
84
  --ast Print the AST instead of the compiled code
@@ -120,14 +121,7 @@ function parseArgs(args) {
120
121
  while (i < args.length) {
121
122
  const arg = args[i];
122
123
  if (/^-\w{2,}$/.test(arg)) {
123
- args.splice(i, 1 + i - i, ...(() => {
124
- const results = [];
125
- for (let ref = arg.slice(1), i1 = 0, len = ref.length; i1 < len; i1++) {
126
- const char = ref[i1];
127
- results.push(`-${char}`);
128
- }
129
- return results;
130
- })());
124
+ args.splice(i, 1 + i - i, ...results);
131
125
  continue;
132
126
  }
133
127
  switch (arg) {
@@ -158,6 +152,14 @@ function parseArgs(args) {
158
152
  );
159
153
  break;
160
154
  }
155
+ case "--comptime": {
156
+ (options.parseOptions ??= {}).comptime = true;
157
+ break;
158
+ }
159
+ case "--no-comptime": {
160
+ (options.parseOptions ??= {}).comptime = false;
161
+ break;
162
+ }
161
163
  case "--ast": {
162
164
  options.ast = true;
163
165
  break;
@@ -212,8 +214,8 @@ function parseArgs(args) {
212
214
  }
213
215
  async function* readFiles(filenames) {
214
216
  const results1 = [];
215
- for (let i2 = 0, len1 = filenames.length; i2 < len1; i2++) {
216
- let filename = filenames[i2];
217
+ for (let i1 = 0, len = filenames.length; i1 < len; i1++) {
218
+ let filename = filenames[i1];
217
219
  const stdin = filename === "-";
218
220
  try {
219
221
  let content;
@@ -313,20 +315,28 @@ async function repl(options) {
313
315
  }
314
316
  }
315
317
  })(),
316
- writer: options.ast ? (obj) => {
317
- try {
318
- return JSON.stringify(obj, null, 2);
319
- } catch (e) {
320
- console.log(`Failed to stringify: ${e}`);
321
- return "";
322
- }
323
- } : options.compile ? (obj) => {
324
- if (typeof obj === "string") {
325
- return obj?.replace(/\n*$/, "");
326
- } else {
327
- return "";
318
+ writer: (() => {
319
+ if (options.ast) {
320
+ return (obj) => {
321
+ try {
322
+ return JSON.stringify(obj, null, 2);
323
+ } catch (e) {
324
+ console.log(`Failed to stringify: ${e}`);
325
+ return "";
326
+ }
327
+ };
328
+ } else if (options.compile) {
329
+ return (obj) => {
330
+ if (typeof obj === "string") {
331
+ return obj?.replace(/\n*$/, "");
332
+ } else {
333
+ return "";
334
+ }
335
+ };
328
336
  }
329
- } : void 0,
337
+ ;
338
+ return;
339
+ })(),
330
340
  eval: async function(input, context, filename, callback) {
331
341
  if (input === "\n") {
332
342
  return callback(null, void 0);
@@ -600,9 +610,9 @@ async function cli() {
600
610
  }
601
611
  });
602
612
  } catch (error) {
603
- let ref1;
604
- if (ref1 = error.message.match(/Aborting build because of (\d+) TypeScript diagnostic/)) {
605
- const match = ref1;
613
+ let ref;
614
+ if (ref = error.message.match(/Aborting build because of (\d+) TypeScript diagnostic/)) {
615
+ const match = ref;
606
616
  return process.exitCode = Math.min(255, errors + +match[1]);
607
617
  } else {
608
618
  process.exitCode = 1;
package/dist/config.js CHANGED
@@ -44,21 +44,29 @@ var configFileNames = /* @__PURE__ */ new Set([
44
44
  "civetconfig.civet"
45
45
  ]);
46
46
  async function findInDir(dirPath) {
47
- const dir = await import_promises.default.opendir(dirPath);
48
- for await (const entry of dir) {
49
- if (entry.isDirectory() && entry.name === ".config") {
50
- const found = await findInDir(
51
- import_path.default.join(dirPath, entry.name)
52
- );
47
+ for (const entryName of await import_promises.default.readdir(dirPath)) {
48
+ const entryPath = import_path.default.join(dirPath, entryName);
49
+ if (entryName === ".config" && await (async () => {
50
+ try {
51
+ return (await import_promises.default.stat(entryPath)).isDir();
52
+ } catch (e) {
53
+ return;
54
+ }
55
+ })()) {
56
+ const found = await findInDir(entryPath);
53
57
  if (found) {
54
58
  return found;
55
59
  }
56
60
  }
57
- if (entry.isFile()) {
58
- const name = entry.name.replace(/^\./, "");
59
- if (configFileNames.has(name)) {
60
- return import_path.default.join(dirPath, entry.name);
61
+ const name = entryName.replace(/^\./, "");
62
+ if (configFileNames.has(name) && await (async () => {
63
+ try {
64
+ return (await import_promises.default.stat(entryPath)).isFile();
65
+ } catch (e) {
66
+ return;
61
67
  }
68
+ })()) {
69
+ return entryPath;
62
70
  }
63
71
  }
64
72
  return;
@@ -78,21 +86,32 @@ async function findConfig(startDir) {
78
86
  }
79
87
  async function loadConfig(path2) {
80
88
  const config = await import_promises.default.readFile(path2, "utf8");
89
+ let data = {};
81
90
  if (path2.endsWith(".json")) {
82
- return JSON.parse(config);
91
+ try {
92
+ data = JSON.parse(config);
93
+ } catch (e) {
94
+ throw new Error(`Error parsing JSON config file ${path2}: ${e}`);
95
+ }
83
96
  } else {
84
- const js = (0, import_main.compile)(config, { js: true });
85
- let exports2;
97
+ let js;
86
98
  try {
87
- exports2 = await import(`data:text/javascript,${encodeURIComponent(js)}`);
99
+ js = (0, import_main.compile)(config, { js: true });
88
100
  } catch (e) {
89
- console.error("Error loading config file", path2, e);
101
+ throw new Error(`Error compiling Civet config file ${path2}: ${e}`);
90
102
  }
91
- if (typeof exports2.default !== "object" || exports2.default === null) {
92
- throw new Error("civet config file must export an object");
103
+ try {
104
+ const exports2 = await import(`data:text/javascript,${encodeURIComponent(js)}`);
105
+ data = exports2?.default;
106
+ } catch (e) {
107
+ throw new Error(`Error running Civet config file ${path2}: ${e}`);
93
108
  }
94
- return exports2.default;
95
109
  }
110
+ if (!(data != null && typeof data === "object" && !Array.isArray(data))) {
111
+ throw new Error(`Civet config file must export an object, not ${Array.isArray(data) ? "array" : data != null ? typeof data : "null"}`);
112
+ }
113
+ delete data?.parseOptions?.comptime;
114
+ return data;
96
115
  }
97
116
  // Annotate the CommonJS export names for ESM import in node:
98
117
  0 && (module.exports = {
@@ -36,7 +36,7 @@ module.exports = __toCommonJS(esbuild_plugin_exports);
36
36
  var import_promises = require("fs/promises");
37
37
  var import_path = __toESM(require("path"));
38
38
  var import_main = require("./main.js");
39
- function civet(options = {}) {
39
+ var civet = function(options = {}) {
40
40
  const {
41
41
  filter = /\.civet$/,
42
42
  inlineMap = true,
@@ -124,7 +124,7 @@ function civet(options = {}) {
124
124
  });
125
125
  }
126
126
  };
127
- }
127
+ };
128
128
  var defaultPlugin = civet();
129
129
  civet.setup = defaultPlugin.setup;
130
130
  var esbuild_plugin_default = civet;
package/dist/esbuild.js CHANGED
@@ -165,7 +165,10 @@ var rawPlugin = (options = {}, meta) => {
165
165
  });
166
166
  const compiledTS = import_civet.default.compile(rawCivetSource, {
167
167
  filename,
168
- js: false
168
+ js: false,
169
+ parseOptions: {
170
+ comptime: Boolean(options.comptime)
171
+ }
169
172
  });
170
173
  fsMap.set(filename, compiledTS);
171
174
  return compiledTS;
@@ -310,17 +313,22 @@ var rawPlugin = (options = {}, meta) => {
310
313
  const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
311
314
  this.addWatchFile(filename);
312
315
  let compiled;
316
+ const civetOptions = {
317
+ filename: id,
318
+ sourceMap: true,
319
+ parseOptions: {
320
+ comptime: Boolean(options.comptime)
321
+ }
322
+ };
313
323
  if (options.ts === "civet" && !transformTS) {
314
324
  compiled = import_civet.default.compile(rawCivetSource, {
315
- filename: id,
316
- js: true,
317
- sourceMap: true
325
+ ...civetOptions,
326
+ js: true
318
327
  });
319
328
  } else {
320
329
  const compiledTS = import_civet.default.compile(rawCivetSource, {
321
- filename: id,
322
- js: false,
323
- sourceMap: true
330
+ ...civetOptions,
331
+ js: false
324
332
  });
325
333
  const resolved = filename + outExt;
326
334
  sourceMaps.set(
@@ -365,9 +373,8 @@ var rawPlugin = (options = {}, meta) => {
365
373
  case "civet":
366
374
  default: {
367
375
  compiled = import_civet.default.compile(rawCivetSource, {
368
- filename: id,
369
- js: true,
370
- sourceMap: true
376
+ ...civetOptions,
377
+ js: true
371
378
  });
372
379
  if (options.ts == void 0) {
373
380
  console.log(
package/dist/esm.mjs CHANGED
@@ -14,7 +14,7 @@ var backslashRegExp = /\\/g;
14
14
  function normalizeSlashes(value) {
15
15
  return value.replace(backslashRegExp, directorySeparator);
16
16
  }
17
- function ensureRegister() {
17
+ var ensureRegister = function() {
18
18
  if (registered) {
19
19
  return;
20
20
  }
@@ -34,7 +34,7 @@ function ensureRegister() {
34
34
  };
35
35
  sourceMapSupport.install(installation);
36
36
  return registered = true;
37
- }
37
+ };
38
38
  function resolve(specifier, context, next) {
39
39
  const { parentURL = baseURL } = context;
40
40
  if (extensionsRegex.test(specifier)) {