pluggable-codegen 0.3.2 → 0.3.3
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 +9 -11
- package/dist/codegen.cjs +15 -6
- package/dist/codegen.mjs +15 -6
- package/dist/options.cjs +1 -1
- package/dist/options.mjs +1 -1
- package/dist/presets/graphql-codegen.cjs +4 -2
- package/dist/presets/graphql-codegen.mjs +4 -2
- package/dist/types.d.mts +1 -0
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -58,6 +58,10 @@ export default defineCodegen(
|
|
|
58
58
|
// the list of file names, or to 'contents' if the output is
|
|
59
59
|
// based on the files' contents:
|
|
60
60
|
cacheBy: undefined,
|
|
61
|
+
// provide an array of additional glob patterns to consider when
|
|
62
|
+
// checking the cache; this can include e.g. `import.meta.filename`
|
|
63
|
+
// to make the job rebuild when its definition changes
|
|
64
|
+
extraDeps: undefined,
|
|
61
65
|
// you can override the global 'eslint', 'prettier',
|
|
62
66
|
// and 'glob' options per-job:
|
|
63
67
|
// prettier: false,
|
|
@@ -132,16 +136,10 @@ declare class File {
|
|
|
132
136
|
|
|
133
137
|
## Presets
|
|
134
138
|
|
|
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
139
|
### GraphQL Codegen
|
|
142
140
|
|
|
143
|
-
This preset wraps the
|
|
144
|
-
have to run `
|
|
145
|
-
simply configure `
|
|
146
|
-
you used `
|
|
147
|
-
`pluggable-codegen/presets/graphql-
|
|
141
|
+
This preset wraps the `@graphql-codegen/cli` package so that you don't
|
|
142
|
+
have to run `graphql-codegen --watch` separately. It doesn't accept any options;
|
|
143
|
+
simply configure `graphql-codegen` using a `codegen.yml` file as you would if
|
|
144
|
+
you used `graphql-codegen` standalone and add the `graphqlCodegen()` job
|
|
145
|
+
exported from `pluggable-codegen/presets/graphql-codegen`.
|
package/dist/codegen.cjs
CHANGED
|
@@ -39,10 +39,19 @@ 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
|
-
|
|
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 () => {
|
|
44
45
|
const file = (0, node_path.resolve)(this.#options.rootDir, job.output);
|
|
45
|
-
let output
|
|
46
|
+
let output;
|
|
47
|
+
try {
|
|
48
|
+
output = await job.generate(files, controller.signal);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
console.log(`\nError while generating '${job.output}':`);
|
|
51
|
+
console.log(e);
|
|
52
|
+
console.log("");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
46
55
|
if (output === null) return;
|
|
47
56
|
if (controller.signal.aborted) return;
|
|
48
57
|
if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
|
|
@@ -59,8 +68,8 @@ var Codegen = class Codegen {
|
|
|
59
68
|
return require_utils.hash(output);
|
|
60
69
|
});
|
|
61
70
|
}
|
|
62
|
-
async #scanFiles(
|
|
63
|
-
return (await (0, tinyglobby.glob)(
|
|
71
|
+
async #scanFiles(patterns) {
|
|
72
|
+
return (await (0, tinyglobby.glob)(patterns, {
|
|
64
73
|
cwd: this.#options.rootDir,
|
|
65
74
|
absolute: true
|
|
66
75
|
})).sort().map((path) => new require_file.File(path, this.#options.rootDir));
|
|
@@ -88,7 +97,7 @@ var Codegen = class Codegen {
|
|
|
88
97
|
}
|
|
89
98
|
#createMatcher(job) {
|
|
90
99
|
const root = (0, tinyglobby.escapePath)(this.#options.rootDir);
|
|
91
|
-
return (0, picomatch.default)(
|
|
100
|
+
return (0, picomatch.default)([job.input, job.extraDeps ?? []].flat().map((pattern) => (0, node_path.resolve)(root, pattern)));
|
|
92
101
|
}
|
|
93
102
|
};
|
|
94
103
|
|
package/dist/codegen.mjs
CHANGED
|
@@ -37,10 +37,19 @@ 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
|
-
|
|
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 () => {
|
|
42
43
|
const file = resolve(this.#options.rootDir, job.output);
|
|
43
|
-
let output
|
|
44
|
+
let output;
|
|
45
|
+
try {
|
|
46
|
+
output = await job.generate(files, controller.signal);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.log(`\nError while generating '${job.output}':`);
|
|
49
|
+
console.log(e);
|
|
50
|
+
console.log("");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
44
53
|
if (output === null) return;
|
|
45
54
|
if (controller.signal.aborted) return;
|
|
46
55
|
if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) output = await this.#applyESLint(output, file);
|
|
@@ -57,8 +66,8 @@ var Codegen = class Codegen {
|
|
|
57
66
|
return hash(output);
|
|
58
67
|
});
|
|
59
68
|
}
|
|
60
|
-
async #scanFiles(
|
|
61
|
-
return (await glob(
|
|
69
|
+
async #scanFiles(patterns) {
|
|
70
|
+
return (await glob(patterns, {
|
|
62
71
|
cwd: this.#options.rootDir,
|
|
63
72
|
absolute: true
|
|
64
73
|
})).sort().map((path) => new File(path, this.#options.rootDir));
|
|
@@ -86,7 +95,7 @@ var Codegen = class Codegen {
|
|
|
86
95
|
}
|
|
87
96
|
#createMatcher(job) {
|
|
88
97
|
const root = escapePath(this.#options.rootDir);
|
|
89
|
-
return picomatch(
|
|
98
|
+
return picomatch([job.input, job.extraDeps ?? []].flat().map((pattern) => resolve(root, pattern)));
|
|
90
99
|
}
|
|
91
100
|
};
|
|
92
101
|
|
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(/^(
|
|
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(/^(
|
|
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
|
|
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
|
-
})
|
|
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
|
|
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
|
-
})
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pluggable-codegen",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
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",
|