pluggable-codegen 0.2.3 → 0.3.1
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/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/_virtual/_rolldown/runtime.mjs +7 -0
- package/dist/cache.cjs +77 -0
- package/dist/cache.mjs +76 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +42 -0
- package/dist/codegen.cjs +96 -0
- package/dist/codegen.d.mts +14 -0
- package/dist/codegen.mjs +94 -0
- package/dist/file.cjs +26 -0
- package/dist/file.d.mts +12 -0
- package/dist/file.mjs +25 -0
- package/dist/index.cjs +10 -329
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +6 -0
- package/dist/options.cjs +51 -0
- package/dist/options.mjs +49 -0
- package/dist/plugin.cjs +18 -0
- package/dist/plugin.d.mts +13 -0
- package/dist/plugin.mjs +18 -0
- package/dist/presets/graphql-codegen.cjs +43 -58
- package/dist/presets/graphql-codegen.d.mts +10 -0
- package/dist/presets/graphql-codegen.mjs +46 -0
- package/dist/types.cjs +8 -0
- package/dist/types.d.mts +32 -0
- package/dist/types.mjs +7 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.mjs +29 -0
- package/package.json +33 -25
- package/dist/chunk-3RG5ZIWI.js +0 -10
- package/dist/chunk-CBSI3RRL.js +0 -306
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +0 -43
- package/dist/index.d.ts +0 -23
- package/dist/index.js +0 -32
- package/dist/presets/graphql-codegen.d.ts +0 -9
- package/dist/presets/graphql-codegen.js +0 -65
- package/dist/types-CtuMLaaY.d.ts +0 -39
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { File } from "./file.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
5
|
+
type CodegenOptions = {
|
|
6
|
+
rootDir?: string;
|
|
7
|
+
cacheFile?: string | false;
|
|
8
|
+
prettier?: boolean;
|
|
9
|
+
eslint?: boolean;
|
|
10
|
+
};
|
|
11
|
+
type NormalizedCodegenOptions = {
|
|
12
|
+
rootDir: string;
|
|
13
|
+
cacheFile?: string;
|
|
14
|
+
prettier: boolean;
|
|
15
|
+
eslint: boolean;
|
|
16
|
+
};
|
|
17
|
+
type CodegenJob = {
|
|
18
|
+
input: string | string[];
|
|
19
|
+
output: string;
|
|
20
|
+
generate: (files: File[], signal: AbortSignal) => MaybePromise<Buffer | string | null>;
|
|
21
|
+
cacheBy?: 'files' | 'contents';
|
|
22
|
+
prettier?: boolean;
|
|
23
|
+
eslint?: boolean;
|
|
24
|
+
};
|
|
25
|
+
type CodegenJobGenerator = (rootDir: string) => MaybePromise<CodegenJob[] | CodegenJob>;
|
|
26
|
+
type CodegenInput = CodegenJobGenerator | CodegenJob;
|
|
27
|
+
type CodegenPresetOptions = Omit<CodegenJob, 'input' | 'output' | 'generate'>;
|
|
28
|
+
declare function defineCodegen(job: CodegenInput, ...jobs: CodegenInput[]): [CodegenInput, ...CodegenInput[]];
|
|
29
|
+
declare function defineCodegen(options: CodegenOptions, ...jobs: CodegenInput[]): [CodegenOptions, ...CodegenInput[]];
|
|
30
|
+
declare function defineCodegen(optionsOrJob: CodegenOptions | CodegenInput, ...jobs: CodegenInput[]): [CodegenOptions | CodegenInput, ...CodegenInput[]];
|
|
31
|
+
//#endregion
|
|
32
|
+
export { CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, NormalizedCodegenOptions, defineCodegen };
|
package/dist/types.mjs
ADDED
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
let node_crypto = require("node:crypto");
|
|
4
|
+
|
|
5
|
+
//#region src/utils.ts
|
|
6
|
+
async function lookupPath(dir, candidates, check) {
|
|
7
|
+
if (!Array.isArray(candidates)) candidates = [candidates];
|
|
8
|
+
do
|
|
9
|
+
for (const candidate of candidates) {
|
|
10
|
+
const path = (0, node_path.resolve)(dir, candidate);
|
|
11
|
+
try {
|
|
12
|
+
return await check(path);
|
|
13
|
+
} catch {}
|
|
14
|
+
}
|
|
15
|
+
while (cdup());
|
|
16
|
+
return;
|
|
17
|
+
function cdup() {
|
|
18
|
+
const orig = dir;
|
|
19
|
+
dir = (0, node_path.dirname)(dir);
|
|
20
|
+
return dir !== orig;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function hash(data) {
|
|
24
|
+
const hash = (0, node_crypto.createHash)("sha256");
|
|
25
|
+
hash.update(data);
|
|
26
|
+
return hash.digest("hex");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
exports.hash = hash;
|
|
31
|
+
exports.lookupPath = lookupPath;
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
//#region src/utils.ts
|
|
5
|
+
async function lookupPath(dir, candidates, check) {
|
|
6
|
+
if (!Array.isArray(candidates)) candidates = [candidates];
|
|
7
|
+
do
|
|
8
|
+
for (const candidate of candidates) {
|
|
9
|
+
const path = resolve(dir, candidate);
|
|
10
|
+
try {
|
|
11
|
+
return await check(path);
|
|
12
|
+
} catch {}
|
|
13
|
+
}
|
|
14
|
+
while (cdup());
|
|
15
|
+
return;
|
|
16
|
+
function cdup() {
|
|
17
|
+
const orig = dir;
|
|
18
|
+
dir = dirname(dir);
|
|
19
|
+
return dir !== orig;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function hash(data) {
|
|
23
|
+
const hash = createHash("sha256");
|
|
24
|
+
hash.update(data);
|
|
25
|
+
return hash.digest("hex");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { hash, lookupPath };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pluggable-codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://github.com/jahudka/pluggable-codegen",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/jahudka/pluggable-codegen.git"
|
|
8
|
+
"url": "git+https://github.com/jahudka/pluggable-codegen.git"
|
|
9
9
|
},
|
|
10
10
|
"bugs": {
|
|
11
11
|
"url": "https://github.com/jahudka/pluggable-codegen/issues"
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"bin": {
|
|
24
|
-
"pluggable-codegen": "dist/cli.
|
|
24
|
+
"pluggable-codegen": "dist/cli.mjs"
|
|
25
25
|
},
|
|
26
26
|
"type": "module",
|
|
27
27
|
"types": "./dist/index.d.ts",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"picomatch": "^4.0.
|
|
44
|
-
"tinyglobby": "^0.2.
|
|
43
|
+
"picomatch": "^4.0.4",
|
|
44
|
+
"tinyglobby": "^0.2.16"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"@graphql-codegen/cli": "^5.0.0",
|
|
48
|
-
"yaml": "^2.
|
|
47
|
+
"@graphql-codegen/cli": "^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
48
|
+
"yaml": "^2.9.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependenciesMeta": {
|
|
51
51
|
"@graphql-codegen/cli": {
|
|
@@ -56,22 +56,30 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@eslint/compat": "^1.
|
|
60
|
-
"@eslint/js": "^
|
|
61
|
-
"@graphql-codegen/cli": "^
|
|
62
|
-
"@types/node": "^22.
|
|
63
|
-
"@types/picomatch": "^4.0.
|
|
64
|
-
"eslint": "^
|
|
65
|
-
"eslint-config-prettier": "^10.1.
|
|
66
|
-
"eslint-plugin-simple-import-sort": "^
|
|
67
|
-
"eslint-plugin-unused-imports": "^4.1
|
|
68
|
-
"globals": "^
|
|
69
|
-
"prettier": "^3.
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"typescript": "^
|
|
73
|
-
"
|
|
74
|
-
|
|
75
|
-
|
|
59
|
+
"@eslint/compat": "^2.1.0",
|
|
60
|
+
"@eslint/js": "^10.0.1",
|
|
61
|
+
"@graphql-codegen/cli": "^7.0.0",
|
|
62
|
+
"@types/node": "^22.19.19",
|
|
63
|
+
"@types/picomatch": "^4.0.3",
|
|
64
|
+
"eslint": "^10.4.0",
|
|
65
|
+
"eslint-config-prettier": "^10.1.8",
|
|
66
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
67
|
+
"eslint-plugin-unused-imports": "^4.4.1",
|
|
68
|
+
"globals": "^17.6.0",
|
|
69
|
+
"prettier": "^3.8.3",
|
|
70
|
+
"tsdown": "^0.22.0",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"typescript-eslint": "^8.59.3",
|
|
73
|
+
"yaml": "^2.9.0"
|
|
74
|
+
},
|
|
75
|
+
"scripts": {
|
|
76
|
+
"clean": "rm -rf dist",
|
|
77
|
+
"eslint": "eslint .",
|
|
78
|
+
"eslint:fix": "eslint --fix .",
|
|
79
|
+
"prettier": "prettier --check .",
|
|
80
|
+
"prettier:fix": "prettier --write .",
|
|
81
|
+
"lint": "pnpm run '/^(eslint|prettier)$/'",
|
|
82
|
+
"format": "pnpm run --sequential '/^(eslint|prettier):fix$/'",
|
|
83
|
+
"build": "tsc && tsdown"
|
|
76
84
|
}
|
|
77
|
-
}
|
|
85
|
+
}
|
package/dist/chunk-3RG5ZIWI.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
};
|
package/dist/chunk-CBSI3RRL.js
DELETED
|
@@ -1,306 +0,0 @@
|
|
|
1
|
-
// src/utils.ts
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import { dirname, resolve } from "node:path";
|
|
4
|
-
async function lookupPath(dir, candidates, check) {
|
|
5
|
-
if (!Array.isArray(candidates)) {
|
|
6
|
-
candidates = [candidates];
|
|
7
|
-
}
|
|
8
|
-
do {
|
|
9
|
-
for (const candidate of candidates) {
|
|
10
|
-
const path = resolve(dir, candidate);
|
|
11
|
-
try {
|
|
12
|
-
return await check(path);
|
|
13
|
-
} catch {
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
} while (cdup());
|
|
17
|
-
return void 0;
|
|
18
|
-
function cdup() {
|
|
19
|
-
const orig = dir;
|
|
20
|
-
dir = dirname(dir);
|
|
21
|
-
return dir !== orig;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function hash(data) {
|
|
25
|
-
const hash2 = createHash("sha256");
|
|
26
|
-
hash2.update(data);
|
|
27
|
-
return hash2.digest("hex");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// src/file.ts
|
|
31
|
-
import { readFile } from "node:fs/promises";
|
|
32
|
-
import { relative } from "node:path";
|
|
33
|
-
var File = class {
|
|
34
|
-
fullPath;
|
|
35
|
-
localPath;
|
|
36
|
-
#contents;
|
|
37
|
-
constructor(path, rootDir) {
|
|
38
|
-
this.fullPath = path;
|
|
39
|
-
this.localPath = relative(rootDir, path).replace(/^\.(?:\/|$)/, "");
|
|
40
|
-
}
|
|
41
|
-
async buffer() {
|
|
42
|
-
return this.#contents ??= readFile(this.fullPath);
|
|
43
|
-
}
|
|
44
|
-
async text(encoding = "utf-8") {
|
|
45
|
-
const contents = await this.buffer();
|
|
46
|
-
return contents.toString(encoding);
|
|
47
|
-
}
|
|
48
|
-
async json() {
|
|
49
|
-
return JSON.parse(await this.text());
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/codegen.ts
|
|
54
|
-
import { mkdir as mkdir2, writeFile as writeFile2 } from "node:fs/promises";
|
|
55
|
-
import { dirname as dirname3, resolve as resolve4 } from "node:path";
|
|
56
|
-
import picomatch from "picomatch";
|
|
57
|
-
import { escapePath, glob } from "tinyglobby";
|
|
58
|
-
|
|
59
|
-
// src/cache.ts
|
|
60
|
-
import { createHash as createHash2 } from "node:crypto";
|
|
61
|
-
import { readFile as readFile2, writeFile } from "node:fs/promises";
|
|
62
|
-
import { resolve as resolve2 } from "node:path";
|
|
63
|
-
var Cache = class {
|
|
64
|
-
#rootDir;
|
|
65
|
-
#cacheFile;
|
|
66
|
-
#cacheData;
|
|
67
|
-
#writeTmr;
|
|
68
|
-
constructor(rootDir, cacheFile) {
|
|
69
|
-
this.#rootDir = rootDir;
|
|
70
|
-
this.#cacheFile = cacheFile;
|
|
71
|
-
}
|
|
72
|
-
async check(files, output, mode, generate) {
|
|
73
|
-
if (!mode) {
|
|
74
|
-
await generate();
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const [cacheData, sourceHash, outputHash] = await Promise.all([
|
|
78
|
-
this.#cacheData ??= this.#readCacheData(),
|
|
79
|
-
this.#getSourceHash(files, mode),
|
|
80
|
-
this.#getOutputHash(resolve2(this.#rootDir, output))
|
|
81
|
-
]);
|
|
82
|
-
const entry = cacheData.get(output);
|
|
83
|
-
if (entry && entry.source === sourceHash && entry.output === outputHash) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const newHash = await generate();
|
|
87
|
-
if (newHash === void 0) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
cacheData.set(output, {
|
|
91
|
-
source: sourceHash,
|
|
92
|
-
output: newHash
|
|
93
|
-
});
|
|
94
|
-
clearTimeout(this.#writeTmr);
|
|
95
|
-
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
96
|
-
}
|
|
97
|
-
async #getSourceHash(files, mode) {
|
|
98
|
-
const hash2 = createHash2("sha256");
|
|
99
|
-
for (const file of files) {
|
|
100
|
-
hash2.update(mode === "files" ? file.localPath : await file.buffer());
|
|
101
|
-
}
|
|
102
|
-
return hash2.digest("hex");
|
|
103
|
-
}
|
|
104
|
-
async #getOutputHash(file) {
|
|
105
|
-
try {
|
|
106
|
-
return hash(await readFile2(file));
|
|
107
|
-
} catch {
|
|
108
|
-
return void 0;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
async #readCacheData() {
|
|
112
|
-
if (!this.#cacheFile) {
|
|
113
|
-
return /* @__PURE__ */ new Map();
|
|
114
|
-
}
|
|
115
|
-
try {
|
|
116
|
-
const rawData = JSON.parse(await readFile2(this.#cacheFile, "utf-8"));
|
|
117
|
-
const cacheData = /* @__PURE__ */ new Map();
|
|
118
|
-
if (!isObject(rawData)) {
|
|
119
|
-
return cacheData;
|
|
120
|
-
}
|
|
121
|
-
for (const [key, value] of Object.entries(rawData)) {
|
|
122
|
-
if (!isObject(value) || !isValidEntry(value)) {
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
cacheData.set(key, value);
|
|
126
|
-
}
|
|
127
|
-
return cacheData;
|
|
128
|
-
} catch {
|
|
129
|
-
return /* @__PURE__ */ new Map();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
async #writeCacheData(data) {
|
|
133
|
-
if (this.#cacheFile) {
|
|
134
|
-
await writeFile(this.#cacheFile, JSON.stringify(Object.fromEntries(data)));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
function isObject(v) {
|
|
139
|
-
return !!v && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
140
|
-
}
|
|
141
|
-
function isValidEntry(v) {
|
|
142
|
-
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// src/options.ts
|
|
146
|
-
import { mkdir, readFile as readFile3 } from "node:fs/promises";
|
|
147
|
-
import { dirname as dirname2, resolve as resolve3 } from "node:path";
|
|
148
|
-
async function normalizeOptions(options = {}) {
|
|
149
|
-
let packageJson;
|
|
150
|
-
const rootDir = resolve3(options.rootDir ?? process.cwd());
|
|
151
|
-
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
152
|
-
const prettier = options.prettier ?? hasDevDep(await getPackageJson(), "prettier");
|
|
153
|
-
const eslint = options.eslint ?? hasDevDep(await getPackageJson(), "eslint");
|
|
154
|
-
return { rootDir, cacheFile, prettier, eslint };
|
|
155
|
-
async function getPackageJson() {
|
|
156
|
-
return packageJson ??= readPackageJson(rootDir);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
async function normalizeJobs(rootDir, inputs) {
|
|
160
|
-
const jobs = await Promise.all(
|
|
161
|
-
inputs.map(async (input) => {
|
|
162
|
-
return typeof input === "function" ? input(rootDir) : input;
|
|
163
|
-
})
|
|
164
|
-
);
|
|
165
|
-
return jobs.flat();
|
|
166
|
-
}
|
|
167
|
-
async function resolveCacheFile(rootDir, cacheFile) {
|
|
168
|
-
if (cacheFile === false) {
|
|
169
|
-
return void 0;
|
|
170
|
-
} else if (cacheFile !== void 0) {
|
|
171
|
-
return resolve3(rootDir, cacheFile);
|
|
172
|
-
}
|
|
173
|
-
const codegenPath = typeof __dirname !== "undefined" ? __dirname : import.meta.dirname;
|
|
174
|
-
if (!codegenPath.includes("/node_modules/")) {
|
|
175
|
-
return void 0;
|
|
176
|
-
}
|
|
177
|
-
cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
|
|
178
|
-
try {
|
|
179
|
-
await mkdir(dirname2(cacheFile), { recursive: true, mode: 488 });
|
|
180
|
-
} catch {
|
|
181
|
-
}
|
|
182
|
-
return cacheFile;
|
|
183
|
-
}
|
|
184
|
-
function hasDevDep(packageJson, dep) {
|
|
185
|
-
return typeof packageJson === "object" && packageJson !== null && "devDependencies" in packageJson && typeof packageJson.devDependencies === "object" && packageJson.devDependencies !== null && dep in packageJson.devDependencies;
|
|
186
|
-
}
|
|
187
|
-
async function readPackageJson(dir) {
|
|
188
|
-
return lookupPath(dir, "package.json", async (path) => {
|
|
189
|
-
const content = await readFile3(path, "utf-8");
|
|
190
|
-
return JSON.parse(content);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// src/codegen.ts
|
|
195
|
-
var Codegen = class _Codegen {
|
|
196
|
-
#options;
|
|
197
|
-
#cache;
|
|
198
|
-
#jobs;
|
|
199
|
-
#running = /* @__PURE__ */ new Map();
|
|
200
|
-
#eslint;
|
|
201
|
-
#prettier;
|
|
202
|
-
static async init(jobOrOptions, ...moreJobs) {
|
|
203
|
-
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
204
|
-
const normalizedOptions = await normalizeOptions(options);
|
|
205
|
-
const normalizedJobs = await normalizeJobs(normalizedOptions.rootDir, jobs);
|
|
206
|
-
return new _Codegen(normalizedOptions, normalizedJobs);
|
|
207
|
-
}
|
|
208
|
-
constructor(options, jobs) {
|
|
209
|
-
this.#options = options;
|
|
210
|
-
this.#cache = new Cache(options.rootDir, options.cacheFile);
|
|
211
|
-
this.#jobs = this.#createJobMap(jobs);
|
|
212
|
-
}
|
|
213
|
-
async generateAll() {
|
|
214
|
-
await Promise.allSettled([...this.#jobs.values()].map(async (job) => this.#generate(job)));
|
|
215
|
-
}
|
|
216
|
-
async generateMatching(path) {
|
|
217
|
-
await Promise.allSettled(
|
|
218
|
-
[...this.#jobs].map(async ([match, job]) => {
|
|
219
|
-
if (match(path)) {
|
|
220
|
-
await this.#generate(job);
|
|
221
|
-
}
|
|
222
|
-
})
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
async #generate(job) {
|
|
226
|
-
this.#running.get(job)?.abort();
|
|
227
|
-
const controller = new AbortController();
|
|
228
|
-
this.#running.set(job, controller);
|
|
229
|
-
const files = await this.#scanFiles(job);
|
|
230
|
-
await this.#cache.check(files, job.output, job.cacheBy, async () => {
|
|
231
|
-
const file = resolve4(this.#options.rootDir, job.output);
|
|
232
|
-
let output = await job.generate(files, controller.signal);
|
|
233
|
-
if (output === null) {
|
|
234
|
-
return void 0;
|
|
235
|
-
}
|
|
236
|
-
if (controller.signal.aborted) {
|
|
237
|
-
return void 0;
|
|
238
|
-
}
|
|
239
|
-
if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) {
|
|
240
|
-
output = await this.#applyESLint(output, file);
|
|
241
|
-
}
|
|
242
|
-
if (controller.signal.aborted) {
|
|
243
|
-
return void 0;
|
|
244
|
-
}
|
|
245
|
-
if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) {
|
|
246
|
-
output = await this.#applyPrettier(output, file);
|
|
247
|
-
}
|
|
248
|
-
if (controller.signal.aborted) {
|
|
249
|
-
return void 0;
|
|
250
|
-
}
|
|
251
|
-
try {
|
|
252
|
-
await mkdir2(dirname3(file), { mode: 488, recursive: true });
|
|
253
|
-
} catch {
|
|
254
|
-
}
|
|
255
|
-
await writeFile2(file, output);
|
|
256
|
-
return hash(output);
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
async #scanFiles(job) {
|
|
260
|
-
const files = await glob(job.input, {
|
|
261
|
-
cwd: this.#options.rootDir,
|
|
262
|
-
absolute: true
|
|
263
|
-
});
|
|
264
|
-
return files.sort().map((path) => new File(path, this.#options.rootDir));
|
|
265
|
-
}
|
|
266
|
-
async #applyESLint(source, path) {
|
|
267
|
-
const eslint = await this.#loadESLint();
|
|
268
|
-
const [result] = await eslint.lintText(source, {
|
|
269
|
-
filePath: path
|
|
270
|
-
});
|
|
271
|
-
return result?.output ?? source;
|
|
272
|
-
}
|
|
273
|
-
async #applyPrettier(source, path) {
|
|
274
|
-
const prettier = await this.#loadPrettier();
|
|
275
|
-
const options = await prettier.resolveConfig(path);
|
|
276
|
-
return prettier.format(source, {
|
|
277
|
-
...options ?? {},
|
|
278
|
-
filepath: path
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
async #loadESLint() {
|
|
282
|
-
return this.#eslint ??= import("eslint").then(
|
|
283
|
-
({ ESLint }) => new ESLint({
|
|
284
|
-
fix: true
|
|
285
|
-
})
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
async #loadPrettier() {
|
|
289
|
-
return this.#prettier ??= import("prettier");
|
|
290
|
-
}
|
|
291
|
-
#createJobMap(jobs) {
|
|
292
|
-
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
293
|
-
}
|
|
294
|
-
#createMatcher(job) {
|
|
295
|
-
const root = escapePath(this.#options.rootDir);
|
|
296
|
-
return picomatch(
|
|
297
|
-
Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
export {
|
|
303
|
-
lookupPath,
|
|
304
|
-
File,
|
|
305
|
-
Codegen
|
|
306
|
-
};
|
package/dist/cli.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
package/dist/cli.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
Codegen,
|
|
4
|
-
lookupPath
|
|
5
|
-
} from "./chunk-CBSI3RRL.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.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Codegen,
|
|
3
|
-
File
|
|
4
|
-
} from "./chunk-CBSI3RRL.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
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
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 };
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
};
|