@danielx/civet 0.7.13 → 0.7.15

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
@@ -40,8 +40,8 @@ module.exports = __toCommonJS(cli_exports);
40
40
  var import_main = require("./main.js");
41
41
  var import_config = require("./config.js");
42
42
  var import_unplugin = require("./unplugin");
43
- var import_promises = __toESM(require("fs/promises"));
44
- var import_path = __toESM(require("path"));
43
+ var import_promises = __toESM(require("node:fs/promises"));
44
+ var import_node_path = __toESM(require("node:path"));
45
45
  var unplugin;
46
46
  function version() {
47
47
  return require("../package.json").version;
@@ -98,11 +98,7 @@ You can override this behavior via: --civet rewriteCivetImports=.ext
98
98
  var encoding = "utf8";
99
99
  function parseArgs(args) {
100
100
  const options = {};
101
- Object.defineProperty(options, "run", {
102
- get: function() {
103
- return !(this.ast || this.compile || this.typecheck || this.emitDeclaration);
104
- }
105
- });
101
+ const isRun = () => !(options.ast || options.compile || options.typecheck || options.emitDeclaration);
106
102
  const filenames = [];
107
103
  let scriptArgs = [];
108
104
  let i = 0;
@@ -208,7 +204,7 @@ function parseArgs(args) {
208
204
  if (arg.startsWith("-") && arg !== "-") {
209
205
  console.error(`Invalid command-line argument: ${arg}`);
210
206
  errors++;
211
- } else if (options.run) {
207
+ } else if (options.run = isRun()) {
212
208
  endOfArgs(i);
213
209
  } else {
214
210
  filenames.push(arg);
@@ -220,6 +216,7 @@ function parseArgs(args) {
220
216
  if (errors) {
221
217
  process.exit(Math.min(255, errors));
222
218
  }
219
+ options.run = isRun();
223
220
  return { filenames, scriptArgs, options };
224
221
  }
225
222
  async function* readFiles(filenames) {
@@ -276,7 +273,7 @@ async function repl(options) {
276
273
  const { pathToFileURL } = await import("node:url");
277
274
  importModuleDynamically = (specifier) => {
278
275
  if (/^\.\.?[/\\]/.test(specifier)) {
279
- return import(pathToFileURL(import_path.default.join(process.cwd(), specifier)));
276
+ return import(pathToFileURL(import_node_path.default.join(process.cwd(), specifier)));
280
277
  } else {
281
278
  return import(specifier);
282
279
  }
@@ -457,7 +454,8 @@ async function cli() {
457
454
  ...options
458
455
  };
459
456
  }
460
- if (!filenames.length) {
457
+ const typescript = options.typecheck || options.emitDeclaration;
458
+ if (!(filenames.length || typescript)) {
461
459
  if (process.stdin.isTTY) {
462
460
  options.repl = true;
463
461
  } else {
@@ -465,13 +463,13 @@ async function cli() {
465
463
  filenames = ["-"];
466
464
  }
467
465
  }
468
- if (options.typecheck || options.emitDeclaration) {
466
+ if (typescript) {
469
467
  const unpluginOptions = {
470
468
  ...options,
471
469
  ts: options.js ? "civet" : "preserve",
472
470
  outputExtension: ".tsx"
473
471
  };
474
- (unpluginOptions.parseOptions ??= {}).rewriteCivetImports = ".civet.tsx";
472
+ (unpluginOptions.parseOptions ??= {}).rewriteCivetImports = ".civet.jsx";
475
473
  unplugin = (0, import_unplugin.rawPlugin)(unpluginOptions, { framework: "civet-cli" });
476
474
  await unplugin.buildStart();
477
475
  }
@@ -484,14 +482,14 @@ async function cli() {
484
482
  }
485
483
  let outputDir, outputExt, outputPath;
486
484
  if (options.output) {
487
- const optionsPath = import_path.default.parse(options.output);
485
+ const optionsPath = import_node_path.default.parse(options.output);
488
486
  let stat;
489
487
  try {
490
488
  stat = await import_promises.default.stat(options.output);
491
489
  } catch {
492
490
  stat = null;
493
491
  }
494
- if (stat?.isDirectory() || options.output.endsWith(import_path.default.sep) || options.output.endsWith("/")) {
492
+ if (stat?.isDirectory() || options.output.endsWith(import_node_path.default.sep) || options.output.endsWith("/")) {
495
493
  outputDir = options.output;
496
494
  } else if (/^(\.[^.]+)+$/.test(optionsPath.base)) {
497
495
  outputExt = optionsPath.base;
@@ -543,7 +541,7 @@ async function cli() {
543
541
  if (stdin && !options.output || options.output === "-") {
544
542
  process.stdout.write(output);
545
543
  } else {
546
- let targetPath = import_path.default.parse(filename);
544
+ let targetPath = import_node_path.default.parse(filename);
547
545
  delete targetPath.base;
548
546
  if (options.js) {
549
547
  targetPath.ext += ".jsx";
@@ -562,7 +560,7 @@ async function cli() {
562
560
  if (targetPath.dir) {
563
561
  await import_promises.default.mkdir(targetPath.dir, { recursive: true });
564
562
  }
565
- const targetFilename = import_path.default.format(targetPath);
563
+ const targetFilename = import_node_path.default.format(targetPath);
566
564
  try {
567
565
  await import_promises.default.writeFile(targetFilename, output);
568
566
  } catch (error2) {
@@ -627,7 +625,7 @@ async function cli() {
627
625
  module.filename = filename;
628
626
  }
629
627
  process.argv = ["civet", module.filename, ...scriptArgs];
630
- module.paths = require("module")._nodeModulePaths(import_path.default.dirname(module.filename));
628
+ module.paths = require("module")._nodeModulePaths(import_node_path.default.dirname(module.filename));
631
629
  try {
632
630
  module._compile(output, module.filename);
633
631
  } catch (error2) {
@@ -645,7 +643,7 @@ async function cli() {
645
643
  emitFile({ source, fileName }) {
646
644
  return import_promises.default.writeFile(fileName, source);
647
645
  }
648
- });
646
+ }, !filenames.length);
649
647
  } catch (error) {
650
648
  let ref1;
651
649
  if (ref1 = error.message.match(/Aborting build because of (\d+) TypeScript diagnostic/)) {
package/dist/config.js CHANGED
@@ -38,38 +38,51 @@ module.exports = __toCommonJS(config_exports);
38
38
  var import_path = __toESM(require("path"));
39
39
  var import_promises = __toESM(require("fs/promises"));
40
40
  var import_main = require("./main.js");
41
- var configFileNames = /* @__PURE__ */ new Set([
42
- "\u{1F408}.json",
43
- "\u{1F408}.civet",
44
- "civetconfig.json",
45
- "civetconfig.civet",
46
- "civet.config.json",
47
- "civet.config.civet"
48
- ]);
41
+ var configNames = [
42
+ "\u{1F408}",
43
+ "civetconfig",
44
+ "civet.config",
45
+ "package"
46
+ ];
47
+ var configExtensions = [
48
+ ".civet",
49
+ ".js",
50
+ ".yaml",
51
+ ".yml",
52
+ ".json"
53
+ ];
54
+ var configDir = ".config";
49
55
  async function findInDir(dirPath) {
50
- for (const entryName of await import_promises.default.readdir(dirPath)) {
51
- const entryPath = import_path.default.join(dirPath, entryName);
52
- if (entryName === ".config" && await (async () => {
53
- try {
54
- return (await import_promises.default.stat(entryPath)).isDirectory();
55
- } catch (e) {
56
- return;
57
- }
58
- })()) {
59
- const found = await findInDir(entryPath);
60
- if (found) {
61
- return found;
62
- }
56
+ const entries = new Set(await import_promises.default.readdir(dirPath));
57
+ const pathFor = (name) => import_path.default.join(dirPath, name);
58
+ if (entries.has(configDir) && await (async () => {
59
+ try {
60
+ return (await import_promises.default.stat(pathFor(configDir))).isDirectory();
61
+ } catch (e) {
62
+ return;
63
63
  }
64
- const name = entryName.replace(/^\./, "");
65
- if (configFileNames.has(name) && await (async () => {
66
- try {
67
- return (await import_promises.default.stat(entryPath)).isFile();
68
- } catch (e) {
69
- return;
64
+ })()) {
65
+ const found = await findInDir(pathFor(configDir));
66
+ if (found) {
67
+ return found;
68
+ }
69
+ }
70
+ for (let i = 0, len = configNames.length; i < len; i++) {
71
+ const configName = configNames[i];
72
+ for (let i1 = 0, len1 = configExtensions.length; i1 < len1; i1++) {
73
+ const extension = configExtensions[i1];
74
+ for (let ref = ["." + configName + extension, configName + extension], i2 = 0, len2 = ref.length; i2 < len2; i2++) {
75
+ const entry = ref[i2];
76
+ if (entries.has(entry) && await (async () => {
77
+ try {
78
+ return (await import_promises.default.stat(pathFor(entry))).isFile();
79
+ } catch (e) {
80
+ return;
81
+ }
82
+ })()) {
83
+ return pathFor(entry);
84
+ }
70
85
  }
71
- })()) {
72
- return entryPath;
73
86
  }
74
87
  }
75
88
  return;
@@ -87,30 +100,49 @@ async function findConfig(startDir) {
87
100
  }
88
101
  return;
89
102
  }
90
- async function loadConfig(path2) {
91
- const config = await import_promises.default.readFile(path2, "utf8");
103
+ async function loadConfig(pathname) {
104
+ const config = await import_promises.default.readFile(pathname, "utf8");
92
105
  let data = {};
93
- if (path2.endsWith(".json")) {
106
+ if (pathname.endsWith(".json")) {
107
+ let json;
94
108
  try {
95
- data = JSON.parse(config);
109
+ json = JSON.parse(config);
96
110
  } catch (e) {
97
- throw new Error(`Error parsing JSON config file ${path2}: ${e}`);
111
+ throw new Error(`Error parsing JSON config file ${pathname}: ${e}`);
98
112
  }
99
- } else {
100
- let js;
113
+ if ("civetConfig" in json) {
114
+ data = json.civetConfig;
115
+ } else if (import_path.default.basename(pathname).startsWith("package")) {
116
+ return {};
117
+ } else {
118
+ data = json;
119
+ }
120
+ } else if (/\.ya?ml$/.test(pathname)) {
101
121
  try {
102
- js = (0, import_main.compile)(config, {
103
- js: true,
104
- sync: true
105
- });
122
+ const { default: YAML } = await import("yaml");
123
+ const yaml = YAML.parse(config);
124
+ data = yaml.civetConfig ?? yaml;
106
125
  } catch (e) {
107
- throw new Error(`Error compiling Civet config file ${path2}: ${e}`);
126
+ throw new Error(`Error parsing YAML config file ${pathname}: ${e}`);
127
+ }
128
+ } else {
129
+ let js;
130
+ if (pathname.endsWith(".civet")) {
131
+ try {
132
+ js = await (0, import_main.compile)(config, {
133
+ js: true
134
+ });
135
+ } catch (e) {
136
+ throw new Error(`Error compiling Civet config file ${pathname}: ${e}`);
137
+ }
138
+ } else {
139
+ js = config;
108
140
  }
109
141
  try {
110
142
  const exports2 = await import(`data:text/javascript,${encodeURIComponent(js)}`);
111
143
  data = exports2?.default;
112
144
  } catch (e) {
113
- throw new Error(`Error running Civet config file ${path2}: ${e}`);
145
+ throw new Error(`Error running Civet config file ${pathname}: ${e}`);
114
146
  }
115
147
  }
116
148
  if (!(data != null && typeof data === "object" && !Array.isArray(data))) {
package/dist/esbuild.js CHANGED
@@ -100,6 +100,8 @@ var rawPlugin = (options = {}, meta) => {
100
100
  let compilerOptions, compilerOptionsWithSourceMap;
101
101
  let rootDir = process.cwd();
102
102
  let esbuildOptions;
103
+ let configErrors;
104
+ let configFileNames;
103
105
  const tsPromise = transformTS || options.ts === "tsc" ? import("typescript").then((m) => m.default) : null;
104
106
  const getFormatHost = (sys) => {
105
107
  return {
@@ -114,6 +116,16 @@ var rawPlugin = (options = {}, meta) => {
114
116
  enforce: "pre",
115
117
  async buildStart() {
116
118
  if (transformTS || options.ts === "tsc") {
119
+ let mogrify2 = function(key) {
120
+ if (key in config && Array.isArray(config[key])) {
121
+ config[key] = config[key].map((item) => {
122
+ if (typeof item !== "string")
123
+ return item;
124
+ return item.replace(/\.civet\b(?!\.)/g, ".civet.tsx");
125
+ });
126
+ }
127
+ };
128
+ var mogrify = mogrify2;
117
129
  const ts = await tsPromise;
118
130
  const civetConfigPath = "config" in options ? options.config : await (0, import_config.findInDir)(process.cwd());
119
131
  if (civetConfigPath) {
@@ -135,17 +147,26 @@ var rawPlugin = (options = {}, meta) => {
135
147
  console.error(ts.formatDiagnostic(error, getFormatHost(ts.sys)));
136
148
  throw error;
137
149
  }
150
+ mogrify2("files");
151
+ const system = { ...ts.sys };
152
+ const { readDirectory: systemReadDirectory } = system;
153
+ system.readDirectory = (path2, extensions, excludes, includes, depth) => {
154
+ extensions = [...extensions ?? [], ".civet"];
155
+ return systemReadDirectory(path2, extensions, excludes, includes, depth).map((name) => name.endsWith(".civet") ? name + ".tsx" : name);
156
+ };
138
157
  const configContents = ts.parseJsonConfigFileContent(
139
158
  config,
140
- ts.sys,
159
+ system,
141
160
  process.cwd()
142
161
  );
162
+ configErrors = configContents.errors;
163
+ configFileNames = configContents.fileNames;
143
164
  compilerOptions = {
144
165
  ...configContents.options,
145
166
  target: ts.ScriptTarget.ESNext,
146
167
  composite: false
147
168
  };
148
- compilerOptions.jsx ?? (compilerOptions.jsx = "preserve");
169
+ compilerOptions.jsx ?? (compilerOptions.jsx = ts.JsxEmit.Preserve);
149
170
  compilerOptionsWithSourceMap = {
150
171
  ...compilerOptions,
151
172
  sourceMap: true
@@ -153,11 +174,15 @@ var rawPlugin = (options = {}, meta) => {
153
174
  fsMap = /* @__PURE__ */ new Map();
154
175
  }
155
176
  },
156
- async buildEnd() {
177
+ async buildEnd(useConfigFileNames = false) {
157
178
  if (transformTS) {
158
179
  const ts = await tsPromise;
159
180
  const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
160
- const { fileExists: systemFileExists, readFile: systemReadFile } = system;
181
+ const {
182
+ fileExists: systemFileExists,
183
+ readFile: systemReadFile,
184
+ readDirectory: systemReadDirectory
185
+ } = system;
161
186
  system.fileExists = (filename) => {
162
187
  if (!filename.endsWith(".civet.tsx"))
163
188
  return systemFileExists(filename);
@@ -165,9 +190,35 @@ var rawPlugin = (options = {}, meta) => {
165
190
  return true;
166
191
  return systemFileExists(filename.slice(0, -4));
167
192
  };
193
+ system.readDirectory = (path2) => systemReadDirectory(path2).map((name) => name.endsWith(".civet") ? name + ".tsx" : name);
168
194
  system.readFile = (filename, encoding = "utf-8") => {
195
+ if (import_path.default.basename(filename) === "package.json") {
196
+ let recurse2 = function(node) {
197
+ if (node && typeof node === "object") {
198
+ for (const key in node) {
199
+ const value = node[key];
200
+ if (typeof value === "string") {
201
+ if (value.endsWith(".civet")) {
202
+ node[key] = value + ".tsx";
203
+ modified = true;
204
+ }
205
+ } else if (value) {
206
+ recurse2(value);
207
+ }
208
+ }
209
+ }
210
+ };
211
+ var recurse = recurse2;
212
+ const json = systemReadFile(filename, encoding);
213
+ if (!json)
214
+ return json;
215
+ const parsed = JSON.parse(json);
216
+ let modified = false;
217
+ recurse2(parsed.imports);
218
+ return modified ? JSON.stringify(parsed) : json;
219
+ }
169
220
  if (!filename.endsWith(".civet.tsx"))
170
- return systemReadFile(filename);
221
+ return systemReadFile(filename, encoding);
171
222
  if (fsMap.has(filename))
172
223
  return fsMap.get(filename);
173
224
  const civetFilename = filename.slice(0, -4);
@@ -189,8 +240,9 @@ var rawPlugin = (options = {}, meta) => {
189
240
  compilerOptions,
190
241
  ts
191
242
  );
243
+ host.compilerHost.getDirectories = system.getDirectories;
192
244
  const program = ts.createProgram({
193
- rootNames: [...fsMap.keys()],
245
+ rootNames: useConfigFileNames ? configFileNames : [...fsMap.keys()],
194
246
  options: compilerOptions,
195
247
  host: host.compilerHost
196
248
  });
@@ -216,6 +268,9 @@ var rawPlugin = (options = {}, meta) => {
216
268
  start: range.start
217
269
  };
218
270
  });
271
+ if (configErrors?.length) {
272
+ diagnostics.unshift(...configErrors);
273
+ }
219
274
  if (diagnostics.length > 0) {
220
275
  console.error(
221
276
  ts.formatDiagnosticsWithColorAndContext(