pluggable-codegen 0.2.3 → 0.3.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/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 +32 -24
- 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
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/dist/cache.cjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_utils = require('./utils.cjs');
|
|
3
|
+
let node_fs_promises = require("node:fs/promises");
|
|
4
|
+
let node_path = require("node:path");
|
|
5
|
+
let node_crypto = require("node:crypto");
|
|
6
|
+
|
|
7
|
+
//#region src/cache.ts
|
|
8
|
+
var Cache = class {
|
|
9
|
+
#rootDir;
|
|
10
|
+
#cacheFile;
|
|
11
|
+
#cacheData;
|
|
12
|
+
#writeTmr;
|
|
13
|
+
constructor(rootDir, cacheFile) {
|
|
14
|
+
this.#rootDir = rootDir;
|
|
15
|
+
this.#cacheFile = cacheFile;
|
|
16
|
+
}
|
|
17
|
+
async check(files, output, mode, generate) {
|
|
18
|
+
if (!mode) {
|
|
19
|
+
await generate();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const [cacheData, sourceHash, outputHash] = await Promise.all([
|
|
23
|
+
this.#cacheData ??= this.#readCacheData(),
|
|
24
|
+
this.#getSourceHash(files, mode),
|
|
25
|
+
this.#getOutputHash((0, node_path.resolve)(this.#rootDir, output))
|
|
26
|
+
]);
|
|
27
|
+
const entry = cacheData.get(output);
|
|
28
|
+
if (entry && entry.source === sourceHash && entry.output === outputHash) return;
|
|
29
|
+
const newHash = await generate();
|
|
30
|
+
if (newHash === void 0) return;
|
|
31
|
+
cacheData.set(output, {
|
|
32
|
+
source: sourceHash,
|
|
33
|
+
output: newHash
|
|
34
|
+
});
|
|
35
|
+
clearTimeout(this.#writeTmr);
|
|
36
|
+
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
37
|
+
}
|
|
38
|
+
async #getSourceHash(files, mode) {
|
|
39
|
+
const hash = (0, node_crypto.createHash)("sha256");
|
|
40
|
+
for (const file of files) hash.update(mode === "files" ? file.localPath : await file.buffer());
|
|
41
|
+
return hash.digest("hex");
|
|
42
|
+
}
|
|
43
|
+
async #getOutputHash(file) {
|
|
44
|
+
try {
|
|
45
|
+
return require_utils.hash(await (0, node_fs_promises.readFile)(file));
|
|
46
|
+
} catch {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async #readCacheData() {
|
|
51
|
+
if (!this.#cacheFile) return /* @__PURE__ */ new Map();
|
|
52
|
+
try {
|
|
53
|
+
const rawData = JSON.parse(await (0, node_fs_promises.readFile)(this.#cacheFile, "utf-8"));
|
|
54
|
+
const cacheData = /* @__PURE__ */ new Map();
|
|
55
|
+
if (!isObject(rawData)) return cacheData;
|
|
56
|
+
for (const [key, value] of Object.entries(rawData)) {
|
|
57
|
+
if (!isObject(value) || !isValidEntry(value)) continue;
|
|
58
|
+
cacheData.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
return cacheData;
|
|
61
|
+
} catch {
|
|
62
|
+
return /* @__PURE__ */ new Map();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async #writeCacheData(data) {
|
|
66
|
+
if (this.#cacheFile) await (0, node_fs_promises.writeFile)(this.#cacheFile, JSON.stringify(Object.fromEntries(data)));
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function isObject(v) {
|
|
70
|
+
return !!v && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
71
|
+
}
|
|
72
|
+
function isValidEntry(v) {
|
|
73
|
+
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
exports.Cache = Cache;
|
package/dist/cache.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { hash } from "./utils.mjs";
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
|
|
6
|
+
//#region src/cache.ts
|
|
7
|
+
var Cache = class {
|
|
8
|
+
#rootDir;
|
|
9
|
+
#cacheFile;
|
|
10
|
+
#cacheData;
|
|
11
|
+
#writeTmr;
|
|
12
|
+
constructor(rootDir, cacheFile) {
|
|
13
|
+
this.#rootDir = rootDir;
|
|
14
|
+
this.#cacheFile = cacheFile;
|
|
15
|
+
}
|
|
16
|
+
async check(files, output, mode, generate) {
|
|
17
|
+
if (!mode) {
|
|
18
|
+
await generate();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const [cacheData, sourceHash, outputHash] = await Promise.all([
|
|
22
|
+
this.#cacheData ??= this.#readCacheData(),
|
|
23
|
+
this.#getSourceHash(files, mode),
|
|
24
|
+
this.#getOutputHash(resolve(this.#rootDir, output))
|
|
25
|
+
]);
|
|
26
|
+
const entry = cacheData.get(output);
|
|
27
|
+
if (entry && entry.source === sourceHash && entry.output === outputHash) return;
|
|
28
|
+
const newHash = await generate();
|
|
29
|
+
if (newHash === void 0) return;
|
|
30
|
+
cacheData.set(output, {
|
|
31
|
+
source: sourceHash,
|
|
32
|
+
output: newHash
|
|
33
|
+
});
|
|
34
|
+
clearTimeout(this.#writeTmr);
|
|
35
|
+
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
36
|
+
}
|
|
37
|
+
async #getSourceHash(files, mode) {
|
|
38
|
+
const hash = createHash("sha256");
|
|
39
|
+
for (const file of files) hash.update(mode === "files" ? file.localPath : await file.buffer());
|
|
40
|
+
return hash.digest("hex");
|
|
41
|
+
}
|
|
42
|
+
async #getOutputHash(file) {
|
|
43
|
+
try {
|
|
44
|
+
return hash(await readFile(file));
|
|
45
|
+
} catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async #readCacheData() {
|
|
50
|
+
if (!this.#cacheFile) return /* @__PURE__ */ new Map();
|
|
51
|
+
try {
|
|
52
|
+
const rawData = JSON.parse(await readFile(this.#cacheFile, "utf-8"));
|
|
53
|
+
const cacheData = /* @__PURE__ */ new Map();
|
|
54
|
+
if (!isObject(rawData)) return cacheData;
|
|
55
|
+
for (const [key, value] of Object.entries(rawData)) {
|
|
56
|
+
if (!isObject(value) || !isValidEntry(value)) continue;
|
|
57
|
+
cacheData.set(key, value);
|
|
58
|
+
}
|
|
59
|
+
return cacheData;
|
|
60
|
+
} catch {
|
|
61
|
+
return /* @__PURE__ */ new Map();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async #writeCacheData(data) {
|
|
65
|
+
if (this.#cacheFile) await writeFile(this.#cacheFile, JSON.stringify(Object.fromEntries(data)));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function isObject(v) {
|
|
69
|
+
return !!v && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
70
|
+
}
|
|
71
|
+
function isValidEntry(v) {
|
|
72
|
+
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { Cache };
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { __require } from "./_virtual/_rolldown/runtime.mjs";
|
|
3
|
+
import { lookupPath } from "./utils.mjs";
|
|
4
|
+
import { Codegen } from "./codegen.mjs";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
//#region src/cli.ts
|
|
8
|
+
const configFiles = [
|
|
9
|
+
"codegen.config.mjs",
|
|
10
|
+
"codegen.config.cjs",
|
|
11
|
+
"codegen.config.js"
|
|
12
|
+
];
|
|
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
|
+
await (await Codegen.init(...config)).generateAll();
|
|
24
|
+
})();
|
|
25
|
+
async function importConfig(path) {
|
|
26
|
+
try {
|
|
27
|
+
return (await import(path)).default;
|
|
28
|
+
} catch (e) {
|
|
29
|
+
if (!path.endsWith(".mjs")) {
|
|
30
|
+
const config = __require(path);
|
|
31
|
+
return config.default ?? config;
|
|
32
|
+
}
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function validateConfig(config) {
|
|
37
|
+
if (!Array.isArray(config) || !config.length) return false;
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { };
|
package/dist/codegen.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_utils = require('./utils.cjs');
|
|
3
|
+
const require_cache = require('./cache.cjs');
|
|
4
|
+
const require_file = require('./file.cjs');
|
|
5
|
+
const require_options = require('./options.cjs');
|
|
6
|
+
let node_fs_promises = require("node:fs/promises");
|
|
7
|
+
let node_path = require("node:path");
|
|
8
|
+
let picomatch = require("picomatch");
|
|
9
|
+
picomatch = require_runtime.__toESM(picomatch, 1);
|
|
10
|
+
let tinyglobby = require("tinyglobby");
|
|
11
|
+
|
|
12
|
+
//#region src/codegen.ts
|
|
13
|
+
var Codegen = class Codegen {
|
|
14
|
+
#options;
|
|
15
|
+
#cache;
|
|
16
|
+
#jobs;
|
|
17
|
+
#running = /* @__PURE__ */ new Map();
|
|
18
|
+
#eslint;
|
|
19
|
+
#prettier;
|
|
20
|
+
static async init(jobOrOptions, ...moreJobs) {
|
|
21
|
+
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
22
|
+
const normalizedOptions = await require_options.normalizeOptions(options);
|
|
23
|
+
return new Codegen(normalizedOptions, await require_options.normalizeJobs(normalizedOptions.rootDir, jobs));
|
|
24
|
+
}
|
|
25
|
+
constructor(options, jobs) {
|
|
26
|
+
this.#options = options;
|
|
27
|
+
this.#cache = new require_cache.Cache(options.rootDir, options.cacheFile);
|
|
28
|
+
this.#jobs = this.#createJobMap(jobs);
|
|
29
|
+
}
|
|
30
|
+
async generateAll() {
|
|
31
|
+
await Promise.allSettled([...this.#jobs.values()].map(async (job) => this.#generate(job)));
|
|
32
|
+
}
|
|
33
|
+
async generateMatching(path) {
|
|
34
|
+
await Promise.allSettled([...this.#jobs].map(async ([match, job]) => {
|
|
35
|
+
if (match(path)) await this.#generate(job);
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
async #generate(job) {
|
|
39
|
+
this.#running.get(job)?.abort();
|
|
40
|
+
const controller = new AbortController();
|
|
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;
|
|
52
|
+
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);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async #scanFiles(job) {
|
|
63
|
+
return (await (0, tinyglobby.glob)(job.input, {
|
|
64
|
+
cwd: this.#options.rootDir,
|
|
65
|
+
absolute: true
|
|
66
|
+
})).sort().map((path) => new require_file.File(path, this.#options.rootDir));
|
|
67
|
+
}
|
|
68
|
+
async #applyESLint(source, path) {
|
|
69
|
+
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
70
|
+
return result?.output ?? source;
|
|
71
|
+
}
|
|
72
|
+
async #applyPrettier(source, path) {
|
|
73
|
+
const prettier = await this.#loadPrettier();
|
|
74
|
+
const options = await prettier.resolveConfig(path);
|
|
75
|
+
return prettier.format(source, {
|
|
76
|
+
...options ?? {},
|
|
77
|
+
filepath: path
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async #loadESLint() {
|
|
81
|
+
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
82
|
+
}
|
|
83
|
+
async #loadPrettier() {
|
|
84
|
+
return this.#prettier ??= import("prettier");
|
|
85
|
+
}
|
|
86
|
+
#createJobMap(jobs) {
|
|
87
|
+
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
88
|
+
}
|
|
89
|
+
#createMatcher(job) {
|
|
90
|
+
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(/^\//, "")}`);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.Codegen = Codegen;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CodegenInput, CodegenOptions } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/codegen.d.ts
|
|
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
|
+
//#endregion
|
|
14
|
+
export { Codegen };
|
package/dist/codegen.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { hash } from "./utils.mjs";
|
|
2
|
+
import { Cache } from "./cache.mjs";
|
|
3
|
+
import { File } from "./file.mjs";
|
|
4
|
+
import { normalizeJobs, normalizeOptions } from "./options.mjs";
|
|
5
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, resolve } from "node:path";
|
|
7
|
+
import picomatch from "picomatch";
|
|
8
|
+
import { escapePath, glob } from "tinyglobby";
|
|
9
|
+
|
|
10
|
+
//#region src/codegen.ts
|
|
11
|
+
var Codegen = class Codegen {
|
|
12
|
+
#options;
|
|
13
|
+
#cache;
|
|
14
|
+
#jobs;
|
|
15
|
+
#running = /* @__PURE__ */ new Map();
|
|
16
|
+
#eslint;
|
|
17
|
+
#prettier;
|
|
18
|
+
static async init(jobOrOptions, ...moreJobs) {
|
|
19
|
+
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
20
|
+
const normalizedOptions = await normalizeOptions(options);
|
|
21
|
+
return new Codegen(normalizedOptions, await normalizeJobs(normalizedOptions.rootDir, jobs));
|
|
22
|
+
}
|
|
23
|
+
constructor(options, jobs) {
|
|
24
|
+
this.#options = options;
|
|
25
|
+
this.#cache = new Cache(options.rootDir, options.cacheFile);
|
|
26
|
+
this.#jobs = this.#createJobMap(jobs);
|
|
27
|
+
}
|
|
28
|
+
async generateAll() {
|
|
29
|
+
await Promise.allSettled([...this.#jobs.values()].map(async (job) => this.#generate(job)));
|
|
30
|
+
}
|
|
31
|
+
async generateMatching(path) {
|
|
32
|
+
await Promise.allSettled([...this.#jobs].map(async ([match, job]) => {
|
|
33
|
+
if (match(path)) await this.#generate(job);
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
async #generate(job) {
|
|
37
|
+
this.#running.get(job)?.abort();
|
|
38
|
+
const controller = new AbortController();
|
|
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;
|
|
50
|
+
try {
|
|
51
|
+
await mkdir(dirname(file), {
|
|
52
|
+
mode: 488,
|
|
53
|
+
recursive: true
|
|
54
|
+
});
|
|
55
|
+
} catch {}
|
|
56
|
+
await writeFile(file, output);
|
|
57
|
+
return hash(output);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async #scanFiles(job) {
|
|
61
|
+
return (await glob(job.input, {
|
|
62
|
+
cwd: this.#options.rootDir,
|
|
63
|
+
absolute: true
|
|
64
|
+
})).sort().map((path) => new File(path, this.#options.rootDir));
|
|
65
|
+
}
|
|
66
|
+
async #applyESLint(source, path) {
|
|
67
|
+
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
68
|
+
return result?.output ?? source;
|
|
69
|
+
}
|
|
70
|
+
async #applyPrettier(source, path) {
|
|
71
|
+
const prettier = await this.#loadPrettier();
|
|
72
|
+
const options = await prettier.resolveConfig(path);
|
|
73
|
+
return prettier.format(source, {
|
|
74
|
+
...options ?? {},
|
|
75
|
+
filepath: path
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async #loadESLint() {
|
|
79
|
+
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
80
|
+
}
|
|
81
|
+
async #loadPrettier() {
|
|
82
|
+
return this.#prettier ??= import("prettier");
|
|
83
|
+
}
|
|
84
|
+
#createJobMap(jobs) {
|
|
85
|
+
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
86
|
+
}
|
|
87
|
+
#createMatcher(job) {
|
|
88
|
+
const root = escapePath(this.#options.rootDir);
|
|
89
|
+
return picomatch(Array.isArray(job.input) ? job.input.map((pattern) => `${root}/${pattern.replace(/^\//, "")}`) : `${root}/${job.input.replace(/^\//, "")}`);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { Codegen };
|
package/dist/file.cjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let node_fs_promises = require("node:fs/promises");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
|
|
5
|
+
//#region src/file.ts
|
|
6
|
+
var File = class {
|
|
7
|
+
fullPath;
|
|
8
|
+
localPath;
|
|
9
|
+
#contents;
|
|
10
|
+
constructor(path, rootDir) {
|
|
11
|
+
this.fullPath = path;
|
|
12
|
+
this.localPath = (0, node_path.relative)(rootDir, path).replace(/^\.(?:\/|$)/, "");
|
|
13
|
+
}
|
|
14
|
+
async buffer() {
|
|
15
|
+
return this.#contents ??= (0, node_fs_promises.readFile)(this.fullPath);
|
|
16
|
+
}
|
|
17
|
+
async text(encoding = "utf-8") {
|
|
18
|
+
return (await this.buffer()).toString(encoding);
|
|
19
|
+
}
|
|
20
|
+
async json() {
|
|
21
|
+
return JSON.parse(await this.text());
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.File = File;
|
package/dist/file.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/file.d.ts
|
|
2
|
+
declare class File {
|
|
3
|
+
#private;
|
|
4
|
+
readonly fullPath: string;
|
|
5
|
+
readonly localPath: string;
|
|
6
|
+
constructor(path: string, rootDir: string);
|
|
7
|
+
buffer(): Promise<Buffer>;
|
|
8
|
+
text(encoding?: BufferEncoding): Promise<string>;
|
|
9
|
+
json(): Promise<any>;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { File };
|
package/dist/file.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { relative } from "node:path";
|
|
3
|
+
|
|
4
|
+
//#region src/file.ts
|
|
5
|
+
var File = class {
|
|
6
|
+
fullPath;
|
|
7
|
+
localPath;
|
|
8
|
+
#contents;
|
|
9
|
+
constructor(path, rootDir) {
|
|
10
|
+
this.fullPath = path;
|
|
11
|
+
this.localPath = relative(rootDir, path).replace(/^\.(?:\/|$)/, "");
|
|
12
|
+
}
|
|
13
|
+
async buffer() {
|
|
14
|
+
return this.#contents ??= readFile(this.fullPath);
|
|
15
|
+
}
|
|
16
|
+
async text(encoding = "utf-8") {
|
|
17
|
+
return (await this.buffer()).toString(encoding);
|
|
18
|
+
}
|
|
19
|
+
async json() {
|
|
20
|
+
return JSON.parse(await this.text());
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { File };
|