pluggable-codegen 0.1.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 +146 -0
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/chunk-EUVL47JP.js +265 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +43 -0
- package/dist/index.cjs +288 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +32 -0
- package/dist/presets/graphql-codegen.cjs +63 -0
- package/dist/presets/graphql-codegen.d.ts +9 -0
- package/dist/presets/graphql-codegen.js +65 -0
- package/dist/types-CtuMLaaY.d.ts +39 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Pluggable Code Generator
|
|
2
|
+
|
|
3
|
+
This package provides a simple mechanism to hook simple custom code generation
|
|
4
|
+
scripts into Rollup and Vite build pipelines. It doesn't generate any code
|
|
5
|
+
by itself - it's just a bridge between Rollup or Vite and another script
|
|
6
|
+
which does the actual generation. It's useful in situations where you want
|
|
7
|
+
to watch a bunch of globs and generate some output from them whenever they
|
|
8
|
+
change. It can also be used standalone, although in that case it doesn't
|
|
9
|
+
provide watch functionality.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm i -D pluggable-codegen
|
|
15
|
+
yarn add -D pluggable-codegen
|
|
16
|
+
pnpm add -D pluggable-codegen
|
|
17
|
+
bun add -D pluggable-codegen
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
You can either provide configuration for your code generation inline in your
|
|
23
|
+
bundler config (`rollup.config.ts` / `vite.config.ts`), or using a separate
|
|
24
|
+
config file (which is useful if you want to run the codegen standalone).
|
|
25
|
+
|
|
26
|
+
### Standalone
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
// codegen.config.js
|
|
30
|
+
|
|
31
|
+
export default defineCodegen(
|
|
32
|
+
// optional global config; default values shown
|
|
33
|
+
{
|
|
34
|
+
rootDir: process.cwd(),
|
|
35
|
+
// set to false to disable caching:
|
|
36
|
+
cacheFile: 'node_modules/.cache/pluggable-codegen.json',
|
|
37
|
+
// auto-detected based on devDependencies:
|
|
38
|
+
eslint: undefined,
|
|
39
|
+
prettier: undefined,
|
|
40
|
+
// global overrides for tinyglobby options:
|
|
41
|
+
glob: undefined,
|
|
42
|
+
},
|
|
43
|
+
// followed by one or more codegen jobs:
|
|
44
|
+
{
|
|
45
|
+
input: 'src/some/interesting/files/*.ts', // also can be an array
|
|
46
|
+
output: 'src/some/generated/file.ts',
|
|
47
|
+
generate: async (files, signal) => {
|
|
48
|
+
// 'files' is an array of instances of the File class
|
|
49
|
+
// exported by the package, see below;
|
|
50
|
+
// 'signal' is an abort signal
|
|
51
|
+
|
|
52
|
+
// now do something fun and return a Buffer or a string;
|
|
53
|
+
// you can also return null, in which case nothing will be written
|
|
54
|
+
return `export const fileList = ${JSON.stringify(files)};`;
|
|
55
|
+
},
|
|
56
|
+
// set to 'files' if your generated output is based only on the file names,
|
|
57
|
+
// or to 'contents' if the output is based on the files' contents:
|
|
58
|
+
cacheBy: undefined,
|
|
59
|
+
// you can override the global 'eslint', 'prettier', and 'glob' options
|
|
60
|
+
// per-job:
|
|
61
|
+
// prettier: false,
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The config file can also have a `.cjs` or a `.mjs` extension depending on your
|
|
67
|
+
need; either will be autodetected.
|
|
68
|
+
|
|
69
|
+
You can run the codegen using `node_modules/.bin/pluggable-codegen` or
|
|
70
|
+
popular wrappers like `npx` or `bun x`. If you're using a non-standard
|
|
71
|
+
config file name, you can pass it as the first argument.
|
|
72
|
+
|
|
73
|
+
### Using bundler plugin
|
|
74
|
+
|
|
75
|
+
Within a bundler config, such as `vite.config.ts`, you would just use
|
|
76
|
+
the `codegen()` plugin instead of the `defineCodegen()` typing helper:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// vite.config.ts
|
|
80
|
+
import { defineConfig } from 'vite';
|
|
81
|
+
import { codegen } from 'pluggable-codegen';
|
|
82
|
+
|
|
83
|
+
export default defineConfig({
|
|
84
|
+
plugins: [
|
|
85
|
+
codegen(),
|
|
86
|
+
// same arguments as defineCodegen()
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
In fact, you can combine the two if you want to run the codegen as a bundler
|
|
92
|
+
plugin _as well as_ standalone - this is useful e.g. after `svelte-kit sync`
|
|
93
|
+
and before `svelte-check`:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// vite.config.ts
|
|
97
|
+
|
|
98
|
+
import { defineConfig } from 'vite';
|
|
99
|
+
import { codegen } from 'pluggable-codegen';
|
|
100
|
+
import codegenConfig from './codegen.config';
|
|
101
|
+
|
|
102
|
+
export default defineConfig({
|
|
103
|
+
plugins: [codegen(...codegenConfig)],
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
When used as a bundler plugin, the codegen will run all jobs once at the
|
|
108
|
+
beginning of the build, and each job will be rerun if any of its input files
|
|
109
|
+
change.
|
|
110
|
+
|
|
111
|
+
## The `File` class
|
|
112
|
+
|
|
113
|
+
The list of files matching the input globs will be passed as an array of
|
|
114
|
+
instances of the `File` class. This class has the following methods and
|
|
115
|
+
properties:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
declare class File {
|
|
119
|
+
// full absolute path to the file:
|
|
120
|
+
readonly fullPath: string;
|
|
121
|
+
// relative path from rootDir with leading './' stripped:
|
|
122
|
+
readonly localPath: string;
|
|
123
|
+
|
|
124
|
+
// read file contents; all of these methods cache the raw buffer
|
|
125
|
+
// in memory for later reuse:
|
|
126
|
+
buffer(): Promise<Buffer>;
|
|
127
|
+
text(): Promise<string>;
|
|
128
|
+
json(): Promise<any>;
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Presets
|
|
133
|
+
|
|
134
|
+
### SvelteKit Routegen
|
|
135
|
+
|
|
136
|
+
The [`sveltekit-routegen`](https://www.npmjs.com/package/sveltekit-routegen)
|
|
137
|
+
package (which you can install separately) exports a codegen preset which
|
|
138
|
+
generates type-safe helpers for generating URLs from routes in SvelteKit.
|
|
139
|
+
|
|
140
|
+
### GraphQL Codegen
|
|
141
|
+
|
|
142
|
+
This preset wraps the `graphql-code-generator` codegen so that you don't
|
|
143
|
+
have to run `gql-gen --watch` separately. It doesn't accept any options;
|
|
144
|
+
simply configure `gql-gen` using the `graphql.config.yml` file as you would if
|
|
145
|
+
you used `gql-gen` standalone and add the `gqlgen()` job exported from
|
|
146
|
+
`pluggable-codegen/presets/graphql-code-generator`.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
__require
|
|
10
|
+
};
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// src/file.ts
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { relative } from "node:path";
|
|
4
|
+
var File = class {
|
|
5
|
+
fullPath;
|
|
6
|
+
localPath;
|
|
7
|
+
#contents;
|
|
8
|
+
constructor(path, rootDir) {
|
|
9
|
+
this.fullPath = path;
|
|
10
|
+
this.localPath = relative(rootDir, path).replace(/^\.(?:\/|$)/, "");
|
|
11
|
+
}
|
|
12
|
+
async buffer() {
|
|
13
|
+
return this.#contents ??= readFile(this.fullPath);
|
|
14
|
+
}
|
|
15
|
+
async text(encoding = "utf-8") {
|
|
16
|
+
const contents = await this.buffer();
|
|
17
|
+
return contents.toString(encoding);
|
|
18
|
+
}
|
|
19
|
+
async json() {
|
|
20
|
+
return JSON.parse(await this.text());
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/utils.ts
|
|
25
|
+
import { dirname, resolve } from "node:path";
|
|
26
|
+
async function lookupPath(dir, candidates, check) {
|
|
27
|
+
if (!Array.isArray(candidates)) {
|
|
28
|
+
candidates = [candidates];
|
|
29
|
+
}
|
|
30
|
+
do {
|
|
31
|
+
for (const candidate of candidates) {
|
|
32
|
+
const path = resolve(dir, candidate);
|
|
33
|
+
try {
|
|
34
|
+
return await check(path);
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} while (cdup());
|
|
39
|
+
return void 0;
|
|
40
|
+
function cdup() {
|
|
41
|
+
const orig = dir;
|
|
42
|
+
dir = dirname(dir);
|
|
43
|
+
return dir !== orig;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/codegen.ts
|
|
48
|
+
import { mkdir as mkdir2, writeFile as writeFile2 } from "node:fs/promises";
|
|
49
|
+
import { dirname as dirname3, resolve as resolve3 } from "node:path";
|
|
50
|
+
import picomatch from "picomatch";
|
|
51
|
+
import { escapePath, glob } from "tinyglobby";
|
|
52
|
+
|
|
53
|
+
// src/cache.ts
|
|
54
|
+
import { createHash } from "node:crypto";
|
|
55
|
+
import { readFile as readFile2, writeFile } from "node:fs/promises";
|
|
56
|
+
var Cache = class {
|
|
57
|
+
#cacheFile;
|
|
58
|
+
#cacheData;
|
|
59
|
+
#writeTmr;
|
|
60
|
+
constructor(cacheFile) {
|
|
61
|
+
this.#cacheFile = cacheFile;
|
|
62
|
+
}
|
|
63
|
+
async check(files, output, mode, generate) {
|
|
64
|
+
if (!mode) {
|
|
65
|
+
await generate();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const [cacheData, hash] = await Promise.all([
|
|
69
|
+
this.#cacheData ??= this.#readCacheData(),
|
|
70
|
+
this.#getHash(files, mode)
|
|
71
|
+
]);
|
|
72
|
+
if (cacheData.get(output) === hash || !await generate()) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
cacheData.set(output, hash);
|
|
76
|
+
clearTimeout(this.#writeTmr);
|
|
77
|
+
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
78
|
+
}
|
|
79
|
+
async #getHash(files, mode) {
|
|
80
|
+
const hash = createHash("sha256");
|
|
81
|
+
for (const file of files) {
|
|
82
|
+
hash.update(mode === "files" ? file.localPath : await file.buffer());
|
|
83
|
+
}
|
|
84
|
+
return hash.digest("hex");
|
|
85
|
+
}
|
|
86
|
+
async #readCacheData() {
|
|
87
|
+
if (!this.#cacheFile) {
|
|
88
|
+
return /* @__PURE__ */ new Map();
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const cacheData = await readFile2(this.#cacheFile, "utf-8");
|
|
92
|
+
return new Map(Object.entries(JSON.parse(cacheData)));
|
|
93
|
+
} catch {
|
|
94
|
+
return /* @__PURE__ */ new Map();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async #writeCacheData(data) {
|
|
98
|
+
if (this.#cacheFile) {
|
|
99
|
+
await writeFile(this.#cacheFile, JSON.stringify(Object.fromEntries(data)));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/options.ts
|
|
105
|
+
import { mkdir, readFile as readFile3 } from "node:fs/promises";
|
|
106
|
+
import { dirname as dirname2, resolve as resolve2 } from "node:path";
|
|
107
|
+
async function normalizeOptions(options = {}) {
|
|
108
|
+
let packageJson;
|
|
109
|
+
const rootDir = resolve2(options.rootDir ?? process.cwd());
|
|
110
|
+
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
111
|
+
const prettier = options.prettier ?? hasDevDep(await getPackageJson(), "prettier");
|
|
112
|
+
const eslint = options.eslint ?? hasDevDep(await getPackageJson(), "eslint");
|
|
113
|
+
return { rootDir, cacheFile, prettier, eslint };
|
|
114
|
+
async function getPackageJson() {
|
|
115
|
+
return packageJson ??= readPackageJson(rootDir);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function normalizeJobs(rootDir, inputs) {
|
|
119
|
+
const jobs = await Promise.all(
|
|
120
|
+
inputs.map(async (input) => {
|
|
121
|
+
return typeof input === "function" ? input(rootDir) : input;
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
return jobs.flat();
|
|
125
|
+
}
|
|
126
|
+
async function resolveCacheFile(rootDir, cacheFile) {
|
|
127
|
+
if (cacheFile === false) {
|
|
128
|
+
return void 0;
|
|
129
|
+
} else if (cacheFile !== void 0) {
|
|
130
|
+
return resolve2(rootDir, cacheFile);
|
|
131
|
+
}
|
|
132
|
+
const codegenPath = typeof __dirname !== "undefined" ? __dirname : import.meta.dirname;
|
|
133
|
+
if (!codegenPath.includes("/node_modules/")) {
|
|
134
|
+
return void 0;
|
|
135
|
+
}
|
|
136
|
+
cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
|
|
137
|
+
try {
|
|
138
|
+
await mkdir(dirname2(cacheFile), { recursive: true, mode: 488 });
|
|
139
|
+
} catch {
|
|
140
|
+
}
|
|
141
|
+
return cacheFile;
|
|
142
|
+
}
|
|
143
|
+
function hasDevDep(packageJson, dep) {
|
|
144
|
+
return typeof packageJson === "object" && packageJson !== null && "devDependencies" in packageJson && typeof packageJson.devDependencies === "object" && packageJson.devDependencies !== null && dep in packageJson.devDependencies;
|
|
145
|
+
}
|
|
146
|
+
async function readPackageJson(dir) {
|
|
147
|
+
return lookupPath(dir, "package.json", async (path) => {
|
|
148
|
+
const content = await readFile3(path, "utf-8");
|
|
149
|
+
return JSON.parse(content);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/codegen.ts
|
|
154
|
+
var Codegen = class _Codegen {
|
|
155
|
+
#options;
|
|
156
|
+
#cache;
|
|
157
|
+
#jobs;
|
|
158
|
+
#running = /* @__PURE__ */ new Map();
|
|
159
|
+
#eslint;
|
|
160
|
+
#prettier;
|
|
161
|
+
static async init(jobOrOptions, ...moreJobs) {
|
|
162
|
+
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
163
|
+
const normalizedOptions = await normalizeOptions(options);
|
|
164
|
+
const normalizedJobs = await normalizeJobs(normalizedOptions.rootDir, jobs);
|
|
165
|
+
return new _Codegen(normalizedOptions, normalizedJobs);
|
|
166
|
+
}
|
|
167
|
+
constructor(options, jobs) {
|
|
168
|
+
this.#options = options;
|
|
169
|
+
this.#cache = new Cache(options.cacheFile);
|
|
170
|
+
this.#jobs = this.#createJobMap(jobs);
|
|
171
|
+
}
|
|
172
|
+
async generateAll() {
|
|
173
|
+
await Promise.allSettled([...this.#jobs.values()].map(async (job) => this.#generate(job)));
|
|
174
|
+
}
|
|
175
|
+
async generateMatching(path) {
|
|
176
|
+
await Promise.allSettled(
|
|
177
|
+
[...this.#jobs].map(async ([match, job]) => {
|
|
178
|
+
if (match(path)) {
|
|
179
|
+
await this.#generate(job);
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
async #generate(job) {
|
|
185
|
+
this.#running.get(job)?.abort();
|
|
186
|
+
const controller = new AbortController();
|
|
187
|
+
this.#running.set(job, controller);
|
|
188
|
+
const files = await this.#scanFiles(job);
|
|
189
|
+
await this.#cache.check(files, job.output, job.cacheBy, async () => {
|
|
190
|
+
const file = resolve3(this.#options.rootDir, job.output);
|
|
191
|
+
let output = await job.generate(files, controller.signal);
|
|
192
|
+
if (output === null) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if (controller.signal.aborted) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) {
|
|
199
|
+
output = await this.#applyESLint(output, file);
|
|
200
|
+
}
|
|
201
|
+
if (controller.signal.aborted) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) {
|
|
205
|
+
output = await this.#applyPrettier(output, file);
|
|
206
|
+
}
|
|
207
|
+
if (controller.signal.aborted) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
await mkdir2(dirname3(file), { mode: 488, recursive: true });
|
|
212
|
+
} catch {
|
|
213
|
+
}
|
|
214
|
+
await writeFile2(file, output);
|
|
215
|
+
return true;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
async #scanFiles(job) {
|
|
219
|
+
const files = await glob(job.input, {
|
|
220
|
+
cwd: this.#options.rootDir,
|
|
221
|
+
absolute: true
|
|
222
|
+
});
|
|
223
|
+
return files.map((path) => new File(path, this.#options.rootDir));
|
|
224
|
+
}
|
|
225
|
+
async #applyESLint(source, path) {
|
|
226
|
+
const eslint = await this.#loadESLint();
|
|
227
|
+
const [result] = await eslint.lintText(source, {
|
|
228
|
+
filePath: path
|
|
229
|
+
});
|
|
230
|
+
return result?.output ?? source;
|
|
231
|
+
}
|
|
232
|
+
async #applyPrettier(source, path) {
|
|
233
|
+
const prettier = await this.#loadPrettier();
|
|
234
|
+
const options = await prettier.resolveConfig(path);
|
|
235
|
+
return prettier.format(source, {
|
|
236
|
+
...options ?? {},
|
|
237
|
+
filepath: path
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
async #loadESLint() {
|
|
241
|
+
return this.#eslint ??= import("eslint").then(
|
|
242
|
+
({ ESLint }) => new ESLint({
|
|
243
|
+
fix: true
|
|
244
|
+
})
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
async #loadPrettier() {
|
|
248
|
+
return this.#prettier ??= import("prettier");
|
|
249
|
+
}
|
|
250
|
+
#createJobMap(jobs) {
|
|
251
|
+
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
252
|
+
}
|
|
253
|
+
#createMatcher(job) {
|
|
254
|
+
const root = escapePath(this.#options.rootDir);
|
|
255
|
+
return picomatch(
|
|
256
|
+
Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
export {
|
|
262
|
+
File,
|
|
263
|
+
lookupPath,
|
|
264
|
+
Codegen
|
|
265
|
+
};
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
Codegen,
|
|
4
|
+
lookupPath
|
|
5
|
+
} from "./chunk-EUVL47JP.js";
|
|
6
|
+
import {
|
|
7
|
+
__require
|
|
8
|
+
} from "./chunk-3RG5ZIWI.js";
|
|
9
|
+
|
|
10
|
+
// src/cli.ts
|
|
11
|
+
import { resolve } from "node:path";
|
|
12
|
+
var configFiles = ["codegen.config.mjs", "codegen.config.cjs", "codegen.config.js"];
|
|
13
|
+
(async () => {
|
|
14
|
+
const config = process.argv.length > 2 ? await importConfig(resolve(process.argv[2])) : await lookupPath(process.cwd(), configFiles, importConfig);
|
|
15
|
+
if (!config) {
|
|
16
|
+
console.log(`Usage: pluggable-codegen [config]`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
if (!validateConfig(config)) {
|
|
20
|
+
console.log(`Invalid config`);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
const codegen = await Codegen.init(...config);
|
|
24
|
+
await codegen.generateAll();
|
|
25
|
+
})();
|
|
26
|
+
async function importConfig(path) {
|
|
27
|
+
try {
|
|
28
|
+
const config = await import(path);
|
|
29
|
+
return config.default;
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (!path.endsWith(".mjs")) {
|
|
32
|
+
const config = __require(path);
|
|
33
|
+
return config.default ?? config;
|
|
34
|
+
}
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function validateConfig(config) {
|
|
39
|
+
if (!Array.isArray(config) || !config.length) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/codegen.ts
|
|
2
|
+
var _promises = require('fs/promises');
|
|
3
|
+
var _path = require('path');
|
|
4
|
+
var _picomatch = require('picomatch'); var _picomatch2 = _interopRequireDefault(_picomatch);
|
|
5
|
+
var _tinyglobby = require('tinyglobby');
|
|
6
|
+
|
|
7
|
+
// src/cache.ts
|
|
8
|
+
var _crypto = require('crypto');
|
|
9
|
+
|
|
10
|
+
var Cache = class {
|
|
11
|
+
#cacheFile;
|
|
12
|
+
#cacheData;
|
|
13
|
+
#writeTmr;
|
|
14
|
+
constructor(cacheFile) {
|
|
15
|
+
this.#cacheFile = cacheFile;
|
|
16
|
+
}
|
|
17
|
+
async check(files, output, mode, generate) {
|
|
18
|
+
if (!mode) {
|
|
19
|
+
await generate();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const [cacheData, hash] = await Promise.all([
|
|
23
|
+
this.#cacheData ??= this.#readCacheData(),
|
|
24
|
+
this.#getHash(files, mode)
|
|
25
|
+
]);
|
|
26
|
+
if (cacheData.get(output) === hash || !await generate()) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
cacheData.set(output, hash);
|
|
30
|
+
clearTimeout(this.#writeTmr);
|
|
31
|
+
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
32
|
+
}
|
|
33
|
+
async #getHash(files, mode) {
|
|
34
|
+
const hash = _crypto.createHash.call(void 0, "sha256");
|
|
35
|
+
for (const file of files) {
|
|
36
|
+
hash.update(mode === "files" ? file.localPath : await file.buffer());
|
|
37
|
+
}
|
|
38
|
+
return hash.digest("hex");
|
|
39
|
+
}
|
|
40
|
+
async #readCacheData() {
|
|
41
|
+
if (!this.#cacheFile) {
|
|
42
|
+
return /* @__PURE__ */ new Map();
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const cacheData = await _promises.readFile.call(void 0, this.#cacheFile, "utf-8");
|
|
46
|
+
return new Map(Object.entries(JSON.parse(cacheData)));
|
|
47
|
+
} catch (e) {
|
|
48
|
+
return /* @__PURE__ */ new Map();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async #writeCacheData(data) {
|
|
52
|
+
if (this.#cacheFile) {
|
|
53
|
+
await _promises.writeFile.call(void 0, this.#cacheFile, JSON.stringify(Object.fromEntries(data)));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/file.ts
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
var File = class {
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
#contents;
|
|
65
|
+
constructor(path, rootDir) {
|
|
66
|
+
this.fullPath = path;
|
|
67
|
+
this.localPath = _path.relative.call(void 0, rootDir, path).replace(/^\.(?:\/|$)/, "");
|
|
68
|
+
}
|
|
69
|
+
async buffer() {
|
|
70
|
+
return this.#contents ??= _promises.readFile.call(void 0, this.fullPath);
|
|
71
|
+
}
|
|
72
|
+
async text(encoding = "utf-8") {
|
|
73
|
+
const contents = await this.buffer();
|
|
74
|
+
return contents.toString(encoding);
|
|
75
|
+
}
|
|
76
|
+
async json() {
|
|
77
|
+
return JSON.parse(await this.text());
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/options.ts
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// src/utils.ts
|
|
86
|
+
|
|
87
|
+
async function lookupPath(dir, candidates, check) {
|
|
88
|
+
if (!Array.isArray(candidates)) {
|
|
89
|
+
candidates = [candidates];
|
|
90
|
+
}
|
|
91
|
+
do {
|
|
92
|
+
for (const candidate of candidates) {
|
|
93
|
+
const path = _path.resolve.call(void 0, dir, candidate);
|
|
94
|
+
try {
|
|
95
|
+
return await check(path);
|
|
96
|
+
} catch (e2) {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} while (cdup());
|
|
100
|
+
return void 0;
|
|
101
|
+
function cdup() {
|
|
102
|
+
const orig = dir;
|
|
103
|
+
dir = _path.dirname.call(void 0, dir);
|
|
104
|
+
return dir !== orig;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/options.ts
|
|
109
|
+
async function normalizeOptions(options = {}) {
|
|
110
|
+
let packageJson;
|
|
111
|
+
const rootDir = _path.resolve.call(void 0, _nullishCoalesce(options.rootDir, () => ( process.cwd())));
|
|
112
|
+
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
113
|
+
const prettier = await _asyncNullishCoalesce(options.prettier, async () => ( hasDevDep(await getPackageJson(), "prettier")));
|
|
114
|
+
const eslint = await _asyncNullishCoalesce(options.eslint, async () => ( hasDevDep(await getPackageJson(), "eslint")));
|
|
115
|
+
return { rootDir, cacheFile, prettier, eslint };
|
|
116
|
+
async function getPackageJson() {
|
|
117
|
+
return packageJson ??= readPackageJson(rootDir);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function normalizeJobs(rootDir, inputs) {
|
|
121
|
+
const jobs = await Promise.all(
|
|
122
|
+
inputs.map(async (input) => {
|
|
123
|
+
return typeof input === "function" ? input(rootDir) : input;
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
return jobs.flat();
|
|
127
|
+
}
|
|
128
|
+
async function resolveCacheFile(rootDir, cacheFile) {
|
|
129
|
+
if (cacheFile === false) {
|
|
130
|
+
return void 0;
|
|
131
|
+
} else if (cacheFile !== void 0) {
|
|
132
|
+
return _path.resolve.call(void 0, rootDir, cacheFile);
|
|
133
|
+
}
|
|
134
|
+
const codegenPath = typeof __dirname !== "undefined" ? __dirname : import.meta.dirname;
|
|
135
|
+
if (!codegenPath.includes("/node_modules/")) {
|
|
136
|
+
return void 0;
|
|
137
|
+
}
|
|
138
|
+
cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
|
|
139
|
+
try {
|
|
140
|
+
await _promises.mkdir.call(void 0, _path.dirname.call(void 0, cacheFile), { recursive: true, mode: 488 });
|
|
141
|
+
} catch (e3) {
|
|
142
|
+
}
|
|
143
|
+
return cacheFile;
|
|
144
|
+
}
|
|
145
|
+
function hasDevDep(packageJson, dep) {
|
|
146
|
+
return typeof packageJson === "object" && packageJson !== null && "devDependencies" in packageJson && typeof packageJson.devDependencies === "object" && packageJson.devDependencies !== null && dep in packageJson.devDependencies;
|
|
147
|
+
}
|
|
148
|
+
async function readPackageJson(dir) {
|
|
149
|
+
return lookupPath(dir, "package.json", async (path) => {
|
|
150
|
+
const content = await _promises.readFile.call(void 0, path, "utf-8");
|
|
151
|
+
return JSON.parse(content);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/codegen.ts
|
|
156
|
+
var Codegen = class _Codegen {
|
|
157
|
+
#options;
|
|
158
|
+
#cache;
|
|
159
|
+
#jobs;
|
|
160
|
+
#running = /* @__PURE__ */ new Map();
|
|
161
|
+
#eslint;
|
|
162
|
+
#prettier;
|
|
163
|
+
static async init(jobOrOptions, ...moreJobs) {
|
|
164
|
+
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
165
|
+
const normalizedOptions = await normalizeOptions(options);
|
|
166
|
+
const normalizedJobs = await normalizeJobs(normalizedOptions.rootDir, jobs);
|
|
167
|
+
return new _Codegen(normalizedOptions, normalizedJobs);
|
|
168
|
+
}
|
|
169
|
+
constructor(options, jobs) {
|
|
170
|
+
this.#options = options;
|
|
171
|
+
this.#cache = new Cache(options.cacheFile);
|
|
172
|
+
this.#jobs = this.#createJobMap(jobs);
|
|
173
|
+
}
|
|
174
|
+
async generateAll() {
|
|
175
|
+
await Promise.allSettled([...this.#jobs.values()].map(async (job) => this.#generate(job)));
|
|
176
|
+
}
|
|
177
|
+
async generateMatching(path) {
|
|
178
|
+
await Promise.allSettled(
|
|
179
|
+
[...this.#jobs].map(async ([match, job]) => {
|
|
180
|
+
if (match(path)) {
|
|
181
|
+
await this.#generate(job);
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
async #generate(job) {
|
|
187
|
+
_optionalChain([this, 'access', _ => _.#running, 'access', _2 => _2.get, 'call', _3 => _3(job), 'optionalAccess', _4 => _4.abort, 'call', _5 => _5()]);
|
|
188
|
+
const controller = new AbortController();
|
|
189
|
+
this.#running.set(job, controller);
|
|
190
|
+
const files = await this.#scanFiles(job);
|
|
191
|
+
await this.#cache.check(files, job.output, job.cacheBy, async () => {
|
|
192
|
+
const file = _path.resolve.call(void 0, this.#options.rootDir, job.output);
|
|
193
|
+
let output = await job.generate(files, controller.signal);
|
|
194
|
+
if (output === null) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
if (controller.signal.aborted) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
if (typeof output === "string" && (_nullishCoalesce(job.eslint, () => ( this.#options.eslint)))) {
|
|
201
|
+
output = await this.#applyESLint(output, file);
|
|
202
|
+
}
|
|
203
|
+
if (controller.signal.aborted) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if (typeof output === "string" && (_nullishCoalesce(job.prettier, () => ( this.#options.prettier)))) {
|
|
207
|
+
output = await this.#applyPrettier(output, file);
|
|
208
|
+
}
|
|
209
|
+
if (controller.signal.aborted) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
await _promises.mkdir.call(void 0, _path.dirname.call(void 0, file), { mode: 488, recursive: true });
|
|
214
|
+
} catch (e4) {
|
|
215
|
+
}
|
|
216
|
+
await _promises.writeFile.call(void 0, file, output);
|
|
217
|
+
return true;
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
async #scanFiles(job) {
|
|
221
|
+
const files = await _tinyglobby.glob.call(void 0, job.input, {
|
|
222
|
+
cwd: this.#options.rootDir,
|
|
223
|
+
absolute: true
|
|
224
|
+
});
|
|
225
|
+
return files.map((path) => new File(path, this.#options.rootDir));
|
|
226
|
+
}
|
|
227
|
+
async #applyESLint(source, path) {
|
|
228
|
+
const eslint = await this.#loadESLint();
|
|
229
|
+
const [result] = await eslint.lintText(source, {
|
|
230
|
+
filePath: path
|
|
231
|
+
});
|
|
232
|
+
return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _6 => _6.output]), () => ( source));
|
|
233
|
+
}
|
|
234
|
+
async #applyPrettier(source, path) {
|
|
235
|
+
const prettier = await this.#loadPrettier();
|
|
236
|
+
const options = await prettier.resolveConfig(path);
|
|
237
|
+
return prettier.format(source, {
|
|
238
|
+
..._nullishCoalesce(options, () => ( {})),
|
|
239
|
+
filepath: path
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
async #loadESLint() {
|
|
243
|
+
return this.#eslint ??= Promise.resolve().then(() => _interopRequireWildcard(require("eslint"))).then(
|
|
244
|
+
({ ESLint }) => new ESLint({
|
|
245
|
+
fix: true
|
|
246
|
+
})
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
async #loadPrettier() {
|
|
250
|
+
return this.#prettier ??= Promise.resolve().then(() => _interopRequireWildcard(require("prettier")));
|
|
251
|
+
}
|
|
252
|
+
#createJobMap(jobs) {
|
|
253
|
+
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
254
|
+
}
|
|
255
|
+
#createMatcher(job) {
|
|
256
|
+
const root = _tinyglobby.escapePath.call(void 0, this.#options.rootDir);
|
|
257
|
+
return _picomatch2.default.call(void 0,
|
|
258
|
+
Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/plugin.ts
|
|
264
|
+
function codegen(jobOrOptions, ...moreJobs) {
|
|
265
|
+
const init = Codegen.init(jobOrOptions, ...moreJobs);
|
|
266
|
+
return {
|
|
267
|
+
name: "pluggable-codegen",
|
|
268
|
+
async buildStart() {
|
|
269
|
+
const codegen2 = await init;
|
|
270
|
+
await codegen2.generateAll();
|
|
271
|
+
},
|
|
272
|
+
async watchChange(id) {
|
|
273
|
+
const codegen2 = await init;
|
|
274
|
+
await codegen2.generateMatching(id);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// src/types.ts
|
|
280
|
+
function defineCodegen(optionsOrJob, ...jobs) {
|
|
281
|
+
return [optionsOrJob, ...jobs];
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
exports.Codegen = Codegen; exports.File = File; exports.codegen = codegen; exports.defineCodegen = defineCodegen;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { a as CodegenInput, b as CodegenOptions } from './types-CtuMLaaY.js';
|
|
2
|
+
export { C as CodegenJob, c as CodegenJobGenerator, d as CodegenPresetOptions, F as File, N as NormalizedCodegenOptions, e as defineCodegen } from './types-CtuMLaaY.js';
|
|
3
|
+
|
|
4
|
+
declare class Codegen {
|
|
5
|
+
#private;
|
|
6
|
+
static init(...jobs: CodegenInput[]): Promise<Codegen>;
|
|
7
|
+
static init(options: CodegenOptions, ...jobs: CodegenInput[]): Promise<Codegen>;
|
|
8
|
+
static init(jobOrOptions: CodegenInput | CodegenOptions, ...moreJobs: CodegenInput[]): Promise<Codegen>;
|
|
9
|
+
private constructor();
|
|
10
|
+
generateAll(): Promise<void>;
|
|
11
|
+
generateMatching(path: string): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Plugin = {
|
|
15
|
+
name: string;
|
|
16
|
+
buildStart(): Promise<void>;
|
|
17
|
+
watchChange(id: string): Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
declare function codegen(...jobs: CodegenInput[]): Plugin;
|
|
20
|
+
declare function codegen(options: CodegenOptions, ...jobs: CodegenInput[]): Plugin;
|
|
21
|
+
declare function codegen(jobOrOptions: CodegenInput | CodegenOptions, ...moreJobs: CodegenInput[]): Plugin;
|
|
22
|
+
|
|
23
|
+
export { Codegen, CodegenInput, CodegenOptions, type Plugin, codegen };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Codegen,
|
|
3
|
+
File
|
|
4
|
+
} from "./chunk-EUVL47JP.js";
|
|
5
|
+
import "./chunk-3RG5ZIWI.js";
|
|
6
|
+
|
|
7
|
+
// src/plugin.ts
|
|
8
|
+
function codegen(jobOrOptions, ...moreJobs) {
|
|
9
|
+
const init = Codegen.init(jobOrOptions, ...moreJobs);
|
|
10
|
+
return {
|
|
11
|
+
name: "pluggable-codegen",
|
|
12
|
+
async buildStart() {
|
|
13
|
+
const codegen2 = await init;
|
|
14
|
+
await codegen2.generateAll();
|
|
15
|
+
},
|
|
16
|
+
async watchChange(id) {
|
|
17
|
+
const codegen2 = await init;
|
|
18
|
+
await codegen2.generateMatching(id);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/types.ts
|
|
24
|
+
function defineCodegen(optionsOrJob, ...jobs) {
|
|
25
|
+
return [optionsOrJob, ...jobs];
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
Codegen,
|
|
29
|
+
File,
|
|
30
|
+
codegen,
|
|
31
|
+
defineCodegen
|
|
32
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/presets/graphql-codegen.ts
|
|
2
|
+
var _cli = require('@graphql-codegen/cli');
|
|
3
|
+
function graphqlCodegen(options = {}) {
|
|
4
|
+
return async () => {
|
|
5
|
+
const context = await _cli.createContext.call(void 0, {
|
|
6
|
+
config: _nullishCoalesce(options.config, () => ( "")),
|
|
7
|
+
project: _nullishCoalesce(options.project, () => ( "")),
|
|
8
|
+
watch: false,
|
|
9
|
+
require: [],
|
|
10
|
+
overwrite: false,
|
|
11
|
+
silent: false,
|
|
12
|
+
errorsOnly: false,
|
|
13
|
+
profile: false,
|
|
14
|
+
verbose: true,
|
|
15
|
+
debug: true
|
|
16
|
+
});
|
|
17
|
+
const config = context.getConfig();
|
|
18
|
+
return Object.entries(config.generates).map(([output, options2]) => {
|
|
19
|
+
const input = [
|
|
20
|
+
...normalizeDocuments(config.documents),
|
|
21
|
+
...Array.isArray(options2) ? [] : normalizeDocuments(options2.documents)
|
|
22
|
+
];
|
|
23
|
+
return {
|
|
24
|
+
input,
|
|
25
|
+
output,
|
|
26
|
+
cacheBy: "contents",
|
|
27
|
+
generate: async (files) => {
|
|
28
|
+
const [generatedFile] = await _cli.generate.call(void 0,
|
|
29
|
+
{
|
|
30
|
+
...config,
|
|
31
|
+
documents: files.map((file) => file.localPath),
|
|
32
|
+
generates: {
|
|
33
|
+
[output]: {
|
|
34
|
+
...options2,
|
|
35
|
+
documents: void 0
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
false
|
|
40
|
+
);
|
|
41
|
+
return generatedFile.content;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function normalizeDocuments(documents) {
|
|
48
|
+
if (documents === void 0) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(documents)) {
|
|
52
|
+
documents = [documents];
|
|
53
|
+
}
|
|
54
|
+
for (const document of documents) {
|
|
55
|
+
if (typeof document !== "string") {
|
|
56
|
+
throw new Error("Custom loaders are not supported");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return documents;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
exports.graphqlCodegen = graphqlCodegen;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { C as CodegenJob } from '../types-CtuMLaaY.js';
|
|
2
|
+
|
|
3
|
+
type GraphqlCodegenOptions = {
|
|
4
|
+
config?: string;
|
|
5
|
+
project?: string;
|
|
6
|
+
};
|
|
7
|
+
declare function graphqlCodegen(options?: GraphqlCodegenOptions): () => Promise<CodegenJob[]>;
|
|
8
|
+
|
|
9
|
+
export { type GraphqlCodegenOptions, graphqlCodegen };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import "../chunk-3RG5ZIWI.js";
|
|
2
|
+
|
|
3
|
+
// src/presets/graphql-codegen.ts
|
|
4
|
+
import { createContext, generate } from "@graphql-codegen/cli";
|
|
5
|
+
function graphqlCodegen(options = {}) {
|
|
6
|
+
return async () => {
|
|
7
|
+
const context = await createContext({
|
|
8
|
+
config: options.config ?? "",
|
|
9
|
+
project: options.project ?? "",
|
|
10
|
+
watch: false,
|
|
11
|
+
require: [],
|
|
12
|
+
overwrite: false,
|
|
13
|
+
silent: false,
|
|
14
|
+
errorsOnly: false,
|
|
15
|
+
profile: false,
|
|
16
|
+
verbose: true,
|
|
17
|
+
debug: true
|
|
18
|
+
});
|
|
19
|
+
const config = context.getConfig();
|
|
20
|
+
return Object.entries(config.generates).map(([output, options2]) => {
|
|
21
|
+
const input = [
|
|
22
|
+
...normalizeDocuments(config.documents),
|
|
23
|
+
...Array.isArray(options2) ? [] : normalizeDocuments(options2.documents)
|
|
24
|
+
];
|
|
25
|
+
return {
|
|
26
|
+
input,
|
|
27
|
+
output,
|
|
28
|
+
cacheBy: "contents",
|
|
29
|
+
generate: async (files) => {
|
|
30
|
+
const [generatedFile] = await generate(
|
|
31
|
+
{
|
|
32
|
+
...config,
|
|
33
|
+
documents: files.map((file) => file.localPath),
|
|
34
|
+
generates: {
|
|
35
|
+
[output]: {
|
|
36
|
+
...options2,
|
|
37
|
+
documents: void 0
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
false
|
|
42
|
+
);
|
|
43
|
+
return generatedFile.content;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function normalizeDocuments(documents) {
|
|
50
|
+
if (documents === void 0) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
if (!Array.isArray(documents)) {
|
|
54
|
+
documents = [documents];
|
|
55
|
+
}
|
|
56
|
+
for (const document of documents) {
|
|
57
|
+
if (typeof document !== "string") {
|
|
58
|
+
throw new Error("Custom loaders are not supported");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return documents;
|
|
62
|
+
}
|
|
63
|
+
export {
|
|
64
|
+
graphqlCodegen
|
|
65
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare class File {
|
|
2
|
+
#private;
|
|
3
|
+
readonly fullPath: string;
|
|
4
|
+
readonly localPath: string;
|
|
5
|
+
constructor(path: string, rootDir: string);
|
|
6
|
+
buffer(): Promise<Buffer>;
|
|
7
|
+
text(encoding?: BufferEncoding): Promise<string>;
|
|
8
|
+
json(): Promise<any>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
12
|
+
type CodegenOptions = {
|
|
13
|
+
rootDir?: string;
|
|
14
|
+
cacheFile?: string | false;
|
|
15
|
+
prettier?: boolean;
|
|
16
|
+
eslint?: boolean;
|
|
17
|
+
};
|
|
18
|
+
type NormalizedCodegenOptions = {
|
|
19
|
+
rootDir: string;
|
|
20
|
+
cacheFile?: string;
|
|
21
|
+
prettier: boolean;
|
|
22
|
+
eslint: boolean;
|
|
23
|
+
};
|
|
24
|
+
type CodegenJob = {
|
|
25
|
+
input: string | string[];
|
|
26
|
+
output: string;
|
|
27
|
+
generate: (files: File[], signal: AbortSignal) => MaybePromise<Buffer | string | null>;
|
|
28
|
+
cacheBy?: 'files' | 'contents';
|
|
29
|
+
prettier?: boolean;
|
|
30
|
+
eslint?: boolean;
|
|
31
|
+
};
|
|
32
|
+
type CodegenJobGenerator = (rootDir: string) => MaybePromise<CodegenJob[] | CodegenJob>;
|
|
33
|
+
type CodegenInput = CodegenJobGenerator | CodegenJob;
|
|
34
|
+
type CodegenPresetOptions = Omit<CodegenJob, 'input' | 'output' | 'generate'>;
|
|
35
|
+
declare function defineCodegen(job: CodegenInput, ...jobs: CodegenInput[]): [CodegenInput, ...CodegenInput[]];
|
|
36
|
+
declare function defineCodegen(options: CodegenOptions, ...jobs: CodegenInput[]): [CodegenOptions, ...CodegenInput[]];
|
|
37
|
+
declare function defineCodegen(optionsOrJob: CodegenOptions | CodegenInput, ...jobs: CodegenInput[]): [CodegenOptions | CodegenInput, ...CodegenInput[]];
|
|
38
|
+
|
|
39
|
+
export { type CodegenJob as C, File as F, type NormalizedCodegenOptions as N, type CodegenInput as a, type CodegenOptions as b, type CodegenJobGenerator as c, type CodegenPresetOptions as d, defineCodegen as e };
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pluggable-codegen",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"contributors": [
|
|
6
|
+
{
|
|
7
|
+
"name": "Dan Kadera",
|
|
8
|
+
"email": "me@subsonic.cz",
|
|
9
|
+
"url": "https://github.com/jahudka"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"bin": {
|
|
16
|
+
"pluggable-codegen": "./dist/cli.js"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"main": "./dist/index.cjs",
|
|
21
|
+
"module": "./dist/index.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"require": "./dist/index.cjs",
|
|
26
|
+
"import": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./presets/*": {
|
|
29
|
+
"types": "./dist/presets/*.d.ts",
|
|
30
|
+
"require": "./dist/presets/*.cjs",
|
|
31
|
+
"import": "./dist/presets/*.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"picomatch": "^4.0.2",
|
|
36
|
+
"tinyglobby": "^0.2.13"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@graphql-codegen/cli": "^5.0.0",
|
|
40
|
+
"yaml": "^2.7.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"@graphql-codegen/cli": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"yaml": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@eslint/compat": "^1.2.8",
|
|
52
|
+
"@eslint/js": "^9.25.1",
|
|
53
|
+
"@graphql-codegen/cli": "^5.0.5",
|
|
54
|
+
"@types/node": "^22.15.3",
|
|
55
|
+
"@types/picomatch": "^4.0.0",
|
|
56
|
+
"eslint": "^9.25.1",
|
|
57
|
+
"eslint-config-prettier": "^10.1.2",
|
|
58
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
59
|
+
"eslint-plugin-unused-imports": "^4.1.4",
|
|
60
|
+
"globals": "^16.0.0",
|
|
61
|
+
"prettier": "^3.5.3",
|
|
62
|
+
"rollup": "^4.40.1",
|
|
63
|
+
"tsup": "^8.4.0",
|
|
64
|
+
"typescript": "^5.8.3",
|
|
65
|
+
"typescript-eslint": "^8.31.0",
|
|
66
|
+
"vite": "^6.3.3",
|
|
67
|
+
"yaml": "^2.7.1"
|
|
68
|
+
}
|
|
69
|
+
}
|