kerria 0.0.1 → 0.2.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 +17 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -8
- package/package.json +4 -9
- package/dist/index.cjs +0 -235
- package/dist/index.d.cts +0 -48
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Kerria
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/kerria)
|
|
4
|
+
[](https://www.npmjs.com/package/kerria)
|
|
5
|
+
[](/LICENSE)
|
|
6
|
+
|
|
7
|
+
这是一个渲染无关的文件处理管线。核心思路是将数据分为源数据(Source)与负载数据(Load)两类,源拥有以文件为单位的构建与监听生命周期,负载从源中派生,并在每一次源(或本身所依赖的初始状态)文件发生变化时输出。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
pnpm i kerria
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 使用方式
|
|
16
|
+
|
|
17
|
+
请参考[示例文件](playground/kerria.config.ts)。
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ interface UseLoadOptions {
|
|
|
7
7
|
src?: string;
|
|
8
8
|
out: string;
|
|
9
9
|
defaultValue?: unknown;
|
|
10
|
-
onUpdate
|
|
10
|
+
onUpdate?: (newVal: any, oldVal: any) => any;
|
|
11
11
|
beforeOutput?: (val: any) => any;
|
|
12
12
|
}
|
|
13
13
|
declare function useLoad(name: string, options: UseLoadOptions): LoadInfo;
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,9 @@ import { glob } from "tinyglobby";
|
|
|
9
9
|
|
|
10
10
|
// src/utils.ts
|
|
11
11
|
var isDev = process.env.NODE_ENV === "development";
|
|
12
|
+
function capitalize(str) {
|
|
13
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
14
|
+
}
|
|
12
15
|
|
|
13
16
|
// src/core/processor.ts
|
|
14
17
|
var currentContext = null;
|
|
@@ -62,7 +65,7 @@ function createProcessor(sign, setup) {
|
|
|
62
65
|
return false;
|
|
63
66
|
}
|
|
64
67
|
outputLoads();
|
|
65
|
-
consola.success(`[${sign}] ${event} "${path}"`);
|
|
68
|
+
consola.success(`[${sign}] ${capitalize(event)} "${path}"`);
|
|
66
69
|
});
|
|
67
70
|
}
|
|
68
71
|
for (const info of ctx.loadInfos) {
|
|
@@ -73,7 +76,7 @@ function createProcessor(sign, setup) {
|
|
|
73
76
|
ignoreInitial: true
|
|
74
77
|
}).on("change", async () => {
|
|
75
78
|
const newVal = await fs.readJson(info.src);
|
|
76
|
-
info.value = info.onUpdate(newVal, info.value);
|
|
79
|
+
info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
|
|
77
80
|
info.output();
|
|
78
81
|
consola.success(`[${sign}] Change "${info.src}"`);
|
|
79
82
|
});
|
|
@@ -137,7 +140,7 @@ function useLoad(name, options) {
|
|
|
137
140
|
const src = options.src ? resolve(options.src) : void 0;
|
|
138
141
|
const out = resolve(options.out);
|
|
139
142
|
const {
|
|
140
|
-
defaultValue,
|
|
143
|
+
defaultValue = {},
|
|
141
144
|
onUpdate,
|
|
142
145
|
beforeOutput
|
|
143
146
|
} = options;
|
|
@@ -145,15 +148,15 @@ function useLoad(name, options) {
|
|
|
145
148
|
name,
|
|
146
149
|
src,
|
|
147
150
|
out,
|
|
148
|
-
value: src ? fs2.readJsonSync(src) : defaultValue
|
|
151
|
+
value: src ? fs2.readJsonSync(src) : defaultValue,
|
|
152
|
+
onUpdate,
|
|
149
153
|
output() {
|
|
150
154
|
const data = beforeOutput?.(info.value) ?? info.value;
|
|
151
|
-
fs2.outputJsonSync(
|
|
152
|
-
}
|
|
153
|
-
onUpdate
|
|
155
|
+
fs2.outputJsonSync(out, data);
|
|
156
|
+
}
|
|
154
157
|
};
|
|
155
158
|
ctx.loadInfos.push(info);
|
|
156
|
-
onUpdate(info.value, void 0);
|
|
159
|
+
onUpdate?.(info.value, void 0);
|
|
157
160
|
return info;
|
|
158
161
|
}
|
|
159
162
|
|
package/package.json
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kerria",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"author": "KazariEX",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": "KazariEX/kerria",
|
|
9
9
|
"keywords": [],
|
|
10
10
|
"exports": {
|
|
11
|
-
".":
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.js",
|
|
14
|
-
"require": "./dist/index.cjs"
|
|
15
|
-
}
|
|
11
|
+
".": "./dist/index.js"
|
|
16
12
|
},
|
|
17
|
-
"main": "./dist/index.
|
|
13
|
+
"main": "./dist/index.js",
|
|
18
14
|
"module": "./dist/index.js",
|
|
19
15
|
"types": "./dist/index.d.ts",
|
|
20
16
|
"files": [
|
|
@@ -35,7 +31,6 @@
|
|
|
35
31
|
"scripts": {
|
|
36
32
|
"build": "tsup-node",
|
|
37
33
|
"dev": "tsup-node --watch",
|
|
38
|
-
"release": "bumpp --no-push -c \"release: v%s\""
|
|
39
|
-
"test": "vitest"
|
|
34
|
+
"release": "bumpp --no-push -c \"release: v%s\""
|
|
40
35
|
}
|
|
41
36
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
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 __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
createProcessor: () => createProcessor,
|
|
34
|
-
useCurrentContext: () => useCurrentContext,
|
|
35
|
-
useLoad: () => useLoad,
|
|
36
|
-
useSource: () => useSource
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(index_exports);
|
|
39
|
-
|
|
40
|
-
// src/core/processor.ts
|
|
41
|
-
var import_chokidar = __toESM(require("chokidar"), 1);
|
|
42
|
-
var import_consola = __toESM(require("consola"), 1);
|
|
43
|
-
var import_crypto_es = __toESM(require("crypto-es"), 1);
|
|
44
|
-
var import_find_cache_dir = __toESM(require("find-cache-dir"), 1);
|
|
45
|
-
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
46
|
-
var import_pathe = require("pathe");
|
|
47
|
-
var import_tinyglobby = require("tinyglobby");
|
|
48
|
-
|
|
49
|
-
// src/utils.ts
|
|
50
|
-
var isDev = process.env.NODE_ENV === "development";
|
|
51
|
-
|
|
52
|
-
// src/core/processor.ts
|
|
53
|
-
var currentContext = null;
|
|
54
|
-
function useCurrentContext() {
|
|
55
|
-
return currentContext;
|
|
56
|
-
}
|
|
57
|
-
function createProcessor(sign, setup) {
|
|
58
|
-
const ctx = {
|
|
59
|
-
sign,
|
|
60
|
-
loadInfos: [],
|
|
61
|
-
sourceInfos: []
|
|
62
|
-
};
|
|
63
|
-
currentContext = ctx;
|
|
64
|
-
setup(ctx);
|
|
65
|
-
currentContext = null;
|
|
66
|
-
ctx.sourceInfos.sort((a, b) => {
|
|
67
|
-
return a.kind - b.kind;
|
|
68
|
-
});
|
|
69
|
-
const cacheDir = (0, import_find_cache_dir.default)({ name: "kerria" });
|
|
70
|
-
const cachePath = (0, import_pathe.join)(cacheDir, `${sign}.json`);
|
|
71
|
-
const caches = import_fs_extra.default.existsSync(cachePath) && import_fs_extra.default.readJsonSync(cachePath) || {};
|
|
72
|
-
async function build() {
|
|
73
|
-
for (const info of ctx.sourceInfos) {
|
|
74
|
-
const paths = await (0, import_tinyglobby.glob)(info.patterns, {
|
|
75
|
-
deep: info.deep ? Infinity : 2,
|
|
76
|
-
absolute: true
|
|
77
|
-
});
|
|
78
|
-
await Promise.all(
|
|
79
|
-
paths.map((path) => path.replaceAll("\\", "/")).filter(info.filter).sort((a, b) => a.localeCompare(b)).map((path) => parse(path, info))
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
outputLoads();
|
|
83
|
-
import_consola.default.success(`[${sign}] Build`);
|
|
84
|
-
}
|
|
85
|
-
async function watch() {
|
|
86
|
-
for (const info of ctx.sourceInfos) {
|
|
87
|
-
import_chokidar.default.watch(info.folders, {
|
|
88
|
-
depth: info.deep ? Infinity : 0,
|
|
89
|
-
ignoreInitial: true
|
|
90
|
-
}).on("all", async (event, filename) => {
|
|
91
|
-
const path = filename.replaceAll("\\", "/");
|
|
92
|
-
if (!path.endsWith(info.ext)) {
|
|
93
|
-
return false;
|
|
94
|
-
} else if (!info.filter(path)) {
|
|
95
|
-
return false;
|
|
96
|
-
} else if (event === "change" && !await parse(path, info)) {
|
|
97
|
-
return false;
|
|
98
|
-
} else if (event === "add" && !await add(path, info)) {
|
|
99
|
-
return false;
|
|
100
|
-
} else if (event === "unlink" && !unlink(path, info)) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
outputLoads();
|
|
104
|
-
import_consola.default.success(`[${sign}] ${event} "${path}"`);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
for (const info of ctx.loadInfos) {
|
|
108
|
-
if (!info.src) {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
import_chokidar.default.watch(info.src, {
|
|
112
|
-
ignoreInitial: true
|
|
113
|
-
}).on("change", async () => {
|
|
114
|
-
const newVal = await import_fs_extra.default.readJson(info.src);
|
|
115
|
-
info.value = info.onUpdate(newVal, info.value);
|
|
116
|
-
info.output();
|
|
117
|
-
import_consola.default.success(`[${sign}] Change "${info.src}"`);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
async function parse(path, info) {
|
|
122
|
-
const stats = await import_fs_extra.default.stat(path);
|
|
123
|
-
const hash = import_crypto_es.default.MD5(stats.size.toString()).toString();
|
|
124
|
-
let cache = caches[path];
|
|
125
|
-
if (isDev && cache?.hash === hash) {
|
|
126
|
-
info.onCacheHit?.(cache);
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
cache = { hash };
|
|
130
|
-
const data = await info.parse(path, info);
|
|
131
|
-
if (data !== null) {
|
|
132
|
-
cache = {
|
|
133
|
-
...cache,
|
|
134
|
-
...data ?? {}
|
|
135
|
-
};
|
|
136
|
-
info.onCacheHit?.(cache);
|
|
137
|
-
caches[path] = cache;
|
|
138
|
-
} else {
|
|
139
|
-
unlink(path, info);
|
|
140
|
-
}
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
async function add(path, info) {
|
|
144
|
-
const cache = caches[path];
|
|
145
|
-
if (cache) {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
return await parse(path, info);
|
|
149
|
-
}
|
|
150
|
-
function unlink(path, info) {
|
|
151
|
-
const cache = caches[path];
|
|
152
|
-
if (!cache) {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
info.unlink?.(cache);
|
|
156
|
-
delete caches[path];
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
function outputLoads() {
|
|
160
|
-
import_fs_extra.default.outputJsonSync(cachePath, caches);
|
|
161
|
-
for (const info of ctx.loadInfos) {
|
|
162
|
-
info.output();
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
build,
|
|
167
|
-
watch
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// src/core/useLoad.ts
|
|
172
|
-
var import_fs_extra2 = __toESM(require("fs-extra"), 1);
|
|
173
|
-
var import_pathe2 = require("pathe");
|
|
174
|
-
function useLoad(name, options) {
|
|
175
|
-
const ctx = useCurrentContext();
|
|
176
|
-
const src = options.src ? (0, import_pathe2.resolve)(options.src) : void 0;
|
|
177
|
-
const out = (0, import_pathe2.resolve)(options.out);
|
|
178
|
-
const {
|
|
179
|
-
defaultValue,
|
|
180
|
-
onUpdate,
|
|
181
|
-
beforeOutput
|
|
182
|
-
} = options;
|
|
183
|
-
const info = {
|
|
184
|
-
name,
|
|
185
|
-
src,
|
|
186
|
-
out,
|
|
187
|
-
value: src ? import_fs_extra2.default.readJsonSync(src) : defaultValue ?? {},
|
|
188
|
-
output() {
|
|
189
|
-
const data = beforeOutput?.(info.value) ?? info.value;
|
|
190
|
-
import_fs_extra2.default.outputJsonSync(info.out, data);
|
|
191
|
-
},
|
|
192
|
-
onUpdate
|
|
193
|
-
};
|
|
194
|
-
ctx.loadInfos.push(info);
|
|
195
|
-
onUpdate(info.value, void 0);
|
|
196
|
-
return info;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// src/core/useSource.ts
|
|
200
|
-
var import_fs_extra3 = __toESM(require("fs-extra"), 1);
|
|
201
|
-
var import_pathe3 = require("pathe");
|
|
202
|
-
function useSource(kind, options) {
|
|
203
|
-
const ctx = useCurrentContext();
|
|
204
|
-
const base = (0, import_pathe3.resolve)(options.base);
|
|
205
|
-
const dist = options.dist ? (0, import_pathe3.resolve)(options.dist) : void 0;
|
|
206
|
-
const folders = options.folders?.map((folder) => (0, import_pathe3.resolve)(base, folder)) ?? [base];
|
|
207
|
-
const patterns = folders.map((path) => (0, import_pathe3.resolve)(path, `**/*${options.ext}`));
|
|
208
|
-
const info = {
|
|
209
|
-
...options,
|
|
210
|
-
kind,
|
|
211
|
-
base,
|
|
212
|
-
dist,
|
|
213
|
-
folders,
|
|
214
|
-
patterns,
|
|
215
|
-
deep: options.deep ?? true,
|
|
216
|
-
skip: options.skip ?? 0,
|
|
217
|
-
filter(path) {
|
|
218
|
-
const depth = path.split("/").length - folders[0].split("/").length;
|
|
219
|
-
return info.skip < depth;
|
|
220
|
-
},
|
|
221
|
-
async output(path, data) {
|
|
222
|
-
const outPath = path.replace(base, dist).replace(info.ext, ".json");
|
|
223
|
-
await import_fs_extra3.default.outputJson(outPath, data);
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
ctx.sourceInfos.push(info);
|
|
227
|
-
return info;
|
|
228
|
-
}
|
|
229
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
-
0 && (module.exports = {
|
|
231
|
-
createProcessor,
|
|
232
|
-
useCurrentContext,
|
|
233
|
-
useLoad,
|
|
234
|
-
useSource
|
|
235
|
-
});
|
package/dist/index.d.cts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
interface LoadInfo extends Omit<UseLoadOptions, "defaultValue" | "beforeOutput"> {
|
|
2
|
-
name: string;
|
|
3
|
-
value: any;
|
|
4
|
-
output: () => void;
|
|
5
|
-
}
|
|
6
|
-
interface UseLoadOptions {
|
|
7
|
-
src?: string;
|
|
8
|
-
out: string;
|
|
9
|
-
defaultValue?: unknown;
|
|
10
|
-
onUpdate: (newVal: any, oldVal: any) => any;
|
|
11
|
-
beforeOutput?: (val: any) => any;
|
|
12
|
-
}
|
|
13
|
-
declare function useLoad(name: string, options: UseLoadOptions): LoadInfo;
|
|
14
|
-
|
|
15
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
16
|
-
|
|
17
|
-
interface SourceInfo extends Omit<UseSourceOptions, "folders"> {
|
|
18
|
-
kind: number;
|
|
19
|
-
folders: string[];
|
|
20
|
-
patterns: string[];
|
|
21
|
-
filter: (path: string) => boolean;
|
|
22
|
-
output: (path: string, data: any) => Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
interface UseSourceOptions<T = any> {
|
|
25
|
-
base: string;
|
|
26
|
-
dist?: string;
|
|
27
|
-
folders?: string[];
|
|
28
|
-
ext: string;
|
|
29
|
-
deep?: boolean;
|
|
30
|
-
skip?: number;
|
|
31
|
-
parse: (path: string, info: SourceInfo) => MaybePromise<T | null | void>;
|
|
32
|
-
unlink?: (cache: T) => void;
|
|
33
|
-
onCacheHit?: (cache: T) => void;
|
|
34
|
-
}
|
|
35
|
-
declare function useSource<C extends object>(kind: number, options: UseSourceOptions<C>): SourceInfo;
|
|
36
|
-
|
|
37
|
-
interface ProcessorContext {
|
|
38
|
-
sign: string;
|
|
39
|
-
loadInfos: LoadInfo[];
|
|
40
|
-
sourceInfos: SourceInfo[];
|
|
41
|
-
}
|
|
42
|
-
declare function useCurrentContext(): ProcessorContext;
|
|
43
|
-
declare function createProcessor(sign: string, setup: (ctx: ProcessorContext) => void): {
|
|
44
|
-
build: () => Promise<void>;
|
|
45
|
-
watch: () => Promise<void>;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export { type LoadInfo, type SourceInfo, type UseLoadOptions, createProcessor, useCurrentContext, useLoad, useSource };
|