pluggable-codegen 0.1.1 → 0.2.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/{chunk-DEEA6FJL.js → chunk-CBSI3RRL.js} +88 -47
- package/dist/cli.js +1 -1
- package/dist/index.cjs +86 -45
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,27 +1,5 @@
|
|
|
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
1
|
// src/utils.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
25
3
|
import { dirname, resolve } from "node:path";
|
|
26
4
|
async function lookupPath(dir, candidates, check) {
|
|
27
5
|
if (!Array.isArray(candidates)) {
|
|
@@ -43,21 +21,52 @@ async function lookupPath(dir, candidates, check) {
|
|
|
43
21
|
return dir !== orig;
|
|
44
22
|
}
|
|
45
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
|
+
};
|
|
46
52
|
|
|
47
53
|
// src/codegen.ts
|
|
48
54
|
import { mkdir as mkdir2, writeFile as writeFile2 } from "node:fs/promises";
|
|
49
|
-
import { dirname as dirname3, resolve as
|
|
55
|
+
import { dirname as dirname3, resolve as resolve4 } from "node:path";
|
|
50
56
|
import picomatch from "picomatch";
|
|
51
57
|
import { escapePath, glob } from "tinyglobby";
|
|
52
58
|
|
|
53
59
|
// src/cache.ts
|
|
54
|
-
import { createHash } from "node:crypto";
|
|
60
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
55
61
|
import { readFile as readFile2, writeFile } from "node:fs/promises";
|
|
62
|
+
import { resolve as resolve2 } from "node:path";
|
|
56
63
|
var Cache = class {
|
|
64
|
+
#rootDir;
|
|
57
65
|
#cacheFile;
|
|
58
66
|
#cacheData;
|
|
59
67
|
#writeTmr;
|
|
60
|
-
constructor(cacheFile) {
|
|
68
|
+
constructor(rootDir, cacheFile) {
|
|
69
|
+
this.#rootDir = rootDir;
|
|
61
70
|
this.#cacheFile = cacheFile;
|
|
62
71
|
}
|
|
63
72
|
async check(files, output, mode, generate) {
|
|
@@ -65,31 +74,57 @@ var Cache = class {
|
|
|
65
74
|
await generate();
|
|
66
75
|
return;
|
|
67
76
|
}
|
|
68
|
-
const [cacheData,
|
|
77
|
+
const [cacheData, sourceHash, outputHash] = await Promise.all([
|
|
69
78
|
this.#cacheData ??= this.#readCacheData(),
|
|
70
|
-
this.#
|
|
79
|
+
this.#getSourceHash(files, mode),
|
|
80
|
+
this.#getOutputHash(resolve2(this.#rootDir, output))
|
|
71
81
|
]);
|
|
72
|
-
|
|
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) {
|
|
73
88
|
return;
|
|
74
89
|
}
|
|
75
|
-
cacheData.set(output,
|
|
90
|
+
cacheData.set(output, {
|
|
91
|
+
source: sourceHash,
|
|
92
|
+
output: newHash
|
|
93
|
+
});
|
|
76
94
|
clearTimeout(this.#writeTmr);
|
|
77
95
|
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
78
96
|
}
|
|
79
|
-
async #
|
|
80
|
-
const
|
|
97
|
+
async #getSourceHash(files, mode) {
|
|
98
|
+
const hash2 = createHash2("sha256");
|
|
81
99
|
for (const file of files) {
|
|
82
|
-
|
|
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;
|
|
83
109
|
}
|
|
84
|
-
return hash.digest("hex");
|
|
85
110
|
}
|
|
86
111
|
async #readCacheData() {
|
|
87
112
|
if (!this.#cacheFile) {
|
|
88
113
|
return /* @__PURE__ */ new Map();
|
|
89
114
|
}
|
|
90
115
|
try {
|
|
91
|
-
const
|
|
92
|
-
|
|
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;
|
|
93
128
|
} catch {
|
|
94
129
|
return /* @__PURE__ */ new Map();
|
|
95
130
|
}
|
|
@@ -100,13 +135,19 @@ var Cache = class {
|
|
|
100
135
|
}
|
|
101
136
|
}
|
|
102
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
|
+
}
|
|
103
144
|
|
|
104
145
|
// src/options.ts
|
|
105
146
|
import { mkdir, readFile as readFile3 } from "node:fs/promises";
|
|
106
|
-
import { dirname as dirname2, resolve as
|
|
147
|
+
import { dirname as dirname2, resolve as resolve3 } from "node:path";
|
|
107
148
|
async function normalizeOptions(options = {}) {
|
|
108
149
|
let packageJson;
|
|
109
|
-
const rootDir =
|
|
150
|
+
const rootDir = resolve3(options.rootDir ?? process.cwd());
|
|
110
151
|
const cacheFile = await resolveCacheFile(rootDir, options.cacheFile);
|
|
111
152
|
const prettier = options.prettier ?? hasDevDep(await getPackageJson(), "prettier");
|
|
112
153
|
const eslint = options.eslint ?? hasDevDep(await getPackageJson(), "eslint");
|
|
@@ -127,7 +168,7 @@ async function resolveCacheFile(rootDir, cacheFile) {
|
|
|
127
168
|
if (cacheFile === false) {
|
|
128
169
|
return void 0;
|
|
129
170
|
} else if (cacheFile !== void 0) {
|
|
130
|
-
return
|
|
171
|
+
return resolve3(rootDir, cacheFile);
|
|
131
172
|
}
|
|
132
173
|
const codegenPath = typeof __dirname !== "undefined" ? __dirname : import.meta.dirname;
|
|
133
174
|
if (!codegenPath.includes("/node_modules/")) {
|
|
@@ -166,7 +207,7 @@ var Codegen = class _Codegen {
|
|
|
166
207
|
}
|
|
167
208
|
constructor(options, jobs) {
|
|
168
209
|
this.#options = options;
|
|
169
|
-
this.#cache = new Cache(options.cacheFile);
|
|
210
|
+
this.#cache = new Cache(options.rootDir, options.cacheFile);
|
|
170
211
|
this.#jobs = this.#createJobMap(jobs);
|
|
171
212
|
}
|
|
172
213
|
async generateAll() {
|
|
@@ -187,32 +228,32 @@ var Codegen = class _Codegen {
|
|
|
187
228
|
this.#running.set(job, controller);
|
|
188
229
|
const files = await this.#scanFiles(job);
|
|
189
230
|
await this.#cache.check(files, job.output, job.cacheBy, async () => {
|
|
190
|
-
const file =
|
|
231
|
+
const file = resolve4(this.#options.rootDir, job.output);
|
|
191
232
|
let output = await job.generate(files, controller.signal);
|
|
192
233
|
if (output === null) {
|
|
193
|
-
return
|
|
234
|
+
return void 0;
|
|
194
235
|
}
|
|
195
236
|
if (controller.signal.aborted) {
|
|
196
|
-
return
|
|
237
|
+
return void 0;
|
|
197
238
|
}
|
|
198
239
|
if (typeof output === "string" && (job.eslint ?? this.#options.eslint)) {
|
|
199
240
|
output = await this.#applyESLint(output, file);
|
|
200
241
|
}
|
|
201
242
|
if (controller.signal.aborted) {
|
|
202
|
-
return
|
|
243
|
+
return void 0;
|
|
203
244
|
}
|
|
204
245
|
if (typeof output === "string" && (job.prettier ?? this.#options.prettier)) {
|
|
205
246
|
output = await this.#applyPrettier(output, file);
|
|
206
247
|
}
|
|
207
248
|
if (controller.signal.aborted) {
|
|
208
|
-
return
|
|
249
|
+
return void 0;
|
|
209
250
|
}
|
|
210
251
|
try {
|
|
211
252
|
await mkdir2(dirname3(file), { mode: 488, recursive: true });
|
|
212
253
|
} catch {
|
|
213
254
|
}
|
|
214
255
|
await writeFile2(file, output);
|
|
215
|
-
return
|
|
256
|
+
return hash(output);
|
|
216
257
|
});
|
|
217
258
|
}
|
|
218
259
|
async #scanFiles(job) {
|
|
@@ -259,7 +300,7 @@ var Codegen = class _Codegen {
|
|
|
259
300
|
};
|
|
260
301
|
|
|
261
302
|
export {
|
|
262
|
-
File,
|
|
263
303
|
lookupPath,
|
|
304
|
+
File,
|
|
264
305
|
Codegen
|
|
265
306
|
};
|
package/dist/cli.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -7,11 +7,45 @@ var _tinyglobby = require('tinyglobby');
|
|
|
7
7
|
// src/cache.ts
|
|
8
8
|
var _crypto = require('crypto');
|
|
9
9
|
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// src/utils.ts
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async function lookupPath(dir, candidates, check) {
|
|
16
|
+
if (!Array.isArray(candidates)) {
|
|
17
|
+
candidates = [candidates];
|
|
18
|
+
}
|
|
19
|
+
do {
|
|
20
|
+
for (const candidate of candidates) {
|
|
21
|
+
const path = _path.resolve.call(void 0, dir, candidate);
|
|
22
|
+
try {
|
|
23
|
+
return await check(path);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
} while (cdup());
|
|
28
|
+
return void 0;
|
|
29
|
+
function cdup() {
|
|
30
|
+
const orig = dir;
|
|
31
|
+
dir = _path.dirname.call(void 0, dir);
|
|
32
|
+
return dir !== orig;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function hash(data) {
|
|
36
|
+
const hash2 = _crypto.createHash.call(void 0, "sha256");
|
|
37
|
+
hash2.update(data);
|
|
38
|
+
return hash2.digest("hex");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/cache.ts
|
|
10
42
|
var Cache = class {
|
|
43
|
+
#rootDir;
|
|
11
44
|
#cacheFile;
|
|
12
45
|
#cacheData;
|
|
13
46
|
#writeTmr;
|
|
14
|
-
constructor(cacheFile) {
|
|
47
|
+
constructor(rootDir, cacheFile) {
|
|
48
|
+
this.#rootDir = rootDir;
|
|
15
49
|
this.#cacheFile = cacheFile;
|
|
16
50
|
}
|
|
17
51
|
async check(files, output, mode, generate) {
|
|
@@ -19,32 +53,58 @@ var Cache = class {
|
|
|
19
53
|
await generate();
|
|
20
54
|
return;
|
|
21
55
|
}
|
|
22
|
-
const [cacheData,
|
|
56
|
+
const [cacheData, sourceHash, outputHash] = await Promise.all([
|
|
23
57
|
this.#cacheData ??= this.#readCacheData(),
|
|
24
|
-
this.#
|
|
58
|
+
this.#getSourceHash(files, mode),
|
|
59
|
+
this.#getOutputHash(_path.resolve.call(void 0, this.#rootDir, output))
|
|
25
60
|
]);
|
|
26
|
-
|
|
61
|
+
const entry = cacheData.get(output);
|
|
62
|
+
if (entry && entry.source === sourceHash && entry.output === outputHash) {
|
|
27
63
|
return;
|
|
28
64
|
}
|
|
29
|
-
|
|
65
|
+
const newHash = await generate();
|
|
66
|
+
if (newHash === void 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
cacheData.set(output, {
|
|
70
|
+
source: sourceHash,
|
|
71
|
+
output: newHash
|
|
72
|
+
});
|
|
30
73
|
clearTimeout(this.#writeTmr);
|
|
31
74
|
this.#writeTmr = setTimeout(() => this.#writeCacheData(cacheData), 250);
|
|
32
75
|
}
|
|
33
|
-
async #
|
|
34
|
-
const
|
|
76
|
+
async #getSourceHash(files, mode) {
|
|
77
|
+
const hash2 = _crypto.createHash.call(void 0, "sha256");
|
|
35
78
|
for (const file of files) {
|
|
36
|
-
|
|
79
|
+
hash2.update(mode === "files" ? file.localPath : await file.buffer());
|
|
80
|
+
}
|
|
81
|
+
return hash2.digest("hex");
|
|
82
|
+
}
|
|
83
|
+
async #getOutputHash(file) {
|
|
84
|
+
try {
|
|
85
|
+
return hash(await _promises.readFile.call(void 0, file));
|
|
86
|
+
} catch (e2) {
|
|
87
|
+
return void 0;
|
|
37
88
|
}
|
|
38
|
-
return hash.digest("hex");
|
|
39
89
|
}
|
|
40
90
|
async #readCacheData() {
|
|
41
91
|
if (!this.#cacheFile) {
|
|
42
92
|
return /* @__PURE__ */ new Map();
|
|
43
93
|
}
|
|
44
94
|
try {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
95
|
+
const rawData = JSON.parse(await _promises.readFile.call(void 0, this.#cacheFile, "utf-8"));
|
|
96
|
+
const cacheData = /* @__PURE__ */ new Map();
|
|
97
|
+
if (!isObject(rawData)) {
|
|
98
|
+
return cacheData;
|
|
99
|
+
}
|
|
100
|
+
for (const [key, value] of Object.entries(rawData)) {
|
|
101
|
+
if (!isObject(value) || !isValidEntry(value)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
cacheData.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
return cacheData;
|
|
107
|
+
} catch (e3) {
|
|
48
108
|
return /* @__PURE__ */ new Map();
|
|
49
109
|
}
|
|
50
110
|
}
|
|
@@ -54,6 +114,12 @@ var Cache = class {
|
|
|
54
114
|
}
|
|
55
115
|
}
|
|
56
116
|
};
|
|
117
|
+
function isObject(v) {
|
|
118
|
+
return !!v && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
119
|
+
}
|
|
120
|
+
function isValidEntry(v) {
|
|
121
|
+
return Object.keys(v).length === 2 && "source" in v && typeof v.source === "string" && "output" in v && typeof v.output === "string";
|
|
122
|
+
}
|
|
57
123
|
|
|
58
124
|
// src/file.ts
|
|
59
125
|
|
|
@@ -81,31 +147,6 @@ var File = class {
|
|
|
81
147
|
// src/options.ts
|
|
82
148
|
|
|
83
149
|
|
|
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
150
|
async function normalizeOptions(options = {}) {
|
|
110
151
|
let packageJson;
|
|
111
152
|
const rootDir = _path.resolve.call(void 0, _nullishCoalesce(options.rootDir, () => ( process.cwd())));
|
|
@@ -138,7 +179,7 @@ async function resolveCacheFile(rootDir, cacheFile) {
|
|
|
138
179
|
cacheFile = codegenPath.replace(/^(.+\/node_modules)\/.*$/, "$1/.cache/pluggable-codegen.json");
|
|
139
180
|
try {
|
|
140
181
|
await _promises.mkdir.call(void 0, _path.dirname.call(void 0, cacheFile), { recursive: true, mode: 488 });
|
|
141
|
-
} catch (
|
|
182
|
+
} catch (e4) {
|
|
142
183
|
}
|
|
143
184
|
return cacheFile;
|
|
144
185
|
}
|
|
@@ -168,7 +209,7 @@ var Codegen = class _Codegen {
|
|
|
168
209
|
}
|
|
169
210
|
constructor(options, jobs) {
|
|
170
211
|
this.#options = options;
|
|
171
|
-
this.#cache = new Cache(options.cacheFile);
|
|
212
|
+
this.#cache = new Cache(options.rootDir, options.cacheFile);
|
|
172
213
|
this.#jobs = this.#createJobMap(jobs);
|
|
173
214
|
}
|
|
174
215
|
async generateAll() {
|
|
@@ -192,29 +233,29 @@ var Codegen = class _Codegen {
|
|
|
192
233
|
const file = _path.resolve.call(void 0, this.#options.rootDir, job.output);
|
|
193
234
|
let output = await job.generate(files, controller.signal);
|
|
194
235
|
if (output === null) {
|
|
195
|
-
return
|
|
236
|
+
return void 0;
|
|
196
237
|
}
|
|
197
238
|
if (controller.signal.aborted) {
|
|
198
|
-
return
|
|
239
|
+
return void 0;
|
|
199
240
|
}
|
|
200
241
|
if (typeof output === "string" && (_nullishCoalesce(job.eslint, () => ( this.#options.eslint)))) {
|
|
201
242
|
output = await this.#applyESLint(output, file);
|
|
202
243
|
}
|
|
203
244
|
if (controller.signal.aborted) {
|
|
204
|
-
return
|
|
245
|
+
return void 0;
|
|
205
246
|
}
|
|
206
247
|
if (typeof output === "string" && (_nullishCoalesce(job.prettier, () => ( this.#options.prettier)))) {
|
|
207
248
|
output = await this.#applyPrettier(output, file);
|
|
208
249
|
}
|
|
209
250
|
if (controller.signal.aborted) {
|
|
210
|
-
return
|
|
251
|
+
return void 0;
|
|
211
252
|
}
|
|
212
253
|
try {
|
|
213
254
|
await _promises.mkdir.call(void 0, _path.dirname.call(void 0, file), { mode: 488, recursive: true });
|
|
214
|
-
} catch (
|
|
255
|
+
} catch (e5) {
|
|
215
256
|
}
|
|
216
257
|
await _promises.writeFile.call(void 0, file, output);
|
|
217
|
-
return
|
|
258
|
+
return hash(output);
|
|
218
259
|
});
|
|
219
260
|
}
|
|
220
261
|
async #scanFiles(job) {
|
package/dist/index.js
CHANGED