kerria 0.0.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/LICENSE +21 -0
- package/dist/index.cjs +235 -0
- package/dist/index.d.cts +48 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +195 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present KazariEX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// src/core/processor.ts
|
|
2
|
+
import chokidar from "chokidar";
|
|
3
|
+
import consola from "consola";
|
|
4
|
+
import CryptoES from "crypto-es";
|
|
5
|
+
import findCacheDir from "find-cache-dir";
|
|
6
|
+
import fs from "fs-extra";
|
|
7
|
+
import { join } from "pathe";
|
|
8
|
+
import { glob } from "tinyglobby";
|
|
9
|
+
|
|
10
|
+
// src/utils.ts
|
|
11
|
+
var isDev = process.env.NODE_ENV === "development";
|
|
12
|
+
|
|
13
|
+
// src/core/processor.ts
|
|
14
|
+
var currentContext = null;
|
|
15
|
+
function useCurrentContext() {
|
|
16
|
+
return currentContext;
|
|
17
|
+
}
|
|
18
|
+
function createProcessor(sign, setup) {
|
|
19
|
+
const ctx = {
|
|
20
|
+
sign,
|
|
21
|
+
loadInfos: [],
|
|
22
|
+
sourceInfos: []
|
|
23
|
+
};
|
|
24
|
+
currentContext = ctx;
|
|
25
|
+
setup(ctx);
|
|
26
|
+
currentContext = null;
|
|
27
|
+
ctx.sourceInfos.sort((a, b) => {
|
|
28
|
+
return a.kind - b.kind;
|
|
29
|
+
});
|
|
30
|
+
const cacheDir = findCacheDir({ name: "kerria" });
|
|
31
|
+
const cachePath = join(cacheDir, `${sign}.json`);
|
|
32
|
+
const caches = fs.existsSync(cachePath) && fs.readJsonSync(cachePath) || {};
|
|
33
|
+
async function build() {
|
|
34
|
+
for (const info of ctx.sourceInfos) {
|
|
35
|
+
const paths = await glob(info.patterns, {
|
|
36
|
+
deep: info.deep ? Infinity : 2,
|
|
37
|
+
absolute: true
|
|
38
|
+
});
|
|
39
|
+
await Promise.all(
|
|
40
|
+
paths.map((path) => path.replaceAll("\\", "/")).filter(info.filter).sort((a, b) => a.localeCompare(b)).map((path) => parse(path, info))
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
outputLoads();
|
|
44
|
+
consola.success(`[${sign}] Build`);
|
|
45
|
+
}
|
|
46
|
+
async function watch() {
|
|
47
|
+
for (const info of ctx.sourceInfos) {
|
|
48
|
+
chokidar.watch(info.folders, {
|
|
49
|
+
depth: info.deep ? Infinity : 0,
|
|
50
|
+
ignoreInitial: true
|
|
51
|
+
}).on("all", async (event, filename) => {
|
|
52
|
+
const path = filename.replaceAll("\\", "/");
|
|
53
|
+
if (!path.endsWith(info.ext)) {
|
|
54
|
+
return false;
|
|
55
|
+
} else if (!info.filter(path)) {
|
|
56
|
+
return false;
|
|
57
|
+
} else if (event === "change" && !await parse(path, info)) {
|
|
58
|
+
return false;
|
|
59
|
+
} else if (event === "add" && !await add(path, info)) {
|
|
60
|
+
return false;
|
|
61
|
+
} else if (event === "unlink" && !unlink(path, info)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
outputLoads();
|
|
65
|
+
consola.success(`[${sign}] ${event} "${path}"`);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
for (const info of ctx.loadInfos) {
|
|
69
|
+
if (!info.src) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
chokidar.watch(info.src, {
|
|
73
|
+
ignoreInitial: true
|
|
74
|
+
}).on("change", async () => {
|
|
75
|
+
const newVal = await fs.readJson(info.src);
|
|
76
|
+
info.value = info.onUpdate(newVal, info.value);
|
|
77
|
+
info.output();
|
|
78
|
+
consola.success(`[${sign}] Change "${info.src}"`);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function parse(path, info) {
|
|
83
|
+
const stats = await fs.stat(path);
|
|
84
|
+
const hash = CryptoES.MD5(stats.size.toString()).toString();
|
|
85
|
+
let cache = caches[path];
|
|
86
|
+
if (isDev && cache?.hash === hash) {
|
|
87
|
+
info.onCacheHit?.(cache);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
cache = { hash };
|
|
91
|
+
const data = await info.parse(path, info);
|
|
92
|
+
if (data !== null) {
|
|
93
|
+
cache = {
|
|
94
|
+
...cache,
|
|
95
|
+
...data ?? {}
|
|
96
|
+
};
|
|
97
|
+
info.onCacheHit?.(cache);
|
|
98
|
+
caches[path] = cache;
|
|
99
|
+
} else {
|
|
100
|
+
unlink(path, info);
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
async function add(path, info) {
|
|
105
|
+
const cache = caches[path];
|
|
106
|
+
if (cache) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return await parse(path, info);
|
|
110
|
+
}
|
|
111
|
+
function unlink(path, info) {
|
|
112
|
+
const cache = caches[path];
|
|
113
|
+
if (!cache) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
info.unlink?.(cache);
|
|
117
|
+
delete caches[path];
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
function outputLoads() {
|
|
121
|
+
fs.outputJsonSync(cachePath, caches);
|
|
122
|
+
for (const info of ctx.loadInfos) {
|
|
123
|
+
info.output();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
build,
|
|
128
|
+
watch
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/core/useLoad.ts
|
|
133
|
+
import fs2 from "fs-extra";
|
|
134
|
+
import { resolve } from "pathe";
|
|
135
|
+
function useLoad(name, options) {
|
|
136
|
+
const ctx = useCurrentContext();
|
|
137
|
+
const src = options.src ? resolve(options.src) : void 0;
|
|
138
|
+
const out = resolve(options.out);
|
|
139
|
+
const {
|
|
140
|
+
defaultValue,
|
|
141
|
+
onUpdate,
|
|
142
|
+
beforeOutput
|
|
143
|
+
} = options;
|
|
144
|
+
const info = {
|
|
145
|
+
name,
|
|
146
|
+
src,
|
|
147
|
+
out,
|
|
148
|
+
value: src ? fs2.readJsonSync(src) : defaultValue ?? {},
|
|
149
|
+
output() {
|
|
150
|
+
const data = beforeOutput?.(info.value) ?? info.value;
|
|
151
|
+
fs2.outputJsonSync(info.out, data);
|
|
152
|
+
},
|
|
153
|
+
onUpdate
|
|
154
|
+
};
|
|
155
|
+
ctx.loadInfos.push(info);
|
|
156
|
+
onUpdate(info.value, void 0);
|
|
157
|
+
return info;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/core/useSource.ts
|
|
161
|
+
import fs3 from "fs-extra";
|
|
162
|
+
import { resolve as resolve2 } from "pathe";
|
|
163
|
+
function useSource(kind, options) {
|
|
164
|
+
const ctx = useCurrentContext();
|
|
165
|
+
const base = resolve2(options.base);
|
|
166
|
+
const dist = options.dist ? resolve2(options.dist) : void 0;
|
|
167
|
+
const folders = options.folders?.map((folder) => resolve2(base, folder)) ?? [base];
|
|
168
|
+
const patterns = folders.map((path) => resolve2(path, `**/*${options.ext}`));
|
|
169
|
+
const info = {
|
|
170
|
+
...options,
|
|
171
|
+
kind,
|
|
172
|
+
base,
|
|
173
|
+
dist,
|
|
174
|
+
folders,
|
|
175
|
+
patterns,
|
|
176
|
+
deep: options.deep ?? true,
|
|
177
|
+
skip: options.skip ?? 0,
|
|
178
|
+
filter(path) {
|
|
179
|
+
const depth = path.split("/").length - folders[0].split("/").length;
|
|
180
|
+
return info.skip < depth;
|
|
181
|
+
},
|
|
182
|
+
async output(path, data) {
|
|
183
|
+
const outPath = path.replace(base, dist).replace(info.ext, ".json");
|
|
184
|
+
await fs3.outputJson(outPath, data);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
ctx.sourceInfos.push(info);
|
|
188
|
+
return info;
|
|
189
|
+
}
|
|
190
|
+
export {
|
|
191
|
+
createProcessor,
|
|
192
|
+
useCurrentContext,
|
|
193
|
+
useLoad,
|
|
194
|
+
useSource
|
|
195
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kerria",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "",
|
|
6
|
+
"author": "KazariEX",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": "KazariEX/kerria",
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"chokidar": "^4.0.3",
|
|
25
|
+
"consola": "^3.4.0",
|
|
26
|
+
"crypto-es": "^2.1.0",
|
|
27
|
+
"find-cache-dir": "^5.0.0",
|
|
28
|
+
"fs-extra": "^11.3.0",
|
|
29
|
+
"pathe": "^2.0.2",
|
|
30
|
+
"tinyglobby": "^0.2.10"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/fs-extra": "^11.0.4"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup-node",
|
|
37
|
+
"dev": "tsup-node --watch",
|
|
38
|
+
"release": "bumpp --no-push -c \"release: v%s\"",
|
|
39
|
+
"test": "vitest"
|
|
40
|
+
}
|
|
41
|
+
}
|