pluggable-codegen 0.4.0 → 0.5.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/README.md +2 -2
- package/dist/_virtual/_rolldown/runtime.cjs +2 -4
- package/dist/_virtual/_rolldown/runtime.mjs +2 -3
- package/dist/cache.cjs +10 -7
- package/dist/cache.mjs +9 -5
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +6 -5
- package/dist/codegen.cjs +30 -54
- package/dist/codegen.d.cts +10 -0
- package/dist/codegen.d.mts +4 -8
- package/dist/codegen.mjs +25 -49
- package/dist/file.cjs +1 -3
- package/dist/file.d.cts +9 -0
- package/dist/file.d.mts +2 -5
- package/dist/file.mjs +1 -2
- package/dist/index.cjs +7 -5
- package/dist/index.d.cts +7 -0
- package/dist/index.d.mts +4 -2
- package/dist/index.mjs +2 -1
- package/dist/options.cjs +8 -8
- package/dist/options.mjs +7 -6
- package/dist/plugin.cjs +2 -3
- package/dist/plugin.d.cts +9 -0
- package/dist/plugin.d.mts +4 -8
- package/dist/plugin.mjs +1 -2
- package/dist/postprocessors/eslint.cjs +20 -0
- package/dist/postprocessors/eslint.d.cts +8 -0
- package/dist/postprocessors/eslint.d.mts +8 -0
- package/dist/postprocessors/eslint.mjs +20 -0
- package/dist/postprocessors/index.cjs +12 -0
- package/dist/postprocessors/index.d.cts +7 -0
- package/dist/postprocessors/index.d.mts +7 -0
- package/dist/postprocessors/index.mjs +12 -0
- package/dist/postprocessors/oxfmt.cjs +34 -0
- package/dist/postprocessors/oxfmt.d.cts +9 -0
- package/dist/postprocessors/oxfmt.d.mts +9 -0
- package/dist/postprocessors/oxfmt.mjs +34 -0
- package/dist/postprocessors/prettier.cjs +15 -0
- package/dist/postprocessors/prettier.d.cts +7 -0
- package/dist/postprocessors/prettier.d.mts +7 -0
- package/dist/postprocessors/prettier.mjs +15 -0
- package/dist/postprocessors/types.d.cts +9 -0
- package/dist/postprocessors/types.d.mts +9 -0
- package/dist/presets/graphql-codegen.cjs +2 -4
- package/dist/presets/graphql-codegen.d.cts +6 -0
- package/dist/presets/graphql-codegen.d.mts +2 -6
- package/dist/presets/graphql-codegen.mjs +2 -3
- package/dist/types.cjs +1 -3
- package/dist/types.d.cts +24 -0
- package/dist/types.d.mts +11 -23
- package/dist/types.mjs +1 -2
- package/dist/utils.cjs +1 -3
- package/dist/utils.mjs +1 -2
- package/package.json +53 -42
- package/dist/oxfmt.cjs +0 -24
- package/dist/oxfmt.mjs +0 -23
package/README.md
CHANGED
|
@@ -82,9 +82,9 @@ Within a bundler config, such as `vite.config.ts`, you would just use
|
|
|
82
82
|
the `codegen()` plugin instead of the `defineCodegen()` typing helper:
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
|
+
import { codegen } from 'pluggable-codegen';
|
|
85
86
|
// vite.config.ts
|
|
86
87
|
import { defineConfig } from 'vite';
|
|
87
|
-
import { codegen } from 'pluggable-codegen';
|
|
88
88
|
|
|
89
89
|
export default defineConfig({
|
|
90
90
|
plugins: [
|
|
@@ -100,8 +100,8 @@ and before `svelte-check`:
|
|
|
100
100
|
```typescript
|
|
101
101
|
// vite.config.ts
|
|
102
102
|
|
|
103
|
-
import { defineConfig } from 'vite';
|
|
104
103
|
import { codegen } from 'pluggable-codegen';
|
|
104
|
+
import { defineConfig } from 'vite';
|
|
105
105
|
import codegenConfig from './codegen.config';
|
|
106
106
|
|
|
107
107
|
export default defineConfig({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -19,11 +19,9 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
19
|
}
|
|
20
20
|
return to;
|
|
21
21
|
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
23
23
|
value: mod,
|
|
24
24
|
enumerable: true
|
|
25
25
|
}) : target, mod));
|
|
26
26
|
|
|
27
|
-
//#endregion
|
|
28
|
-
|
|
29
27
|
exports.__toESM = __toESM;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var __require = /*
|
|
3
|
+
// \0rolldown/runtime.js
|
|
4
|
+
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
5
5
|
|
|
6
|
-
//#endregion
|
|
7
6
|
export { __require };
|
package/dist/cache.cjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
const
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
1
|
+
const require_utils = require("./utils.cjs");
|
|
3
2
|
let node_fs_promises = require("node:fs/promises");
|
|
4
3
|
let node_path = require("node:path");
|
|
5
4
|
let node_crypto = require("node:crypto");
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
// src/cache.ts
|
|
8
7
|
var Cache = class {
|
|
9
8
|
#rootDir;
|
|
10
9
|
#cacheFile;
|
|
11
10
|
#cacheData;
|
|
12
|
-
#
|
|
11
|
+
#dirty = false;
|
|
13
12
|
constructor(rootDir, cacheFile) {
|
|
14
13
|
this.#rootDir = rootDir;
|
|
15
14
|
this.#cacheFile = cacheFile;
|
|
@@ -32,8 +31,13 @@ var Cache = class {
|
|
|
32
31
|
source: sourceHash,
|
|
33
32
|
output: newHash
|
|
34
33
|
});
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
this.#dirty = true;
|
|
35
|
+
}
|
|
36
|
+
async flush() {
|
|
37
|
+
if (this.#dirty) {
|
|
38
|
+
this.#dirty = false;
|
|
39
|
+
await this.#writeCacheData(await (this.#cacheData ??= this.#readCacheData()));
|
|
40
|
+
}
|
|
37
41
|
}
|
|
38
42
|
async #getSourceHash(files, mode) {
|
|
39
43
|
const hash = (0, node_crypto.createHash)("sha256");
|
|
@@ -73,5 +77,4 @@ function isValidEntry(v) {
|
|
|
73
77
|
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
74
78
|
}
|
|
75
79
|
|
|
76
|
-
//#endregion
|
|
77
80
|
exports.Cache = Cache;
|
package/dist/cache.mjs
CHANGED
|
@@ -3,12 +3,12 @@ import { readFile, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// src/cache.ts
|
|
7
7
|
var Cache = class {
|
|
8
8
|
#rootDir;
|
|
9
9
|
#cacheFile;
|
|
10
10
|
#cacheData;
|
|
11
|
-
#
|
|
11
|
+
#dirty = false;
|
|
12
12
|
constructor(rootDir, cacheFile) {
|
|
13
13
|
this.#rootDir = rootDir;
|
|
14
14
|
this.#cacheFile = cacheFile;
|
|
@@ -31,8 +31,13 @@ var Cache = class {
|
|
|
31
31
|
source: sourceHash,
|
|
32
32
|
output: newHash
|
|
33
33
|
});
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
this.#dirty = true;
|
|
35
|
+
}
|
|
36
|
+
async flush() {
|
|
37
|
+
if (this.#dirty) {
|
|
38
|
+
this.#dirty = false;
|
|
39
|
+
await this.#writeCacheData(await (this.#cacheData ??= this.#readCacheData()));
|
|
40
|
+
}
|
|
36
41
|
}
|
|
37
42
|
async #getSourceHash(files, mode) {
|
|
38
43
|
const hash = createHash("sha256");
|
|
@@ -72,5 +77,4 @@ function isValidEntry(v) {
|
|
|
72
77
|
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
//#endregion
|
|
76
80
|
export { Cache };
|
package/dist/cli.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {}
|
package/dist/cli.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { lookupPath } from "./utils.mjs";
|
|
|
4
4
|
import { Codegen } from "./codegen.mjs";
|
|
5
5
|
import { resolve } from "node:path";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// src/cli.ts
|
|
8
8
|
const configFiles = [
|
|
9
9
|
"codegen.config.mjs",
|
|
10
10
|
"codegen.config.cjs",
|
|
@@ -14,13 +14,15 @@ const configFiles = [
|
|
|
14
14
|
const config = process.argv.length > 2 ? await importConfig(resolve(process.argv[2])) : await lookupPath(process.cwd(), configFiles, importConfig);
|
|
15
15
|
if (!config) {
|
|
16
16
|
console.log(`Usage: pluggable-codegen [config]`);
|
|
17
|
-
process.
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
return;
|
|
18
19
|
}
|
|
19
20
|
if (!validateConfig(config)) {
|
|
20
21
|
console.log(`Invalid config`);
|
|
21
|
-
process.
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
return;
|
|
22
24
|
}
|
|
23
|
-
await (await Codegen.init(...config)).generateAll();
|
|
25
|
+
if (!await (await Codegen.init(...config)).generateAll()) process.exitCode = 1;
|
|
24
26
|
})();
|
|
25
27
|
async function importConfig(path) {
|
|
26
28
|
try {
|
|
@@ -38,5 +40,4 @@ function validateConfig(config) {
|
|
|
38
40
|
return true;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
//#endregion
|
|
42
43
|
export { };
|
package/dist/codegen.cjs
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
const require_runtime = require(
|
|
2
|
-
const require_utils = require(
|
|
3
|
-
const require_cache = require(
|
|
4
|
-
const require_file = require(
|
|
5
|
-
const
|
|
6
|
-
const
|
|
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_index = require("./postprocessors/index.cjs");
|
|
6
|
+
const require_options = require("./options.cjs");
|
|
7
7
|
let node_fs_promises = require("node:fs/promises");
|
|
8
8
|
let node_path = require("node:path");
|
|
9
9
|
let picomatch = require("picomatch");
|
|
10
10
|
picomatch = require_runtime.__toESM(picomatch, 1);
|
|
11
11
|
let tinyglobby = require("tinyglobby");
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// src/codegen.ts
|
|
14
14
|
var Codegen = class Codegen {
|
|
15
15
|
#options;
|
|
16
16
|
#cache;
|
|
17
17
|
#jobs;
|
|
18
18
|
#running = /* @__PURE__ */ new Map();
|
|
19
|
-
#oxfmt;
|
|
20
|
-
#eslint;
|
|
21
|
-
#prettier;
|
|
22
19
|
static async init(jobOrOptions, ...moreJobs) {
|
|
23
20
|
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
24
21
|
const normalizedOptions = await require_options.normalizeOptions(options);
|
|
25
|
-
|
|
22
|
+
const normalizedJobs = await require_options.normalizeJobs(normalizedOptions.rootDir, jobs);
|
|
23
|
+
return new Codegen(normalizedOptions, normalizedJobs);
|
|
26
24
|
}
|
|
27
25
|
constructor(options, jobs) {
|
|
28
26
|
this.#options = options;
|
|
@@ -30,33 +28,38 @@ var Codegen = class Codegen {
|
|
|
30
28
|
this.#jobs = this.#createJobMap(jobs);
|
|
31
29
|
}
|
|
32
30
|
async generateAll() {
|
|
33
|
-
await Promise.
|
|
31
|
+
const results = await Promise.all([...this.#jobs.values()].map(this.#generate));
|
|
32
|
+
await this.#cache.flush();
|
|
33
|
+
return results.every((v) => v);
|
|
34
34
|
}
|
|
35
35
|
async generateMatching(path) {
|
|
36
|
-
await Promise.
|
|
36
|
+
const results = await Promise.all([...this.#jobs].map(async ([match, job]) => {
|
|
37
37
|
if (match(path)) await this.#generate(job);
|
|
38
38
|
}));
|
|
39
|
+
await this.#cache.flush();
|
|
40
|
+
return results.every((v) => v);
|
|
39
41
|
}
|
|
40
|
-
async
|
|
42
|
+
#generate = async (job) => {
|
|
41
43
|
this.#running.get(job)?.abort();
|
|
42
44
|
const controller = new AbortController();
|
|
43
45
|
this.#running.set(job, controller);
|
|
44
46
|
const files = await this.#scanFiles(job.input);
|
|
45
47
|
const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
|
|
48
|
+
let success = true;
|
|
46
49
|
await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
|
|
47
50
|
try {
|
|
48
51
|
const file = (0, node_path.resolve)(this.#options.rootDir, job.output);
|
|
49
52
|
let output = await job.generate(files, controller.signal);
|
|
50
53
|
if (output === null) return;
|
|
51
54
|
if (controller.signal.aborted) return;
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
const disabled = /* @__PURE__ */ new Set();
|
|
56
|
+
for (const postprocessor of require_index.postprocessors) {
|
|
57
|
+
if (typeof output !== "string") break;
|
|
58
|
+
if (!(job[postprocessor.id] ?? this.#options[postprocessor.id]) || disabled.has(postprocessor.id)) continue;
|
|
59
|
+
for (const id of postprocessor.conflicts) disabled.add(id);
|
|
60
|
+
output = await postprocessor.apply(output, file);
|
|
61
|
+
if (controller.signal.aborted) return;
|
|
62
|
+
}
|
|
60
63
|
if (controller.signal.aborted) return;
|
|
61
64
|
try {
|
|
62
65
|
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
|
|
@@ -70,53 +73,26 @@ var Codegen = class Codegen {
|
|
|
70
73
|
console.log(`\nError while generating '${job.output}':`);
|
|
71
74
|
console.log(e);
|
|
72
75
|
console.log("");
|
|
76
|
+
success = false;
|
|
73
77
|
return;
|
|
74
78
|
}
|
|
75
79
|
});
|
|
76
|
-
|
|
80
|
+
return success;
|
|
81
|
+
};
|
|
77
82
|
async #scanFiles(patterns) {
|
|
78
83
|
return (await (0, tinyglobby.glob)(patterns, {
|
|
79
84
|
cwd: this.#options.rootDir,
|
|
80
85
|
absolute: true
|
|
81
86
|
})).sort().map((path) => new require_file.File(path, this.#options.rootDir));
|
|
82
87
|
}
|
|
83
|
-
async #applyOxfmt(source, path) {
|
|
84
|
-
const oxfmt = await this.#loadOxfmt();
|
|
85
|
-
const config = await require_oxfmt.resolveOxfmtConfig(path);
|
|
86
|
-
const { code, errors } = await oxfmt.format(path, source, config);
|
|
87
|
-
const firstError = errors.filter((e) => e.severity === "Error").pop();
|
|
88
|
-
if (firstError) throw new Error(firstError.message);
|
|
89
|
-
return code;
|
|
90
|
-
}
|
|
91
|
-
async #applyESLint(source, path) {
|
|
92
|
-
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
93
|
-
return result?.output ?? source;
|
|
94
|
-
}
|
|
95
|
-
async #applyPrettier(source, path) {
|
|
96
|
-
const prettier = await this.#loadPrettier();
|
|
97
|
-
const options = await prettier.resolveConfig(path);
|
|
98
|
-
return prettier.format(source, {
|
|
99
|
-
...options ?? {},
|
|
100
|
-
filepath: path
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
async #loadOxfmt() {
|
|
104
|
-
return this.#oxfmt ??= import("oxfmt");
|
|
105
|
-
}
|
|
106
|
-
async #loadESLint() {
|
|
107
|
-
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
108
|
-
}
|
|
109
|
-
async #loadPrettier() {
|
|
110
|
-
return this.#prettier ??= import("prettier");
|
|
111
|
-
}
|
|
112
88
|
#createJobMap(jobs) {
|
|
113
89
|
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
114
90
|
}
|
|
115
91
|
#createMatcher(job) {
|
|
116
92
|
const root = (0, tinyglobby.escapePath)(this.#options.rootDir);
|
|
117
|
-
|
|
93
|
+
const patterns = [job.input, job.extraDeps ?? []];
|
|
94
|
+
return (0, picomatch.default)(patterns.flat().map((pattern) => (0, node_path.resolve)(root, pattern)));
|
|
118
95
|
}
|
|
119
96
|
};
|
|
120
97
|
|
|
121
|
-
//#endregion
|
|
122
98
|
exports.Codegen = Codegen;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CodegenInput, CodegenOptions } from "./types.cjs";
|
|
2
|
+
export declare class Codegen {
|
|
3
|
+
#private;
|
|
4
|
+
static init(...jobs: CodegenInput[]): Promise<Codegen>;
|
|
5
|
+
static init(options: CodegenOptions, ...jobs: CodegenInput[]): Promise<Codegen>;
|
|
6
|
+
static init(jobOrOptions: CodegenInput | CodegenOptions, ...moreJobs: CodegenInput[]): Promise<Codegen>;
|
|
7
|
+
private constructor();
|
|
8
|
+
generateAll(): Promise<boolean>;
|
|
9
|
+
generateMatching(path: string): Promise<boolean>;
|
|
10
|
+
}
|
package/dist/codegen.d.mts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { CodegenInput, CodegenOptions } from "./types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/codegen.d.ts
|
|
4
|
-
declare class Codegen {
|
|
2
|
+
export declare class Codegen {
|
|
5
3
|
#private;
|
|
6
4
|
static init(...jobs: CodegenInput[]): Promise<Codegen>;
|
|
7
5
|
static init(options: CodegenOptions, ...jobs: CodegenInput[]): Promise<Codegen>;
|
|
8
6
|
static init(jobOrOptions: CodegenInput | CodegenOptions, ...moreJobs: CodegenInput[]): Promise<Codegen>;
|
|
9
7
|
private constructor();
|
|
10
|
-
generateAll(): Promise<
|
|
11
|
-
generateMatching(path: string): Promise<
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
|
-
export { Codegen };
|
|
8
|
+
generateAll(): Promise<boolean>;
|
|
9
|
+
generateMatching(path: string): Promise<boolean>;
|
|
10
|
+
}
|
package/dist/codegen.mjs
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
import { hash } from "./utils.mjs";
|
|
2
2
|
import { Cache } from "./cache.mjs";
|
|
3
3
|
import { File } from "./file.mjs";
|
|
4
|
+
import { postprocessors } from "./postprocessors/index.mjs";
|
|
4
5
|
import { normalizeJobs, normalizeOptions } from "./options.mjs";
|
|
5
|
-
import { resolveOxfmtConfig } from "./oxfmt.mjs";
|
|
6
6
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
7
7
|
import { dirname, resolve } from "node:path";
|
|
8
8
|
import picomatch from "picomatch";
|
|
9
9
|
import { escapePath, glob } from "tinyglobby";
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// src/codegen.ts
|
|
12
12
|
var Codegen = class Codegen {
|
|
13
13
|
#options;
|
|
14
14
|
#cache;
|
|
15
15
|
#jobs;
|
|
16
16
|
#running = /* @__PURE__ */ new Map();
|
|
17
|
-
#oxfmt;
|
|
18
|
-
#eslint;
|
|
19
|
-
#prettier;
|
|
20
17
|
static async init(jobOrOptions, ...moreJobs) {
|
|
21
18
|
const [options, jobs] = typeof jobOrOptions === "function" || "input" in jobOrOptions ? [void 0, [jobOrOptions, ...moreJobs]] : [jobOrOptions, moreJobs];
|
|
22
19
|
const normalizedOptions = await normalizeOptions(options);
|
|
23
|
-
|
|
20
|
+
const normalizedJobs = await normalizeJobs(normalizedOptions.rootDir, jobs);
|
|
21
|
+
return new Codegen(normalizedOptions, normalizedJobs);
|
|
24
22
|
}
|
|
25
23
|
constructor(options, jobs) {
|
|
26
24
|
this.#options = options;
|
|
@@ -28,33 +26,38 @@ var Codegen = class Codegen {
|
|
|
28
26
|
this.#jobs = this.#createJobMap(jobs);
|
|
29
27
|
}
|
|
30
28
|
async generateAll() {
|
|
31
|
-
await Promise.
|
|
29
|
+
const results = await Promise.all([...this.#jobs.values()].map(this.#generate));
|
|
30
|
+
await this.#cache.flush();
|
|
31
|
+
return results.every((v) => v);
|
|
32
32
|
}
|
|
33
33
|
async generateMatching(path) {
|
|
34
|
-
await Promise.
|
|
34
|
+
const results = await Promise.all([...this.#jobs].map(async ([match, job]) => {
|
|
35
35
|
if (match(path)) await this.#generate(job);
|
|
36
36
|
}));
|
|
37
|
+
await this.#cache.flush();
|
|
38
|
+
return results.every((v) => v);
|
|
37
39
|
}
|
|
38
|
-
async
|
|
40
|
+
#generate = async (job) => {
|
|
39
41
|
this.#running.get(job)?.abort();
|
|
40
42
|
const controller = new AbortController();
|
|
41
43
|
this.#running.set(job, controller);
|
|
42
44
|
const files = await this.#scanFiles(job.input);
|
|
43
45
|
const extraDeps = job.extraDeps?.length ? await this.#scanFiles(job.extraDeps) : [];
|
|
46
|
+
let success = true;
|
|
44
47
|
await this.#cache.check([...files, ...extraDeps], job.output, job.cacheBy, async () => {
|
|
45
48
|
try {
|
|
46
49
|
const file = resolve(this.#options.rootDir, job.output);
|
|
47
50
|
let output = await job.generate(files, controller.signal);
|
|
48
51
|
if (output === null) return;
|
|
49
52
|
if (controller.signal.aborted) return;
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
const disabled = /* @__PURE__ */ new Set();
|
|
54
|
+
for (const postprocessor of postprocessors) {
|
|
55
|
+
if (typeof output !== "string") break;
|
|
56
|
+
if (!(job[postprocessor.id] ?? this.#options[postprocessor.id]) || disabled.has(postprocessor.id)) continue;
|
|
57
|
+
for (const id of postprocessor.conflicts) disabled.add(id);
|
|
58
|
+
output = await postprocessor.apply(output, file);
|
|
59
|
+
if (controller.signal.aborted) return;
|
|
60
|
+
}
|
|
58
61
|
if (controller.signal.aborted) return;
|
|
59
62
|
try {
|
|
60
63
|
await mkdir(dirname(file), {
|
|
@@ -68,53 +71,26 @@ var Codegen = class Codegen {
|
|
|
68
71
|
console.log(`\nError while generating '${job.output}':`);
|
|
69
72
|
console.log(e);
|
|
70
73
|
console.log("");
|
|
74
|
+
success = false;
|
|
71
75
|
return;
|
|
72
76
|
}
|
|
73
77
|
});
|
|
74
|
-
|
|
78
|
+
return success;
|
|
79
|
+
};
|
|
75
80
|
async #scanFiles(patterns) {
|
|
76
81
|
return (await glob(patterns, {
|
|
77
82
|
cwd: this.#options.rootDir,
|
|
78
83
|
absolute: true
|
|
79
84
|
})).sort().map((path) => new File(path, this.#options.rootDir));
|
|
80
85
|
}
|
|
81
|
-
async #applyOxfmt(source, path) {
|
|
82
|
-
const oxfmt = await this.#loadOxfmt();
|
|
83
|
-
const config = await resolveOxfmtConfig(path);
|
|
84
|
-
const { code, errors } = await oxfmt.format(path, source, config);
|
|
85
|
-
const firstError = errors.filter((e) => e.severity === "Error").pop();
|
|
86
|
-
if (firstError) throw new Error(firstError.message);
|
|
87
|
-
return code;
|
|
88
|
-
}
|
|
89
|
-
async #applyESLint(source, path) {
|
|
90
|
-
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
91
|
-
return result?.output ?? source;
|
|
92
|
-
}
|
|
93
|
-
async #applyPrettier(source, path) {
|
|
94
|
-
const prettier = await this.#loadPrettier();
|
|
95
|
-
const options = await prettier.resolveConfig(path);
|
|
96
|
-
return prettier.format(source, {
|
|
97
|
-
...options ?? {},
|
|
98
|
-
filepath: path
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
async #loadOxfmt() {
|
|
102
|
-
return this.#oxfmt ??= import("oxfmt");
|
|
103
|
-
}
|
|
104
|
-
async #loadESLint() {
|
|
105
|
-
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
106
|
-
}
|
|
107
|
-
async #loadPrettier() {
|
|
108
|
-
return this.#prettier ??= import("prettier");
|
|
109
|
-
}
|
|
110
86
|
#createJobMap(jobs) {
|
|
111
87
|
return new Map(jobs.map((job) => [this.#createMatcher(job), job]));
|
|
112
88
|
}
|
|
113
89
|
#createMatcher(job) {
|
|
114
90
|
const root = escapePath(this.#options.rootDir);
|
|
115
|
-
|
|
91
|
+
const patterns = [job.input, job.extraDeps ?? []];
|
|
92
|
+
return picomatch(patterns.flat().map((pattern) => resolve(root, pattern)));
|
|
116
93
|
}
|
|
117
94
|
};
|
|
118
95
|
|
|
119
|
-
//#endregion
|
|
120
96
|
export { Codegen };
|
package/dist/file.cjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
1
|
let node_fs_promises = require("node:fs/promises");
|
|
3
2
|
let node_path = require("node:path");
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
// src/file.ts
|
|
6
5
|
var File = class {
|
|
7
6
|
fullPath;
|
|
8
7
|
localPath;
|
|
@@ -22,5 +21,4 @@ var File = class {
|
|
|
22
21
|
}
|
|
23
22
|
};
|
|
24
23
|
|
|
25
|
-
//#endregion
|
|
26
24
|
exports.File = File;
|
package/dist/file.d.cts
ADDED
package/dist/file.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
declare class File {
|
|
1
|
+
export declare class File {
|
|
3
2
|
#private;
|
|
4
3
|
readonly fullPath: string;
|
|
5
4
|
readonly localPath: string;
|
|
@@ -7,6 +6,4 @@ declare class File {
|
|
|
7
6
|
buffer(): Promise<Buffer>;
|
|
8
7
|
text(encoding?: BufferEncoding): Promise<string>;
|
|
9
8
|
json(): Promise<any>;
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
12
|
-
export { File };
|
|
9
|
+
}
|
package/dist/file.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { relative } from "node:path";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/file.ts
|
|
5
5
|
var File = class {
|
|
6
6
|
fullPath;
|
|
7
7
|
localPath;
|
|
@@ -21,5 +21,4 @@ var File = class {
|
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
//#endregion
|
|
25
24
|
export { File };
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_file = require(
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
2
|
+
const require_file = require("./file.cjs");
|
|
3
|
+
const require_index = require("./postprocessors/index.cjs");
|
|
4
|
+
const require_codegen = require("./codegen.cjs");
|
|
5
|
+
const require_plugin = require("./plugin.cjs");
|
|
6
|
+
const require_types = require("./types.cjs");
|
|
6
7
|
|
|
7
8
|
exports.Codegen = require_codegen.Codegen;
|
|
8
9
|
exports.File = require_file.File;
|
|
9
10
|
exports.codegen = require_plugin.codegen;
|
|
10
|
-
exports.defineCodegen = require_types.defineCodegen;
|
|
11
|
+
exports.defineCodegen = require_types.defineCodegen;
|
|
12
|
+
exports.postprocessors = require_index.postprocessors;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { File } from "./file.cjs";
|
|
2
|
+
import { Postprocessor, PostprocessorId } from "./postprocessors/types.cjs";
|
|
3
|
+
import { PostprocessorOptions, postprocessors } from "./postprocessors/index.cjs";
|
|
4
|
+
import { CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, MaybePromise, NormalizedCodegenOptions, defineCodegen } from "./types.cjs";
|
|
5
|
+
import { Codegen } from "./codegen.cjs";
|
|
6
|
+
import { Plugin, codegen } from "./plugin.cjs";
|
|
7
|
+
export { Codegen, CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, File, MaybePromise, NormalizedCodegenOptions, Plugin, type Postprocessor, type PostprocessorId, PostprocessorOptions, codegen, defineCodegen, postprocessors };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { File } from "./file.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { Postprocessor, PostprocessorId } from "./postprocessors/types.mjs";
|
|
3
|
+
import { PostprocessorOptions, postprocessors } from "./postprocessors/index.mjs";
|
|
4
|
+
import { CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, MaybePromise, NormalizedCodegenOptions, defineCodegen } from "./types.mjs";
|
|
3
5
|
import { Codegen } from "./codegen.mjs";
|
|
4
6
|
import { Plugin, codegen } from "./plugin.mjs";
|
|
5
|
-
export { Codegen, CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, File, NormalizedCodegenOptions, Plugin, codegen, defineCodegen };
|
|
7
|
+
export { Codegen, CodegenInput, CodegenJob, CodegenJobGenerator, CodegenOptions, CodegenPresetOptions, File, MaybePromise, NormalizedCodegenOptions, Plugin, type Postprocessor, type PostprocessorId, PostprocessorOptions, codegen, defineCodegen, postprocessors };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { File } from "./file.mjs";
|
|
2
|
+
import { postprocessors } from "./postprocessors/index.mjs";
|
|
2
3
|
import { Codegen } from "./codegen.mjs";
|
|
3
4
|
import { codegen } from "./plugin.mjs";
|
|
4
5
|
import { defineCodegen } from "./types.mjs";
|
|
5
6
|
|
|
6
|
-
export { Codegen, File, codegen, defineCodegen };
|
|
7
|
+
export { Codegen, File, codegen, defineCodegen, postprocessors };
|
package/dist/options.cjs
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const require_utils = require("./utils.cjs");
|
|
2
|
+
const require_index = require("./postprocessors/index.cjs");
|
|
3
3
|
let node_fs_promises = require("node:fs/promises");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// src/options.ts
|
|
7
7
|
async function normalizeOptions(options = {}) {
|
|
8
8
|
let packageJson;
|
|
9
9
|
const rootDir = (0, node_path.resolve)(options.rootDir ?? process.cwd());
|
|
10
|
+
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
11
|
+
const ppopts = {};
|
|
12
|
+
for (const postprocessor of require_index.postprocessors) ppopts[postprocessor.id] = options[postprocessor.id] ?? hasDevDep(await getPackageJson(), postprocessor.packageName);
|
|
10
13
|
return {
|
|
11
14
|
rootDir,
|
|
12
|
-
cacheFile
|
|
13
|
-
|
|
14
|
-
prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
|
|
15
|
-
eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
|
|
15
|
+
cacheFile,
|
|
16
|
+
...ppopts
|
|
16
17
|
};
|
|
17
18
|
async function getPackageJson() {
|
|
18
19
|
return packageJson ??= readPackageJson(rootDir);
|
|
@@ -47,6 +48,5 @@ async function readPackageJson(dir) {
|
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
//#endregion
|
|
51
51
|
exports.normalizeJobs = normalizeJobs;
|
|
52
52
|
exports.normalizeOptions = normalizeOptions;
|
package/dist/options.mjs
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { lookupPath } from "./utils.mjs";
|
|
2
|
+
import { postprocessors } from "./postprocessors/index.mjs";
|
|
2
3
|
import { mkdir, readFile } from "node:fs/promises";
|
|
3
4
|
import { dirname, resolve } from "node:path";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
// src/options.ts
|
|
6
7
|
async function normalizeOptions(options = {}) {
|
|
7
8
|
let packageJson;
|
|
8
9
|
const rootDir = resolve(options.rootDir ?? process.cwd());
|
|
10
|
+
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
11
|
+
const ppopts = {};
|
|
12
|
+
for (const postprocessor of postprocessors) ppopts[postprocessor.id] = options[postprocessor.id] ?? hasDevDep(await getPackageJson(), postprocessor.packageName);
|
|
9
13
|
return {
|
|
10
14
|
rootDir,
|
|
11
|
-
cacheFile
|
|
12
|
-
|
|
13
|
-
prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
|
|
14
|
-
eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
|
|
15
|
+
cacheFile,
|
|
16
|
+
...ppopts
|
|
15
17
|
};
|
|
16
18
|
async function getPackageJson() {
|
|
17
19
|
return packageJson ??= readPackageJson(rootDir);
|
|
@@ -46,5 +48,4 @@ async function readPackageJson(dir) {
|
|
|
46
48
|
});
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
//#endregion
|
|
50
51
|
export { normalizeJobs, normalizeOptions };
|