kerria 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/index.d.ts +4 -4
- package/dist/index.js +33 -13
- package/package.json +6 -8
package/dist/index.d.ts
CHANGED
|
@@ -40,16 +40,16 @@ declare function useSource<C extends object>(kind: number, options: UseSourceOpt
|
|
|
40
40
|
|
|
41
41
|
//#endregion
|
|
42
42
|
//#region src/core/processor.d.ts
|
|
43
|
-
interface
|
|
43
|
+
interface KerriaContext {
|
|
44
44
|
sign: string;
|
|
45
45
|
loadInfos: LoadInfo[];
|
|
46
46
|
sourceInfos: SourceInfo[];
|
|
47
47
|
}
|
|
48
|
-
declare function useCurrentContext():
|
|
49
|
-
declare function
|
|
48
|
+
declare function useCurrentContext(): KerriaContext;
|
|
49
|
+
declare function createKerria(sign: string, setup: (ctx: KerriaContext) => void): {
|
|
50
50
|
build: () => Promise<void>;
|
|
51
51
|
watch: () => Promise<void>;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
//#endregion
|
|
55
|
-
export { LoadInfo, SourceInfo, UseLoadOptions,
|
|
55
|
+
export { LoadInfo, SourceInfo, UseLoadOptions, createKerria, useCurrentContext, useLoad, useSource };
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { readFile, stat, writeFile } from "node:fs/promises";
|
|
2
4
|
import chokidar from "chokidar";
|
|
3
5
|
import consola from "consola";
|
|
4
|
-
import
|
|
5
|
-
import fs from "fs-extra";
|
|
6
|
+
import * as pkg from "empathic/package";
|
|
6
7
|
import { join, resolve } from "pathe";
|
|
7
8
|
import { glob } from "tinyglobby";
|
|
9
|
+
import { dirname } from "node:path";
|
|
8
10
|
|
|
9
11
|
//#region src/utils.ts
|
|
10
12
|
const isDev = process.env.NODE_ENV === "development";
|
|
11
13
|
function capitalize(str) {
|
|
12
14
|
return str[0].toUpperCase() + str.slice(1);
|
|
13
15
|
}
|
|
16
|
+
async function readJson(path) {
|
|
17
|
+
const data = await readFile(path, "utf-8");
|
|
18
|
+
return JSON.parse(data);
|
|
19
|
+
}
|
|
20
|
+
function readJsonSync(path) {
|
|
21
|
+
const data = readFileSync(path, "utf-8");
|
|
22
|
+
return JSON.parse(data);
|
|
23
|
+
}
|
|
24
|
+
function writeJson(path, data) {
|
|
25
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
26
|
+
const json = JSON.stringify(data);
|
|
27
|
+
return writeFile(path, json);
|
|
28
|
+
}
|
|
29
|
+
function writeJsonSync(path, data) {
|
|
30
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
31
|
+
const json = JSON.stringify(data);
|
|
32
|
+
return writeFileSync(path, json);
|
|
33
|
+
}
|
|
14
34
|
|
|
15
35
|
//#endregion
|
|
16
36
|
//#region src/core/processor.ts
|
|
@@ -18,7 +38,7 @@ let currentContext = null;
|
|
|
18
38
|
function useCurrentContext() {
|
|
19
39
|
return currentContext;
|
|
20
40
|
}
|
|
21
|
-
function
|
|
41
|
+
function createKerria(sign, setup) {
|
|
22
42
|
const ctx = {
|
|
23
43
|
sign,
|
|
24
44
|
loadInfos: [],
|
|
@@ -30,9 +50,9 @@ function createProcessor(sign, setup) {
|
|
|
30
50
|
ctx.sourceInfos.sort((a, b) => {
|
|
31
51
|
return a.kind - b.kind;
|
|
32
52
|
});
|
|
33
|
-
const cacheDir =
|
|
53
|
+
const cacheDir = pkg.cache("kerria", { create: true });
|
|
34
54
|
const cachePath = join(cacheDir, `${sign}.json`);
|
|
35
|
-
const caches =
|
|
55
|
+
const caches = existsSync(cachePath) && readJsonSync(cachePath) || {};
|
|
36
56
|
async function build() {
|
|
37
57
|
for (const info of ctx.sourceInfos) {
|
|
38
58
|
const paths = await glob(info.patterns, {
|
|
@@ -61,7 +81,7 @@ function createProcessor(sign, setup) {
|
|
|
61
81
|
for (const info of ctx.loadInfos) {
|
|
62
82
|
if (!info.src) continue;
|
|
63
83
|
chokidar.watch(info.src, { ignoreInitial: true }).on("change", async () => {
|
|
64
|
-
const newVal = await
|
|
84
|
+
const newVal = await readJson(info.src);
|
|
65
85
|
info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
|
|
66
86
|
info.output();
|
|
67
87
|
consola.success(`[${sign}] Change "${info.src}"`);
|
|
@@ -69,7 +89,7 @@ function createProcessor(sign, setup) {
|
|
|
69
89
|
}
|
|
70
90
|
}
|
|
71
91
|
async function parse(path, info) {
|
|
72
|
-
const stats = await
|
|
92
|
+
const stats = await stat(path);
|
|
73
93
|
const hash = createHash("md5").update(stats.mtimeMs.toString()).digest("hex");
|
|
74
94
|
let cache = caches[path];
|
|
75
95
|
if (isDev && cache?.hash === hash) {
|
|
@@ -101,8 +121,8 @@ function createProcessor(sign, setup) {
|
|
|
101
121
|
return true;
|
|
102
122
|
}
|
|
103
123
|
function outputLoads() {
|
|
104
|
-
if (isDev)
|
|
105
|
-
else
|
|
124
|
+
if (isDev) writeJsonSync(cachePath, caches);
|
|
125
|
+
else rmSync(cachePath);
|
|
106
126
|
for (const info of ctx.loadInfos) info.output();
|
|
107
127
|
}
|
|
108
128
|
return {
|
|
@@ -122,11 +142,11 @@ function useLoad(name, options) {
|
|
|
122
142
|
name,
|
|
123
143
|
src,
|
|
124
144
|
out,
|
|
125
|
-
value: src ?
|
|
145
|
+
value: src ? readJsonSync(src) : defaultValue,
|
|
126
146
|
onUpdate,
|
|
127
147
|
output() {
|
|
128
148
|
const data = beforeOutput?.(info.value) ?? info.value;
|
|
129
|
-
|
|
149
|
+
writeJsonSync(out, data);
|
|
130
150
|
}
|
|
131
151
|
};
|
|
132
152
|
ctx.loadInfos.push(info);
|
|
@@ -157,7 +177,7 @@ function useSource(kind, options) {
|
|
|
157
177
|
},
|
|
158
178
|
async output(path, data) {
|
|
159
179
|
const outPath = path.replace(base, dist).replace(info.ext, ".json");
|
|
160
|
-
await
|
|
180
|
+
await writeJson(outPath, data);
|
|
161
181
|
}
|
|
162
182
|
};
|
|
163
183
|
ctx.sourceInfos.push(info);
|
|
@@ -165,4 +185,4 @@ function useSource(kind, options) {
|
|
|
165
185
|
}
|
|
166
186
|
|
|
167
187
|
//#endregion
|
|
168
|
-
export {
|
|
188
|
+
export { createKerria, useCurrentContext, useLoad, useSource };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kerria",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "Composable source processor",
|
|
6
6
|
"author": "KazariEX",
|
|
7
7
|
"license": "MIT",
|
|
@@ -19,20 +19,18 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"chokidar": "^4.0.3",
|
|
21
21
|
"consola": "^3.4.2",
|
|
22
|
-
"
|
|
23
|
-
"fs-extra": "^11.3.0",
|
|
22
|
+
"empathic": "^1.1.0",
|
|
24
23
|
"pathe": "^2.0.3",
|
|
25
24
|
"tinyglobby": "^0.2.13"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@antfu/eslint-config": "^4.
|
|
29
|
-
"@types/
|
|
30
|
-
"@types/node": "^22.15.3",
|
|
27
|
+
"@antfu/eslint-config": "^4.13.0",
|
|
28
|
+
"@types/node": "^22.15.17",
|
|
31
29
|
"@zinkawaii/eslint-config": "^0.3.0",
|
|
32
30
|
"@zinkawaii/tsconfig": "^0.0.2",
|
|
33
31
|
"bumpp": "^10.1.0",
|
|
34
|
-
"eslint": "^9.
|
|
35
|
-
"tsdown": "^0.
|
|
32
|
+
"eslint": "^9.26.0",
|
|
33
|
+
"tsdown": "^0.11.5"
|
|
36
34
|
},
|
|
37
35
|
"scripts": {
|
|
38
36
|
"build": "tsdown",
|