pluggable-codegen 0.3.2 → 0.3.4

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,6 @@ 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,
43
41
  },
44
42
  // followed by one or more codegen jobs:
45
43
  {
@@ -52,15 +50,20 @@ export default defineCodegen(
52
50
 
53
51
  // now do something fun and return a Buffer or a string;
54
52
  // you can also return null, in which case nothing will be written
55
- return `export const fileList = ${JSON.stringify(files)};`;
53
+ const fileList = files.map((file) => file.fullPath);
54
+ return `export const fileList = ${JSON.stringify(fileList, null, 2)};`;
56
55
  },
57
56
  // set to 'files' if your generated output is based only on
58
57
  // the list of file names, or to 'contents' if the output is
59
- // based on the files' contents:
58
+ // based on the files' contents; leave undefined to disable cache:
60
59
  cacheBy: undefined,
61
- // you can override the global 'eslint', 'prettier',
62
- // and 'glob' options per-job:
63
- // prettier: false,
60
+ // provide an array of additional glob patterns to consider when
61
+ // checking the cache; this can include e.g. `import.meta.filename`
62
+ // to make the job rebuild when its definition changes
63
+ extraDeps: undefined,
64
+ // you can override the global 'eslint' and 'prettier' options
65
+ // per-job, e.g.:
66
+ prettier: false,
64
67
  },
65
68
  );
66
69
  ```
@@ -132,16 +135,10 @@ declare class File {
132
135
 
133
136
  ## Presets
134
137
 
135
- ### SvelteKit Routegen
136
-
137
- The [`sveltekit-routegen`](https://github.com/jahudka/sveltekit-routegen)
138
- package (which you can install separately) exports a codegen preset which
139
- generates type-safe helpers for generating URLs from routes in SvelteKit.
140
-
141
138
  ### GraphQL Codegen
142
139
 
143
- This preset wraps the `graphql-code-generator` package so that you don't
144
- have to run `gql-gen --watch` separately. It doesn't accept any options;
145
- simply configure `gql-gen` using a `graphql.config.yml` file as you would if
146
- you used `gql-gen` standalone and add the `gqlgen()` job exported from
147
- `pluggable-codegen/presets/graphql-code-generator`.
140
+ This preset wraps the `@graphql-codegen/cli` package so that you don't
141
+ have to run `graphql-codegen --watch` separately. It doesn't accept any options;
142
+ simply configure `graphql-codegen` using a `codegen.yml` file as you would if
143
+ you used `graphql-codegen` standalone and add the `graphqlCodegen()` job
144
+ exported from `pluggable-codegen/presets/graphql-codegen`.
package/dist/codegen.cjs CHANGED
@@ -39,28 +39,36 @@ var Codegen = class Codegen {
39
39
  this.#running.get(job)?.abort();
40
40
  const controller = new AbortController();
41
41
  this.#running.set(job, controller);
42
- const files = await this.#scanFiles(job);
43
- await this.#cache.check(files, job.output, job.cacheBy, async () => {
44
- const file = (0, node_path.resolve)(this.#options.rootDir, job.output);
45
- let output = await job.generate(files, controller.signal);
46
- if (output === null) return;
47
- if (controller.signal.aborted) return;
48
- if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
49
- if (controller.signal.aborted) return;
50
- if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
51
- if (controller.signal.aborted) return;
42
+ const files = await this.#scanFiles(job.input);
43
+ const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
44
+ await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
52
45
  try {
53
- await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
54
- mode: 488,
55
- recursive: true
56
- });
57
- } catch {}
58
- await (0, node_fs_promises.writeFile)(file, output);
59
- return require_utils.hash(output);
46
+ const file = (0, node_path.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
+ if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
51
+ if (controller.signal.aborted) return;
52
+ if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
53
+ if (controller.signal.aborted) return;
54
+ try {
55
+ await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
56
+ mode: 488,
57
+ recursive: true
58
+ });
59
+ } catch {}
60
+ await (0, node_fs_promises.writeFile)(file, output);
61
+ return require_utils.hash(output);
62
+ } catch (e) {
63
+ console.log(`\nError while generating '${job.output}':`);
64
+ console.log(e);
65
+ console.log("");
66
+ return;
67
+ }
60
68
  });
61
69
  }
62
- async #scanFiles(job) {
63
- return (await (0, tinyglobby.glob)(job.input, {
70
+ async #scanFiles(patterns) {
71
+ return (await (0, tinyglobby.glob)(patterns, {
64
72
  cwd: this.#options.rootDir,
65
73
  absolute: true
66
74
  })).sort().map((path) => new require_file.File(path, this.#options.rootDir));
@@ -88,7 +96,7 @@ var Codegen = class Codegen {
88
96
  }
89
97
  #createMatcher(job) {
90
98
  const root = (0, tinyglobby.escapePath)(this.#options.rootDir);
91
- return (0, picomatch.default)(Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`);
99
+ return (0, picomatch.default)([job.input, job.extraDeps ?? []].flat().map((pattern) => (0, node_path.resolve)(root, pattern)));
92
100
  }
93
101
  };
94
102
 
package/dist/codegen.mjs CHANGED
@@ -37,28 +37,36 @@ var Codegen = class Codegen {
37
37
  this.#running.get(job)?.abort();
38
38
  const controller = new AbortController();
39
39
  this.#running.set(job, controller);
40
- const files = await this.#scanFiles(job);
41
- await this.#cache.check(files, job.output, job.cacheBy, async () => {
42
- const file = resolve(this.#options.rootDir, job.output);
43
- let output = await job.generate(files, controller.signal);
44
- if (output === null) return;
45
- if (controller.signal.aborted) return;
46
- if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
47
- if (controller.signal.aborted) return;
48
- if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
49
- if (controller.signal.aborted) return;
40
+ const files = await this.#scanFiles(job.input);
41
+ const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
42
+ await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
50
43
  try {
51
- await mkdir(dirname(file), {
52
- mode: 488,
53
- recursive: true
54
- });
55
- } catch {}
56
- await writeFile(file, output);
57
- return hash(output);
44
+ const file = resolve(this.#options.rootDir, job.output);
45
+ let output = await job.generate(files, controller.signal);
46
+ if (output === null) return;
47
+ if (controller.signal.aborted) return;
48
+ if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
49
+ if (controller.signal.aborted) return;
50
+ if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) output = await this.#applyPrettier(output, file);
51
+ if (controller.signal.aborted) return;
52
+ try {
53
+ await mkdir(dirname(file), {
54
+ mode: 488,
55
+ recursive: true
56
+ });
57
+ } catch {}
58
+ await writeFile(file, output);
59
+ return hash(output);
60
+ } catch (e) {
61
+ console.log(`\nError while generating '${job.output}':`);
62
+ console.log(e);
63
+ console.log("");
64
+ return;
65
+ }
58
66
  });
59
67
  }
60
- async #scanFiles(job) {
61
- return (await glob(job.input, {
68
+ async #scanFiles(patterns) {
69
+ return (await glob(patterns, {
62
70
  cwd: this.#options.rootDir,
63
71
  absolute: true
64
72
  })).sort().map((path) => new File(path, this.#options.rootDir));
@@ -86,7 +94,7 @@ var Codegen = class Codegen {
86
94
  }
87
95
  #createMatcher(job) {
88
96
  const root = escapePath(this.#options.rootDir);
89
- return picomatch(Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`);
97
+ return picomatch([job.input, job.extraDeps ?? []].flat().map((pattern) => resolve(root, pattern)));
90
98
  }
91
99
  };
92
100
 
package/dist/options.cjs CHANGED
@@ -27,7 +27,7 @@ async function resolveCacheFile(rootDir, cacheFile) {
27
27
  else if (cacheFile !== void 0) return (0, node_path.resolve)(rootDir, cacheFile);
28
28
  const codegenPath = typeof __dirname !== "undefined" ? __dirname : __dirname;
29
29
  if (!codegenPath.includes("/node_modules/")) return;
30
- cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
30
+ cacheFile = codegenPath.replace(/^(.+?\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
31
31
  try {
32
32
  await (0, node_fs_promises.mkdir)((0, node_path.dirname)(cacheFile), {
33
33
  recursive: true,
package/dist/options.mjs CHANGED
@@ -26,7 +26,7 @@ async function resolveCacheFile(rootDir, cacheFile) {
26
26
  else if (cacheFile !== void 0) return resolve(rootDir, cacheFile);
27
27
  const codegenPath = typeof __dirname !== "undefined" ? __dirname : import.meta.dirname;
28
28
  if (!codegenPath.includes("/node_modules/")) return;
29
- cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
29
+ cacheFile = codegenPath.replace(/^(.+?\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
30
30
  try {
31
31
  await mkdir(dirname(cacheFile), {
32
32
  recursive: true,
@@ -5,7 +5,7 @@ let _graphql_codegen_cli = require("@graphql-codegen/cli");
5
5
  //#region src/presets/graphql-codegen.ts
6
6
  function graphqlCodegen(options = {}) {
7
7
  return async () => {
8
- const config = (await (0, _graphql_codegen_cli.createContext)({
8
+ const context = await (0, _graphql_codegen_cli.createContext)({
9
9
  config: options.config ?? "",
10
10
  project: options.project ?? "",
11
11
  watch: false,
@@ -16,12 +16,14 @@ function graphqlCodegen(options = {}) {
16
16
  profile: false,
17
17
  verbose: true,
18
18
  debug: true
19
- })).getConfig();
19
+ });
20
+ const config = context.getConfig();
20
21
  return Object.entries(config.generates).map(([output, options]) => {
21
22
  return {
22
23
  input: [...normalizeDocuments(config.documents), ...Array.isArray(options) ? [] : normalizeDocuments(options.documents)],
23
24
  output,
24
25
  cacheBy: "contents",
26
+ extraDeps: [context.filepath],
25
27
  generate: async (files) => {
26
28
  const [generatedFile] = await (0, _graphql_codegen_cli.generate)({
27
29
  ...config,
@@ -3,7 +3,7 @@ import { createContext, generate } from "@graphql-codegen/cli";
3
3
  //#region src/presets/graphql-codegen.ts
4
4
  function graphqlCodegen(options = {}) {
5
5
  return async () => {
6
- const config = (await createContext({
6
+ const context = await createContext({
7
7
  config: options.config ?? "",
8
8
  project: options.project ?? "",
9
9
  watch: false,
@@ -14,12 +14,14 @@ function graphqlCodegen(options = {}) {
14
14
  profile: false,
15
15
  verbose: true,
16
16
  debug: true
17
- })).getConfig();
17
+ });
18
+ const config = context.getConfig();
18
19
  return Object.entries(config.generates).map(([output, options]) => {
19
20
  return {
20
21
  input: [...normalizeDocuments(config.documents), ...Array.isArray(options) ? [] : normalizeDocuments(options.documents)],
21
22
  output,
22
23
  cacheBy: "contents",
24
+ extraDeps: [context.filepath],
23
25
  generate: async (files) => {
24
26
  const [generatedFile] = await generate({
25
27
  ...config,
package/dist/types.d.mts CHANGED
@@ -19,6 +19,7 @@ type CodegenJob = {
19
19
  output: string;
20
20
  generate: (files: File[], signal: AbortSignal) => MaybePromise<Buffer | string | null>;
21
21
  cacheBy?: 'files' | 'contents';
22
+ extraDeps?: string[];
22
23
  prettier?: boolean;
23
24
  eslint?: boolean;
24
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pluggable-codegen",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/jahudka/pluggable-codegen",
6
6
  "repository": {
@@ -56,7 +56,6 @@
56
56
  }
57
57
  },
58
58
  "devDependencies": {
59
- "@eslint/compat": "^2.1.0",
60
59
  "@eslint/js": "^10.0.1",
61
60
  "@graphql-codegen/cli": "^7.0.0",
62
61
  "@types/node": "^22.19.19",