pluggable-codegen 0.3.4 → 0.4.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 +2 -1
- package/dist/codegen.cjs +20 -2
- package/dist/codegen.mjs +20 -2
- package/dist/options.cjs +1 -0
- package/dist/options.mjs +1 -0
- package/dist/oxfmt.cjs +24 -0
- package/dist/oxfmt.mjs +23 -0
- package/dist/types.d.mts +3 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ export default defineCodegen(
|
|
|
38
38
|
// auto-detected based on devDependencies:
|
|
39
39
|
eslint: undefined,
|
|
40
40
|
prettier: undefined,
|
|
41
|
+
oxfmt: undefined, // only an '.oxfmtrc.json' config file is supported for now
|
|
41
42
|
},
|
|
42
43
|
// followed by one or more codegen jobs:
|
|
43
44
|
{
|
|
@@ -61,7 +62,7 @@ export default defineCodegen(
|
|
|
61
62
|
// checking the cache; this can include e.g. `import.meta.filename`
|
|
62
63
|
// to make the job rebuild when its definition changes
|
|
63
64
|
extraDeps: undefined,
|
|
64
|
-
// you can override the global 'eslint' and '
|
|
65
|
+
// you can override the global 'eslint', 'prettier', and 'oxfmt' options
|
|
65
66
|
// per-job, e.g.:
|
|
66
67
|
prettier: false,
|
|
67
68
|
},
|
package/dist/codegen.cjs
CHANGED
|
@@ -3,6 +3,7 @@ const require_utils = require('./utils.cjs');
|
|
|
3
3
|
const require_cache = require('./cache.cjs');
|
|
4
4
|
const require_file = require('./file.cjs');
|
|
5
5
|
const require_options = require('./options.cjs');
|
|
6
|
+
const require_oxfmt = require('./oxfmt.cjs');
|
|
6
7
|
let node_fs_promises = require("node:fs/promises");
|
|
7
8
|
let node_path = require("node:path");
|
|
8
9
|
let picomatch = require("picomatch");
|
|
@@ -15,6 +16,7 @@ var Codegen = class Codegen {
|
|
|
15
16
|
#cache;
|
|
16
17
|
#jobs;
|
|
17
18
|
#running = /* @__PURE__ */ new Map();
|
|
19
|
+
#oxfmt;
|
|
18
20
|
#eslint;
|
|
19
21
|
#prettier;
|
|
20
22
|
static async init(jobOrOptions, ...moreJobs) {
|
|
@@ -47,9 +49,14 @@ var Codegen = class Codegen {
|
|
|
47
49
|
let output = await job.generate(files, controller.signal);
|
|
48
50
|
if (output === null) return;
|
|
49
51
|
if (controller.signal.aborted) return;
|
|
50
|
-
|
|
52
|
+
const oxfmt = job.oxfmt ?? this.#options.oxfmt;
|
|
53
|
+
const eslint = job.eslint ?? this.#options.eslint;
|
|
54
|
+
const prettier = job.prettier ?? this.#options.prettier;
|
|
55
|
+
if (typeof output === "string" && oxfmt) output = await this.#applyOxfmt(output, file);
|
|
51
56
|
if (controller.signal.aborted) return;
|
|
52
|
-
if (typeof output === "string" &&
|
|
57
|
+
if (typeof output === "string" && !oxfmt && eslint) output = await this.#applyESLint(output, file);
|
|
58
|
+
if (controller.signal.aborted) return;
|
|
59
|
+
if (typeof output === "string" && !oxfmt && prettier) output = await this.#applyPrettier(output, file);
|
|
53
60
|
if (controller.signal.aborted) return;
|
|
54
61
|
try {
|
|
55
62
|
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), {
|
|
@@ -73,6 +80,14 @@ var Codegen = class Codegen {
|
|
|
73
80
|
absolute: true
|
|
74
81
|
})).sort().map((path) => new require_file.File(path, this.#options.rootDir));
|
|
75
82
|
}
|
|
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
|
+
}
|
|
76
91
|
async #applyESLint(source, path) {
|
|
77
92
|
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
78
93
|
return result?.output ?? source;
|
|
@@ -85,6 +100,9 @@ var Codegen = class Codegen {
|
|
|
85
100
|
filepath: path
|
|
86
101
|
});
|
|
87
102
|
}
|
|
103
|
+
async #loadOxfmt() {
|
|
104
|
+
return this.#oxfmt ??= import("oxfmt");
|
|
105
|
+
}
|
|
88
106
|
async #loadESLint() {
|
|
89
107
|
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
90
108
|
}
|
package/dist/codegen.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { hash } from "./utils.mjs";
|
|
|
2
2
|
import { Cache } from "./cache.mjs";
|
|
3
3
|
import { File } from "./file.mjs";
|
|
4
4
|
import { normalizeJobs, normalizeOptions } from "./options.mjs";
|
|
5
|
+
import { resolveOxfmtConfig } from "./oxfmt.mjs";
|
|
5
6
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
6
7
|
import { dirname, resolve } from "node:path";
|
|
7
8
|
import picomatch from "picomatch";
|
|
@@ -13,6 +14,7 @@ var Codegen = class Codegen {
|
|
|
13
14
|
#cache;
|
|
14
15
|
#jobs;
|
|
15
16
|
#running = /* @__PURE__ */ new Map();
|
|
17
|
+
#oxfmt;
|
|
16
18
|
#eslint;
|
|
17
19
|
#prettier;
|
|
18
20
|
static async init(jobOrOptions, ...moreJobs) {
|
|
@@ -45,9 +47,14 @@ var Codegen = class Codegen {
|
|
|
45
47
|
let output = await job.generate(files, controller.signal);
|
|
46
48
|
if (output === null) return;
|
|
47
49
|
if (controller.signal.aborted) return;
|
|
48
|
-
|
|
50
|
+
const oxfmt = job.oxfmt ?? this.#options.oxfmt;
|
|
51
|
+
const eslint = job.eslint ?? this.#options.eslint;
|
|
52
|
+
const prettier = job.prettier ?? this.#options.prettier;
|
|
53
|
+
if (typeof output === "string" && oxfmt) output = await this.#applyOxfmt(output, file);
|
|
49
54
|
if (controller.signal.aborted) return;
|
|
50
|
-
if (typeof output === "string" &&
|
|
55
|
+
if (typeof output === "string" && !oxfmt && eslint) output = await this.#applyESLint(output, file);
|
|
56
|
+
if (controller.signal.aborted) return;
|
|
57
|
+
if (typeof output === "string" && !oxfmt && prettier) output = await this.#applyPrettier(output, file);
|
|
51
58
|
if (controller.signal.aborted) return;
|
|
52
59
|
try {
|
|
53
60
|
await mkdir(dirname(file), {
|
|
@@ -71,6 +78,14 @@ var Codegen = class Codegen {
|
|
|
71
78
|
absolute: true
|
|
72
79
|
})).sort().map((path) => new File(path, this.#options.rootDir));
|
|
73
80
|
}
|
|
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
|
+
}
|
|
74
89
|
async #applyESLint(source, path) {
|
|
75
90
|
const [result] = await (await this.#loadESLint()).lintText(source, { filePath: path });
|
|
76
91
|
return result?.output ?? source;
|
|
@@ -83,6 +98,9 @@ var Codegen = class Codegen {
|
|
|
83
98
|
filepath: path
|
|
84
99
|
});
|
|
85
100
|
}
|
|
101
|
+
async #loadOxfmt() {
|
|
102
|
+
return this.#oxfmt ??= import("oxfmt");
|
|
103
|
+
}
|
|
86
104
|
async #loadESLint() {
|
|
87
105
|
return this.#eslint ??= import("eslint").then(({ ESLint }) => new ESLint({ fix: true }));
|
|
88
106
|
}
|
package/dist/options.cjs
CHANGED
|
@@ -10,6 +10,7 @@ async function normalizeOptions(options = {}) {
|
|
|
10
10
|
return {
|
|
11
11
|
rootDir,
|
|
12
12
|
cacheFile: await resolveCacheFile(rootDir, options.cacheFile),
|
|
13
|
+
oxfmt: options.oxfmt ?? hasDevDep(await getPackageJson(), "oxfmt"),
|
|
13
14
|
prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
|
|
14
15
|
eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
|
|
15
16
|
};
|
package/dist/options.mjs
CHANGED
|
@@ -9,6 +9,7 @@ async function normalizeOptions(options = {}) {
|
|
|
9
9
|
return {
|
|
10
10
|
rootDir,
|
|
11
11
|
cacheFile: await resolveCacheFile(rootDir, options.cacheFile),
|
|
12
|
+
oxfmt: options.oxfmt ?? hasDevDep(await getPackageJson(), "oxfmt"),
|
|
12
13
|
prettier: options.prettier ?? hasDevDep(await getPackageJson(), "prettier"),
|
|
13
14
|
eslint: options.eslint ?? hasDevDep(await getPackageJson(), "eslint")
|
|
14
15
|
};
|
package/dist/oxfmt.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
|
|
6
|
+
//#region src/oxfmt.ts
|
|
7
|
+
const candidates = [".oxfmtrc.json"];
|
|
8
|
+
const cache = /* @__PURE__ */ new Map();
|
|
9
|
+
async function resolveOxfmtConfig(from) {
|
|
10
|
+
const dir = (0, node_path.dirname)(from);
|
|
11
|
+
const cached = cache.get(dir);
|
|
12
|
+
if (cached) return cached;
|
|
13
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
14
|
+
cache.set(dir, promise);
|
|
15
|
+
resolve(await require_utils.lookupPath(dir, candidates, loadConfig));
|
|
16
|
+
return promise;
|
|
17
|
+
}
|
|
18
|
+
async function loadConfig(file) {
|
|
19
|
+
const contents = await (0, node_fs_promises.readFile)(file, "utf-8");
|
|
20
|
+
return JSON.parse(contents);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
exports.resolveOxfmtConfig = resolveOxfmtConfig;
|
package/dist/oxfmt.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { lookupPath } from "./utils.mjs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/oxfmt.ts
|
|
6
|
+
const candidates = [".oxfmtrc.json"];
|
|
7
|
+
const cache = /* @__PURE__ */ new Map();
|
|
8
|
+
async function resolveOxfmtConfig(from) {
|
|
9
|
+
const dir = dirname(from);
|
|
10
|
+
const cached = cache.get(dir);
|
|
11
|
+
if (cached) return cached;
|
|
12
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
13
|
+
cache.set(dir, promise);
|
|
14
|
+
resolve(await lookupPath(dir, candidates, loadConfig));
|
|
15
|
+
return promise;
|
|
16
|
+
}
|
|
17
|
+
async function loadConfig(file) {
|
|
18
|
+
const contents = await readFile(file, "utf-8");
|
|
19
|
+
return JSON.parse(contents);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { resolveOxfmtConfig };
|
package/dist/types.d.mts
CHANGED
|
@@ -5,12 +5,14 @@ type MaybePromise<T> = Promise<T> | T;
|
|
|
5
5
|
type CodegenOptions = {
|
|
6
6
|
rootDir?: string;
|
|
7
7
|
cacheFile?: string | false;
|
|
8
|
+
oxfmt?: boolean;
|
|
8
9
|
prettier?: boolean;
|
|
9
10
|
eslint?: boolean;
|
|
10
11
|
};
|
|
11
12
|
type NormalizedCodegenOptions = {
|
|
12
13
|
rootDir: string;
|
|
13
14
|
cacheFile?: string;
|
|
15
|
+
oxfmt: boolean;
|
|
14
16
|
prettier: boolean;
|
|
15
17
|
eslint: boolean;
|
|
16
18
|
};
|
|
@@ -20,6 +22,7 @@ type CodegenJob = {
|
|
|
20
22
|
generate: (files: File[], signal: AbortSignal) => MaybePromise<Buffer | string | null>;
|
|
21
23
|
cacheBy?: 'files' | 'contents';
|
|
22
24
|
extraDeps?: string[];
|
|
25
|
+
oxfmt?: boolean;
|
|
23
26
|
prettier?: boolean;
|
|
24
27
|
eslint?: boolean;
|
|
25
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pluggable-codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://github.com/jahudka/pluggable-codegen",
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"maintainers": [
|
|
14
14
|
{
|
|
15
|
-
"name": "
|
|
15
|
+
"name": "Eliška Daniela Kaderová",
|
|
16
16
|
"email": "me@subsonic.cz",
|
|
17
17
|
"url": "https://github.com/jahudka"
|
|
18
18
|
}
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
66
66
|
"eslint-plugin-unused-imports": "^4.4.1",
|
|
67
67
|
"globals": "^17.6.0",
|
|
68
|
+
"oxfmt": "^0.63.0",
|
|
68
69
|
"prettier": "^3.8.3",
|
|
69
70
|
"tsdown": "^0.22.0",
|
|
70
71
|
"typescript": "^6.0.3",
|