@danielx/civet 0.11.6 → 0.11.8

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/cache.js ADDED
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // source/cache.civet
31
+ var cache_exports = {};
32
+ __export(cache_exports, {
33
+ createDiskCache: () => createDiskCache,
34
+ createMemoryCache: () => createMemoryCache,
35
+ hashParts: () => hashParts,
36
+ makeCacheKey: () => makeCacheKey,
37
+ stableStringify: () => stableStringify
38
+ });
39
+ module.exports = __toCommonJS(cache_exports);
40
+ var crypto = __toESM(require("node:crypto"));
41
+ var fs = __toESM(require("node:fs"));
42
+ var path = __toESM(require("node:path"));
43
+ function hashParts(parts) {
44
+ const hash = crypto.createHash("sha1");
45
+ for (const part of parts) {
46
+ hash.update(part).update("\0");
47
+ }
48
+ return hash.digest("hex");
49
+ }
50
+ function stableStringify(value) {
51
+ return JSON.stringify(value, (_key, val) => {
52
+ if (typeof val === "function") {
53
+ return void 0;
54
+ }
55
+ if (val && typeof val === "object" && !Array.isArray(val)) {
56
+ const sorted = {};
57
+ for (const key of Object.keys(val).sort()) {
58
+ sorted[key] = val[key];
59
+ }
60
+ return sorted;
61
+ }
62
+ return val;
63
+ });
64
+ }
65
+ function makeCacheKey(input) {
66
+ const resolved = path.resolve(input.sourcePath).replace(/\\/g, "/");
67
+ return hashParts([
68
+ input.source,
69
+ input.compilerName,
70
+ input.compilerVersion,
71
+ input.civetVersion ?? "",
72
+ input.civetMtime ?? "",
73
+ resolved,
74
+ stableStringify(input.options ?? {})
75
+ ]);
76
+ }
77
+ function createMemoryCache() {
78
+ const store = /* @__PURE__ */ new Map();
79
+ return {
80
+ get(key) {
81
+ return store.get(key);
82
+ },
83
+ set(key, value) {
84
+ store.set(key, value);
85
+ return;
86
+ }
87
+ };
88
+ }
89
+ function createDiskCache(dir) {
90
+ try {
91
+ fs.mkdirSync(dir, { recursive: true });
92
+ } catch {
93
+ }
94
+ return {
95
+ get(key) {
96
+ const target = path.join(dir, key);
97
+ try {
98
+ const content = fs.readFileSync(target, "utf8");
99
+ try {
100
+ const now = /* @__PURE__ */ new Date();
101
+ fs.utimesSync(target, now, now);
102
+ } catch {
103
+ }
104
+ return content;
105
+ } catch {
106
+ return void 0;
107
+ }
108
+ },
109
+ set(key, value) {
110
+ const target = path.join(dir, key);
111
+ try {
112
+ const tmp = `${target}.tmp.${process.pid}`;
113
+ fs.writeFileSync(tmp, value);
114
+ return fs.renameSync(tmp, target);
115
+ } catch {
116
+ return;
117
+ }
118
+ }
119
+ };
120
+ }
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ createDiskCache,
124
+ createMemoryCache,
125
+ hashParts,
126
+ makeCacheKey,
127
+ stableStringify
128
+ });
package/dist/cache.mjs ADDED
@@ -0,0 +1,89 @@
1
+ // source/cache.civet
2
+ import * as crypto from "node:crypto";
3
+ import * as fs from "node:fs";
4
+ import * as path from "node:path";
5
+ function hashParts(parts) {
6
+ const hash = crypto.createHash("sha1");
7
+ for (const part of parts) {
8
+ hash.update(part).update("\0");
9
+ }
10
+ return hash.digest("hex");
11
+ }
12
+ function stableStringify(value) {
13
+ return JSON.stringify(value, (_key, val) => {
14
+ if (typeof val === "function") {
15
+ return void 0;
16
+ }
17
+ if (val && typeof val === "object" && !Array.isArray(val)) {
18
+ const sorted = {};
19
+ for (const key of Object.keys(val).sort()) {
20
+ sorted[key] = val[key];
21
+ }
22
+ return sorted;
23
+ }
24
+ return val;
25
+ });
26
+ }
27
+ function makeCacheKey(input) {
28
+ const resolved = path.resolve(input.sourcePath).replace(/\\/g, "/");
29
+ return hashParts([
30
+ input.source,
31
+ input.compilerName,
32
+ input.compilerVersion,
33
+ input.civetVersion ?? "",
34
+ input.civetMtime ?? "",
35
+ resolved,
36
+ stableStringify(input.options ?? {})
37
+ ]);
38
+ }
39
+ function createMemoryCache() {
40
+ const store = /* @__PURE__ */ new Map();
41
+ return {
42
+ get(key) {
43
+ return store.get(key);
44
+ },
45
+ set(key, value) {
46
+ store.set(key, value);
47
+ return;
48
+ }
49
+ };
50
+ }
51
+ function createDiskCache(dir) {
52
+ try {
53
+ fs.mkdirSync(dir, { recursive: true });
54
+ } catch {
55
+ }
56
+ return {
57
+ get(key) {
58
+ const target = path.join(dir, key);
59
+ try {
60
+ const content = fs.readFileSync(target, "utf8");
61
+ try {
62
+ const now = /* @__PURE__ */ new Date();
63
+ fs.utimesSync(target, now, now);
64
+ } catch {
65
+ }
66
+ return content;
67
+ } catch {
68
+ return void 0;
69
+ }
70
+ },
71
+ set(key, value) {
72
+ const target = path.join(dir, key);
73
+ try {
74
+ const tmp = `${target}.tmp.${process.pid}`;
75
+ fs.writeFileSync(tmp, value);
76
+ return fs.renameSync(tmp, target);
77
+ } catch {
78
+ return;
79
+ }
80
+ }
81
+ };
82
+ }
83
+ export {
84
+ createDiskCache,
85
+ createMemoryCache,
86
+ hashParts,
87
+ makeCacheKey,
88
+ stableStringify
89
+ };
package/dist/civet CHANGED
@@ -42,11 +42,9 @@ __export(cli_exports, {
42
42
  module.exports = __toCommonJS(cli_exports);
43
43
  var import_main = require("./main.js");
44
44
  var import_config = require("./config.js");
45
- var import_unplugin = require("./unplugin/unplugin.js");
46
45
  var import_promises = __toESM(require("node:fs/promises"));
47
46
  var import_node_path = __toESM(require("node:path"));
48
47
  var import_meta = {};
49
- var unplugin;
50
48
  async function version() {
51
49
  if (import_meta.url) {
52
50
  const { createRequire } = await import("node:module");
@@ -56,6 +54,7 @@ async function version() {
56
54
  }
57
55
  }
58
56
  var encoding = "utf8";
57
+ var epipeStdoutHandlerInstalled = false;
59
58
  async function parseArgs(args, isTTY = process.stdin.isTTY) {
60
59
  const options = {};
61
60
  const isRun = () => !(options.ast || options.compile || options.typecheck || options.emitDeclaration);
@@ -176,6 +175,28 @@ async function parseArgs(args, isTTY = process.stdin.isTTY) {
176
175
  options.typecheck = true;
177
176
  break;
178
177
  }
178
+ case "--max-errors": {
179
+ const n = parseInt(args[++i] ?? "", 10);
180
+ if (isNaN(n) || n < 0) {
181
+ console.error(`Invalid value for --max-errors: ${args[i]}`);
182
+ errors++;
183
+ } else {
184
+ options.maxErrors = n;
185
+ }
186
+ ;
187
+ break;
188
+ }
189
+ case "--tsbuildinfo": {
190
+ const v = args[++i];
191
+ if (!v || v.startsWith("-")) {
192
+ console.error(`Missing PATH after --tsbuildinfo`);
193
+ errors++;
194
+ } else {
195
+ options.tsBuildInfoFile = v;
196
+ }
197
+ ;
198
+ break;
199
+ }
179
200
  case "--emit-declaration":
180
201
  case "--emitDeclaration": {
181
202
  options.emitDeclaration = true;
@@ -202,6 +223,12 @@ async function parseArgs(args, isTTY = process.stdin.isTTY) {
202
223
  if (options.typecheck || options.emitDeclaration) {
203
224
  options.typescript = true;
204
225
  }
226
+ if (options.maxErrors == null && process.env.CIVET_TYPECHECK_MAX_ERRORS) {
227
+ const parsed = parseInt(process.env.CIVET_TYPECHECK_MAX_ERRORS, 10);
228
+ if (!isNaN(parsed)) {
229
+ options.maxErrors = parsed;
230
+ }
231
+ }
205
232
  if (!(filenames.length || options.typescript || options.eval)) {
206
233
  if (isTTY) {
207
234
  options.repl = true;
@@ -231,6 +258,9 @@ async function parseArgs(args, isTTY = process.stdin.isTTY) {
231
258
  options.outputExt = optionsPath.ext;
232
259
  }
233
260
  }
261
+ if (options.outputExt && /\.[cm]?jsx?$/i.test(options.outputExt)) {
262
+ options.js ??= true;
263
+ }
234
264
  ;
235
265
  (options.parseOptions ??= {}).rewriteCivetImports ??= options.outputExt ?? ".civet.jsx";
236
266
  if (errors) {
@@ -388,7 +418,7 @@ async function repl(args, options) {
388
418
  input = input.replace(/\r/g, "\n");
389
419
  if (input === "\n") {
390
420
  return callback(null, void 0);
391
- } else if (input in ["quit\n", "exit\n", "quit()\n", "exit()\n"]) {
421
+ } else if (["quit\n", "exit\n", "quit()\n", "exit()\n"].includes(input)) {
392
422
  return process.exit(0);
393
423
  } else if (input.endsWith("\n\n")) {
394
424
  let showError2 = function(error) {
@@ -495,6 +525,7 @@ ${" ".repeat(error.column - 1)}^ ${error.header}`);
495
525
  return;
496
526
  }
497
527
  async function cli(args = process.argv.slice(2)) {
528
+ let unplugin;
498
529
  let { filenames, scriptArgs, options } = await parseArgs(args);
499
530
  if (options.version) {
500
531
  console.log(await version());
@@ -536,6 +567,10 @@ Options:
536
567
  --inline-map Generate a sourcemap
537
568
  --no-cache Disable compiler caching (slow, for debugging)
538
569
  --typecheck Run TypeScript and output diagnostics
570
+ --max-errors N Allow up to N TS diagnostics before exiting non-zero
571
+ (also: CIVET_TYPECHECK_MAX_ERRORS env)
572
+ --tsbuildinfo PATH Persist incremental TS analysis state to PATH for
573
+ faster repeat typechecks
539
574
  --emit-declaration Run TypeScript and emit .d.ts files (if no errors)
540
575
  --trace XX Log detailed parsing notes to a file, for parser debugging
541
576
 
@@ -564,6 +599,7 @@ You can override this behavior via: --civet rewriteCivetImports=.ext
564
599
  }
565
600
  }
566
601
  if (options.typescript) {
602
+ const { rawPlugin } = await import("./unplugin/unplugin.js");
567
603
  if (!import_meta.url) {
568
604
  try {
569
605
  require("typescript");
@@ -590,10 +626,13 @@ You can override this behavior via: --civet rewriteCivetImports=.ext
590
626
  ...options,
591
627
  ts: options.js ? "civet" : "preserve",
592
628
  outputExtension: ".tsx",
593
- declarationExtension: options.outputExt?.replace(/\.[jt]sx?$/i, ".d.ts")
629
+ declarationExtension: options.outputExt?.replace(/\.[jt]sx?$/i, ".d.ts"),
630
+ // No filenames + --typecheck: lint the project. Filenames given:
631
+ // scope to those (matches the bundler-driven default).
632
+ discoverProjectFiles: filenames.length === 0
594
633
  };
595
- (unpluginOptions.parseOptions ??= {}).rewriteCivetImports = ".civet.jsx";
596
- unplugin = (0, import_unplugin.rawPlugin)(unpluginOptions, { framework: "civet-cli" });
634
+ (unpluginOptions.parseOptions ??= {}).rewriteCivetImports = false;
635
+ unplugin = rawPlugin(unpluginOptions, { framework: "civet-cli" });
597
636
  await unplugin.buildStart();
598
637
  }
599
638
  if (options.run) {
@@ -603,14 +642,17 @@ You can override this behavior via: --civet rewriteCivetImports=.ext
603
642
  if (options.repl) {
604
643
  return repl(args, options);
605
644
  }
606
- process.stdout.on("error", (e) => {
607
- if (["EPIPE", "EOF"].includes(e.code)) {
608
- return process.exit(0);
609
- } else {
610
- console.error(e);
611
- return process.exit(1);
612
- }
613
- });
645
+ if (!epipeStdoutHandlerInstalled) {
646
+ epipeStdoutHandlerInstalled = true;
647
+ process.stdout.on("error", (e) => {
648
+ if (["EPIPE", "EOF"].includes(e.code)) {
649
+ return process.exit(0);
650
+ } else {
651
+ console.error(e);
652
+ return process.exit(1);
653
+ }
654
+ });
655
+ }
614
656
  let errors = 0;
615
657
  for await (let { filename, error, content, stdin } of readFiles(filenames, options.eval)) {
616
658
  if (error) {
@@ -739,11 +781,21 @@ You can override this behavior via: --civet rewriteCivetImports=.ext
739
781
  process.exitCode = Math.min(255, errors);
740
782
  if (unplugin != null) {
741
783
  try {
742
- return await unplugin.buildEnd.call({
784
+ const pending = [];
785
+ await unplugin.buildEnd.call({
743
786
  emitFile({ source, fileName }) {
744
- return import_promises.default.writeFile(fileName, source);
787
+ pending.push((async () => {
788
+ {
789
+ const outputDir = import_node_path.default.dirname(fileName);
790
+ if (!(outputDir === ".")) {
791
+ await import_promises.default.mkdir(outputDir, { recursive: true });
792
+ }
793
+ return await import_promises.default.writeFile(fileName, source);
794
+ }
795
+ })());
745
796
  }
746
797
  }, !filenames.length);
798
+ return await Promise.all(pending);
747
799
  } catch (error) {
748
800
  let ref5;
749
801
  if (ref5 = error.message.match(/Aborting build because of (\d+) TypeScript diagnostic/)) {