pluggable-codegen 0.3.3 → 0.4.0

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/README.md CHANGED
@@ -38,8 +38,7 @@ export default defineCodegen(
38
38
  // auto-detected based on devDependencies:
39
39
  eslint: undefined,
40
40
  prettier: undefined,
41
- // global overrides for tinyglobby options:
42
- glob: undefined,
41
+ oxfmt: undefined, // only an '.oxfmtrc.json' config file is supported for now
43
42
  },
44
43
  // followed by one or more codegen jobs:
45
44
  {
@@ -52,19 +51,20 @@ export default defineCodegen(
52
51
 
53
52
  // now do something fun and return a Buffer or a string;
54
53
  // you can also return null, in which case nothing will be written
55
- return `export const fileList = ${JSON.stringify(files)};`;
54
+ const fileList = files.map((file) => file.fullPath);
55
+ return `export const fileList = ${JSON.stringify(fileList, null, 2)};`;
56
56
  },
57
57
  // set to 'files' if your generated output is based only on
58
58
  // the list of file names, or to 'contents' if the output is
59
- // based on the files' contents:
59
+ // based on the files' contents; leave undefined to disable cache:
60
60
  cacheBy: undefined,
61
61
  // provide an array of additional glob patterns to consider when
62
62
  // checking the cache; this can include e.g. `import.meta.filename`
63
63
  // to make the job rebuild when its definition changes
64
64
  extraDeps: undefined,
65
- // you can override the global 'eslint', 'prettier',
66
- // and 'glob' options per-job:
67
- // prettier: false,
65
+ // you can override the global 'eslint', 'prettier', and 'oxfmt' options
66
+ // per-job, e.g.:
67
+ prettier: false,
68
68
  },
69
69
  );
70
70
  ```
package/dist/codegen.cjs CHANGED
@@ -3,6 +3,7 @@ const require_utils = require('./utils.cjs');
3
3
  const require_cache = require('./cache.cjs');
4
4
  const require_file = require('./file.cjs');
5
5
  const require_options = require('./options.cjs');
6
+ const require_oxfmt = require('./oxfmt.cjs');
6
7
  let node_fs_promises = require("node:fs/promises");
7
8
  let node_path = require("node:path");
8
9
  let picomatch = require("picomatch");
@@ -15,6 +16,7 @@ var Codegen = class Codegen {
15
16
  #cache;
16
17
  #jobs;
17
18
  #running = /* @__PURE__ */ new Map();
19
+ #oxfmt;
18
20
  #eslint;
19
21
  #prettier;
20
22
  static async init(jobOrOptions, ...moreJobs) {
@@ -42,30 +44,34 @@ var Codegen = class Codegen {
42
44
  const files = await this.#scanFiles(job.input);
43
45
  const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
44
46
  await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
45
- const file = (0, node_path.resolve)(this.#options.rootDir, job.output);
46
- let output;
47
47
  try {
48
- output = await job.generate(files, controller.signal);
48
+ const file = (0, node_path.resolve)(this.#options.rootDir, job.output);
49
+ let output = await job.generate(files, controller.signal);
50
+ if (output === null) return;
51
+ if (controller.signal.aborted) return;
52
+ const oxfmt = job.oxfmt ?? this.#options.oxfmt;
53
+ const eslint = job.eslint ?? this.#options.eslint;
54
+ const prettier = job.prettier ?? this.#options.prettier;
55
+ if (typeof output === "string" && oxfmt) output = await this.#applyOxfmt(output, file);
56
+ if (controller.signal.aborted) return;
57
+ if (typeof output === "string" && !oxfmt && eslint) output = await this.#applyESLint(output, file);
58
+ if (controller.signal.aborted) return;
59
+ if (typeof output === "string" && !oxfmt && prettier) output = await this.#applyPrettier(output, file);
60
+ if (controller.signal.aborted) return;
61
+ try {
62
+ await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
63
+ mode: 488,
64
+ recursive: true
65
+ });
66
+ } catch {}
67
+ await (0, node_fs_promises.writeFile)(file, output);
68
+ return require_utils.hash(output);
49
69
  } catch (e) {
50
70
  console.log(`\nError while generating '${job.output}':`);
51
71
  console.log(e);
52
72
  console.log("");
53
73
  return;
54
74
  }
55
- if (output === null) return;
56
- if (controller.signal.aborted) return;
57
- if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
58
- if (controller.signal.aborted) return;
59
- if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
60
- if (controller.signal.aborted) return;
61
- try {
62
- await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
63
- mode: 488,
64
- recursive: true
65
- });
66
- } catch {}
67
- await (0, node_fs_promises.writeFile)(file, output);
68
- return require_utils.hash(output);
69
75
  });
70
76
  }
71
77
  async #scanFiles(patterns) {
@@ -74,6 +80,14 @@ var Codegen = class Codegen {
74
80
  absolute: true
75
81
  })).sort().map((path) => new require_file.File(path, this.#options.rootDir));
76
82
  }
83
+ async #applyOxfmt(source, path) {
84
+ const oxfmt = await this.#loadOxfmt();
85
+ const config = await require_oxfmt.resolveOxfmtConfig(path);
86
+ const { code, errors } = await oxfmt.format(path, source, config);
87
+ const firstError = errors.filter((e) => e.severity === "Error").pop();
88
+ if (firstError) throw new Error(firstError.message);
89
+ return code;
90
+ }
77
91
  async #applyESLint(source, path) {
78
92
  const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
79
93
  return result?.output ?? source;
@@ -86,6 +100,9 @@ var Codegen = class Codegen {
86
100
  filepath: path
87
101
  });
88
102
  }
103
+ async #loadOxfmt() {
104
+ return this.#oxfmt ??= import("oxfmt");
105
+ }
89
106
  async #loadESLint() {
90
107
  return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
91
108
  }
package/dist/codegen.mjs CHANGED
@@ -2,6 +2,7 @@ import { hash } from "./utils.mjs";
2
2
  import { Cache } from "./cache.mjs";
3
3
  import { File } from "./file.mjs";
4
4
  import { normalizeJobs, normalizeOptions } from "./options.mjs";
5
+ import { resolveOxfmtConfig } from "./oxfmt.mjs";
5
6
  import { mkdir, writeFile } from "node:fs/promises";
6
7
  import { dirname, resolve } from "node:path";
7
8
  import picomatch from "picomatch";
@@ -13,6 +14,7 @@ var Codegen = class Codegen {
13
14
  #cache;
14
15
  #jobs;
15
16
  #running = /* @__PURE__ */ new Map();
17
+ #oxfmt;
16
18
  #eslint;
17
19
  #prettier;
18
20
  static async init(jobOrOptions, ...moreJobs) {
@@ -40,30 +42,34 @@ var Codegen = class Codegen {
40
42
  const files = await this.#scanFiles(job.input);
41
43
  const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
42
44
  await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
43
- const file = resolve(this.#options.rootDir, job.output);
44
- let output;
45
45
  try {
46
- output = await job.generate(files, controller.signal);
46
+ const file = resolve(this.#options.rootDir, job.output);
47
+ let output = await job.generate(files, controller.signal);
48
+ if (output === null) return;
49
+ if (controller.signal.aborted) return;
50
+ const oxfmt = job.oxfmt ?? this.#options.oxfmt;
51
+ const eslint = job.eslint ?? this.#options.eslint;
52
+ const prettier = job.prettier ?? this.#options.prettier;
53
+ if (typeof output === "string" && oxfmt) output = await this.#applyOxfmt(output, file);
54
+ if (controller.signal.aborted) return;
55
+ if (typeof output === "string" && !oxfmt && eslint) output = await this.#applyESLint(output, file);
56
+ if (controller.signal.aborted) return;
57
+ if (typeof output === "string" && !oxfmt && prettier) output = await this.#applyPrettier(output, file);
58
+ if (controller.signal.aborted) return;
59
+ try {
60
+ await mkdir(dirname(file), {
61
+ mode: 488,
62
+ recursive: true
63
+ });
64
+ } catch {}
65
+ await writeFile(file, output);
66
+ return hash(output);
47
67
  } catch (e) {
48
68
  console.log(`\nError while generating '${job.output}':`);
49
69
  console.log(e);
50
70
  console.log("");
51
71
  return;
52
72
  }
53
- if (output === null) return;
54
- if (controller.signal.aborted) return;
55
- if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
56
- if (controller.signal.aborted) return;
57
- if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
58
- if (controller.signal.aborted) return;
59
- try {
60
- await mkdir(dirname(file), {
61
- mode: 488,
62
- recursive: true
63
- });
64
- } catch {}
65
- await writeFile(file, output);
66
- return hash(output);
67
73
  });
68
74
  }
69
75
  async #scanFiles(patterns) {
@@ -72,6 +78,14 @@ var Codegen = class Codegen {
72
78
  absolute: true
73
79
  })).sort().map((path) => new File(path, this.#options.rootDir));
74
80
  }
81
+ async #applyOxfmt(source, path) {
82
+ const oxfmt = await this.#loadOxfmt();
83
+ const config = await resolveOxfmtConfig(path);
84
+ const { code, errors } = await oxfmt.format(path, source, config);
85
+ const firstError = errors.filter((e) => e.severity === "Error").pop();
86
+ if (firstError) throw new Error(firstError.message);
87
+ return code;
88
+ }
75
89
  async #applyESLint(source, path) {
76
90
  const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
77
91
  return result?.output ?? source;
@@ -84,6 +98,9 @@ var Codegen = class Codegen {
84
98
  filepath: path
85
99
  });
86
100
  }
101
+ async #loadOxfmt() {
102
+ return this.#oxfmt ??= import("oxfmt");
103
+ }
87
104
  async #loadESLint() {
88
105
  return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
89
106
  }
package/dist/options.cjs CHANGED
@@ -10,6 +10,7 @@ async function normalizeOptions(options = {}) {
10
10
  return {
11
11
  rootDir,
12
12
  cacheFile: await resolveCacheFile(rootDir, options.cacheFile),
13
+ oxfmt: options.oxfmt ?? hasDevDep(await getPackageJson(), "oxfmt"),
13
14
  prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
14
15
  eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
15
16
  };
package/dist/options.mjs CHANGED
@@ -9,6 +9,7 @@ async function normalizeOptions(options = {}) {
9
9
  return {
10
10
  rootDir,
11
11
  cacheFile: await resolveCacheFile(rootDir, options.cacheFile),
12
+ oxfmt: options.oxfmt ?? hasDevDep(await getPackageJson(), "oxfmt"),
12
13
  prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
13
14
  eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
14
15
  };
package/dist/oxfmt.cjs ADDED
@@ -0,0 +1,24 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ let node_fs_promises = require("node:fs/promises");
4
+ let node_path = require("node:path");
5
+
6
+ //#region src/oxfmt.ts
7
+ const candidates = [".oxfmtrc.json"];
8
+ const cache = /* @__PURE__ */ new Map();
9
+ async function resolveOxfmtConfig(from) {
10
+ const dir = (0, node_path.dirname)(from);
11
+ const cached = cache.get(dir);
12
+ if (cached) return cached;
13
+ const { promise, resolve } = Promise.withResolvers();
14
+ cache.set(dir, promise);
15
+ resolve(await require_utils.lookupPath(dir, candidates, loadConfig));
16
+ return promise;
17
+ }
18
+ async function loadConfig(file) {
19
+ const contents = await (0, node_fs_promises.readFile)(file, "utf-8");
20
+ return JSON.parse(contents);
21
+ }
22
+
23
+ //#endregion
24
+ exports.resolveOxfmtConfig = resolveOxfmtConfig;
package/dist/oxfmt.mjs ADDED
@@ -0,0 +1,23 @@
1
+ import { lookupPath } from "./utils.mjs";
2
+ import { readFile } from "node:fs/promises";
3
+ import { dirname } from "node:path";
4
+
5
+ //#region src/oxfmt.ts
6
+ const candidates = [".oxfmtrc.json"];
7
+ const cache = /* @__PURE__ */ new Map();
8
+ async function resolveOxfmtConfig(from) {
9
+ const dir = dirname(from);
10
+ const cached = cache.get(dir);
11
+ if (cached) return cached;
12
+ const { promise, resolve } = Promise.withResolvers();
13
+ cache.set(dir, promise);
14
+ resolve(await lookupPath(dir, candidates, loadConfig));
15
+ return promise;
16
+ }
17
+ async function loadConfig(file) {
18
+ const contents = await readFile(file, "utf-8");
19
+ return JSON.parse(contents);
20
+ }
21
+
22
+ //#endregion
23
+ export { resolveOxfmtConfig };
package/dist/types.d.mts CHANGED
@@ -5,12 +5,14 @@ type MaybePromise<T> = Promise<T> | T;
5
5
  type CodegenOptions = {
6
6
  rootDir?: string;
7
7
  cacheFile?: string | false;
8
+ oxfmt?: boolean;
8
9
  prettier?: boolean;
9
10
  eslint?: boolean;
10
11
  };
11
12
  type NormalizedCodegenOptions = {
12
13
  rootDir: string;
13
14
  cacheFile?: string;
15
+ oxfmt: boolean;
14
16
  prettier: boolean;
15
17
  eslint: boolean;
16
18
  };
@@ -20,6 +22,7 @@ type CodegenJob = {
20
22
  generate: (files: File[], signal: AbortSignal) => MaybePromise<Buffer | string | null>;
21
23
  cacheBy?: 'files' | 'contents';
22
24
  extraDeps?: string[];
25
+ oxfmt?: boolean;
23
26
  prettier?: boolean;
24
27
  eslint?: boolean;
25
28
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pluggable-codegen",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/jahudka/pluggable-codegen",
6
6
  "repository": {
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "maintainers": [
14
14
  {
15
- "name": "Dan Kadera",
15
+ "name": "Eliška Daniela Kaderová",
16
16
  "email": "me@subsonic.cz",
17
17
  "url": "https://github.com/jahudka"
18
18
  }
@@ -65,6 +65,7 @@
65
65
  "eslint-plugin-simple-import-sort": "^13.0.0",
66
66
  "eslint-plugin-unused-imports": "^4.4.1",
67
67
  "globals": "^17.6.0",
68
+ "oxfmt": "^0.63.0",
68
69
  "prettier": "^3.8.3",
69
70
  "tsdown": "^0.22.0",
70
71
  "typescript": "^6.0.3",