kerria 0.1.0 → 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/package.json +3 -7
- package/dist/index.cjs +0 -238
- package/dist/index.d.cts +0 -48
package/package.json
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kerria",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "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": [
|
package/dist/index.cjs
DELETED
|
@@ -1,238 +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
|
-
function capitalize(str) {
|
|
52
|
-
return str[0].toUpperCase() + str.slice(1);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// src/core/processor.ts
|
|
56
|
-
var currentContext = null;
|
|
57
|
-
function useCurrentContext() {
|
|
58
|
-
return currentContext;
|
|
59
|
-
}
|
|
60
|
-
function createProcessor(sign, setup) {
|
|
61
|
-
const ctx = {
|
|
62
|
-
sign,
|
|
63
|
-
loadInfos: [],
|
|
64
|
-
sourceInfos: []
|
|
65
|
-
};
|
|
66
|
-
currentContext = ctx;
|
|
67
|
-
setup(ctx);
|
|
68
|
-
currentContext = null;
|
|
69
|
-
ctx.sourceInfos.sort((a, b) => {
|
|
70
|
-
return a.kind - b.kind;
|
|
71
|
-
});
|
|
72
|
-
const cacheDir = (0, import_find_cache_dir.default)({ name: "kerria" });
|
|
73
|
-
const cachePath = (0, import_pathe.join)(cacheDir, `${sign}.json`);
|
|
74
|
-
const caches = import_fs_extra.default.existsSync(cachePath) && import_fs_extra.default.readJsonSync(cachePath) || {};
|
|
75
|
-
async function build() {
|
|
76
|
-
for (const info of ctx.sourceInfos) {
|
|
77
|
-
const paths = await (0, import_tinyglobby.glob)(info.patterns, {
|
|
78
|
-
deep: info.deep ? Infinity : 2,
|
|
79
|
-
absolute: true
|
|
80
|
-
});
|
|
81
|
-
await Promise.all(
|
|
82
|
-
paths.map((path) => path.replaceAll("\\", "/")).filter(info.filter).sort((a, b) => a.localeCompare(b)).map((path) => parse(path, info))
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
outputLoads();
|
|
86
|
-
import_consola.default.success(`[${sign}] Build`);
|
|
87
|
-
}
|
|
88
|
-
async function watch() {
|
|
89
|
-
for (const info of ctx.sourceInfos) {
|
|
90
|
-
import_chokidar.default.watch(info.folders, {
|
|
91
|
-
depth: info.deep ? Infinity : 0,
|
|
92
|
-
ignoreInitial: true
|
|
93
|
-
}).on("all", async (event, filename) => {
|
|
94
|
-
const path = filename.replaceAll("\\", "/");
|
|
95
|
-
if (!path.endsWith(info.ext)) {
|
|
96
|
-
return false;
|
|
97
|
-
} else if (!info.filter(path)) {
|
|
98
|
-
return false;
|
|
99
|
-
} else if (event === "change" && !await parse(path, info)) {
|
|
100
|
-
return false;
|
|
101
|
-
} else if (event === "add" && !await add(path, info)) {
|
|
102
|
-
return false;
|
|
103
|
-
} else if (event === "unlink" && !unlink(path, info)) {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
outputLoads();
|
|
107
|
-
import_consola.default.success(`[${sign}] ${capitalize(event)} "${path}"`);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
for (const info of ctx.loadInfos) {
|
|
111
|
-
if (!info.src) {
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
import_chokidar.default.watch(info.src, {
|
|
115
|
-
ignoreInitial: true
|
|
116
|
-
}).on("change", async () => {
|
|
117
|
-
const newVal = await import_fs_extra.default.readJson(info.src);
|
|
118
|
-
info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
|
|
119
|
-
info.output();
|
|
120
|
-
import_consola.default.success(`[${sign}] Change "${info.src}"`);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
async function parse(path, info) {
|
|
125
|
-
const stats = await import_fs_extra.default.stat(path);
|
|
126
|
-
const hash = import_crypto_es.default.MD5(stats.size.toString()).toString();
|
|
127
|
-
let cache = caches[path];
|
|
128
|
-
if (isDev && cache?.hash === hash) {
|
|
129
|
-
info.onCacheHit?.(cache);
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
cache = { hash };
|
|
133
|
-
const data = await info.parse(path, info);
|
|
134
|
-
if (data !== null) {
|
|
135
|
-
cache = {
|
|
136
|
-
...cache,
|
|
137
|
-
...data ?? {}
|
|
138
|
-
};
|
|
139
|
-
info.onCacheHit?.(cache);
|
|
140
|
-
caches[path] = cache;
|
|
141
|
-
} else {
|
|
142
|
-
unlink(path, info);
|
|
143
|
-
}
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
async function add(path, info) {
|
|
147
|
-
const cache = caches[path];
|
|
148
|
-
if (cache) {
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
return await parse(path, info);
|
|
152
|
-
}
|
|
153
|
-
function unlink(path, info) {
|
|
154
|
-
const cache = caches[path];
|
|
155
|
-
if (!cache) {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
info.unlink?.(cache);
|
|
159
|
-
delete caches[path];
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
function outputLoads() {
|
|
163
|
-
import_fs_extra.default.outputJsonSync(cachePath, caches);
|
|
164
|
-
for (const info of ctx.loadInfos) {
|
|
165
|
-
info.output();
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
build,
|
|
170
|
-
watch
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// src/core/useLoad.ts
|
|
175
|
-
var import_fs_extra2 = __toESM(require("fs-extra"), 1);
|
|
176
|
-
var import_pathe2 = require("pathe");
|
|
177
|
-
function useLoad(name, options) {
|
|
178
|
-
const ctx = useCurrentContext();
|
|
179
|
-
const src = options.src ? (0, import_pathe2.resolve)(options.src) : void 0;
|
|
180
|
-
const out = (0, import_pathe2.resolve)(options.out);
|
|
181
|
-
const {
|
|
182
|
-
defaultValue = {},
|
|
183
|
-
onUpdate,
|
|
184
|
-
beforeOutput
|
|
185
|
-
} = options;
|
|
186
|
-
const info = {
|
|
187
|
-
name,
|
|
188
|
-
src,
|
|
189
|
-
out,
|
|
190
|
-
value: src ? import_fs_extra2.default.readJsonSync(src) : defaultValue,
|
|
191
|
-
onUpdate,
|
|
192
|
-
output() {
|
|
193
|
-
const data = beforeOutput?.(info.value) ?? info.value;
|
|
194
|
-
import_fs_extra2.default.outputJsonSync(out, data);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
ctx.loadInfos.push(info);
|
|
198
|
-
onUpdate?.(info.value, void 0);
|
|
199
|
-
return info;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// src/core/useSource.ts
|
|
203
|
-
var import_fs_extra3 = __toESM(require("fs-extra"), 1);
|
|
204
|
-
var import_pathe3 = require("pathe");
|
|
205
|
-
function useSource(kind, options) {
|
|
206
|
-
const ctx = useCurrentContext();
|
|
207
|
-
const base = (0, import_pathe3.resolve)(options.base);
|
|
208
|
-
const dist = options.dist ? (0, import_pathe3.resolve)(options.dist) : void 0;
|
|
209
|
-
const folders = options.folders?.map((folder) => (0, import_pathe3.resolve)(base, folder)) ?? [base];
|
|
210
|
-
const patterns = folders.map((path) => (0, import_pathe3.resolve)(path, `**/*${options.ext}`));
|
|
211
|
-
const info = {
|
|
212
|
-
...options,
|
|
213
|
-
kind,
|
|
214
|
-
base,
|
|
215
|
-
dist,
|
|
216
|
-
folders,
|
|
217
|
-
patterns,
|
|
218
|
-
deep: options.deep ?? true,
|
|
219
|
-
skip: options.skip ?? 0,
|
|
220
|
-
filter(path) {
|
|
221
|
-
const depth = path.split("/").length - folders[0].split("/").length;
|
|
222
|
-
return info.skip < depth;
|
|
223
|
-
},
|
|
224
|
-
async output(path, data) {
|
|
225
|
-
const outPath = path.replace(base, dist).replace(info.ext, ".json");
|
|
226
|
-
await import_fs_extra3.default.outputJson(outPath, data);
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
ctx.sourceInfos.push(info);
|
|
230
|
-
return info;
|
|
231
|
-
}
|
|
232
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
233
|
-
0 && (module.exports = {
|
|
234
|
-
createProcessor,
|
|
235
|
-
useCurrentContext,
|
|
236
|
-
useLoad,
|
|
237
|
-
useSource
|
|
238
|
-
});
|
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 };
|