kerria 0.3.4 → 0.4.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.
@@ -1,5 +1,5 @@
1
1
  //#region src/core/useLoad.d.ts
2
- interface LoadInfo extends Omit<UseLoadOptions, "defaultValue" | "beforeOutput"> {
2
+ interface LoadInfo extends Omit<UseLoadOptions, "defaultValue" | "output"> {
3
3
  name: string;
4
4
  value: any;
5
5
  output: () => void;
@@ -8,8 +8,8 @@ interface UseLoadOptions {
8
8
  src?: string;
9
9
  out: string;
10
10
  defaultValue?: unknown;
11
- onUpdate?: (newVal: any, oldVal: any) => any;
12
- beforeOutput?: (val: any) => any;
11
+ update?: (newVal: any, oldVal: any) => any;
12
+ output?: (val: any) => any;
13
13
  }
14
14
  declare function useLoad(name: string, options: UseLoadOptions): LoadInfo;
15
15
  //#endregion
@@ -32,10 +32,10 @@ interface UseSourceOptions<T = any> {
32
32
  deep?: boolean;
33
33
  skip?: number;
34
34
  parse: (path: string, info: SourceInfo) => MaybePromise<T | null | void>;
35
+ cache?: (cache: T) => void;
35
36
  unlink?: (cache: T) => void;
36
- onCacheHit?: (cache: T) => void;
37
37
  }
38
- declare function useSource<C extends object>(kind: number, options: UseSourceOptions<C>): SourceInfo;
38
+ declare function useSource<T extends object>(kind: number, options: UseSourceOptions<T>): SourceInfo;
39
39
  //#endregion
40
40
  //#region src/core/kerria.d.ts
41
41
  interface KerriaContext {
@@ -46,7 +46,7 @@ interface KerriaContext {
46
46
  declare function useCurrentContext(): KerriaContext;
47
47
  declare function createKerria(sign: string, setup: (ctx: KerriaContext) => void): {
48
48
  build: () => Promise<void>;
49
- watch: () => Promise<void>;
49
+ watch: () => () => void;
50
50
  };
51
51
  //#endregion
52
52
  export { KerriaContext, LoadInfo, SourceInfo, UseLoadOptions, UseSourceOptions, createKerria, useCurrentContext, useLoad, useSource };
@@ -22,13 +22,11 @@ function readJsonSync(path) {
22
22
  }
23
23
  function writeJson(path, data) {
24
24
  mkdirSync(dirname(path), { recursive: true });
25
- const json = JSON.stringify(data);
26
- return writeFile(path, json);
25
+ return writeFile(path, JSON.stringify(data));
27
26
  }
28
27
  function writeJsonSync(path, data) {
29
28
  mkdirSync(dirname(path), { recursive: true });
30
- const json = JSON.stringify(data);
31
- return writeFileSync(path, json);
29
+ return writeFileSync(path, JSON.stringify(data));
32
30
  }
33
31
 
34
32
  //#endregion
@@ -46,11 +44,8 @@ function createKerria(sign, setup) {
46
44
  currentContext = ctx;
47
45
  setup(ctx);
48
46
  currentContext = null;
49
- ctx.sourceInfos.sort((a, b) => {
50
- return a.kind - b.kind;
51
- });
52
- const cacheDir = pkg.cache("kerria", { create: true });
53
- const cachePath = join(cacheDir, `${sign}.json`);
47
+ ctx.sourceInfos.sort((a, b) => a.kind - b.kind);
48
+ const cachePath = join(pkg.cache("kerria", { create: true }), `${sign}.json`);
54
49
  const caches = existsSync(cachePath) && readJsonSync(cachePath) || {};
55
50
  async function build() {
56
51
  for (const info of ctx.sourceInfos) {
@@ -60,36 +55,46 @@ function createKerria(sign, setup) {
60
55
  outputLoads();
61
56
  consola.success(`[${sign}] Build`);
62
57
  }
63
- async function watch() {
64
- for (const info of ctx.sourceInfos) chokidar.watch(info.folders, {
65
- depth: info.deep ? Infinity : 0,
66
- ignoreInitial: true
67
- }).on("all", async (event, filename) => {
68
- const path = filename.replaceAll("\\", "/");
69
- if (!path.endsWith(info.ext)) return false;
70
- else if (!info.filter(path)) return false;
71
- else if (event === "change" && !await parse(path, info)) return false;
72
- else if (event === "add" && !await add(path, info)) return false;
73
- else if (event === "unlink" && !unlink(path, info)) return false;
74
- outputLoads();
75
- consola.success(`[${sign}] ${capitalize(event)} "${path}"`);
76
- });
58
+ function watch() {
59
+ const watchers = [];
60
+ for (const info of ctx.sourceInfos) {
61
+ const watcher = chokidar.watch(info.folders, {
62
+ depth: info.deep ? Infinity : 0,
63
+ ignoreInitial: true
64
+ });
65
+ watchers.push(watcher);
66
+ watcher.on("all", async (event, filename) => {
67
+ const path = filename.replaceAll("\\", "/");
68
+ if (!path.endsWith(info.ext)) return false;
69
+ else if (!info.filter(path)) return false;
70
+ else if (event === "change" && !await parse(path, info)) return false;
71
+ else if (event === "add" && !await add(path, info)) return false;
72
+ else if (event === "unlink" && !unlink(path, info)) return false;
73
+ outputLoads();
74
+ consola.success(`[${sign}] ${capitalize(event)} "${path}"`);
75
+ });
76
+ }
77
77
  for (const info of ctx.loadInfos) {
78
78
  if (!info.src) continue;
79
- chokidar.watch(info.src, { ignoreInitial: true }).on("change", async () => {
79
+ const watcher = chokidar.watch(info.src, { ignoreInitial: true });
80
+ watchers.push(watcher);
81
+ watcher.on("change", async () => {
80
82
  const newVal = await readJson(info.src);
81
- info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
83
+ info.value = info.update?.(newVal, info.value) ?? newVal;
82
84
  info.output();
83
85
  consola.success(`[${sign}] Change "${info.src}"`);
84
86
  });
85
87
  }
88
+ return () => {
89
+ for (const watcher of watchers) watcher.close();
90
+ };
86
91
  }
87
92
  async function parse(path, info) {
88
93
  const stats = await stat(path);
89
94
  const hash = createHash("md5").update(stats.mtimeMs.toString()).digest("hex");
90
95
  let cache = caches[path];
91
96
  if (isDev && cache?.hash === hash) {
92
- info.onCacheHit?.(cache);
97
+ info.cache?.(cache);
93
98
  return false;
94
99
  }
95
100
  const data = await info.parse(path, info);
@@ -98,14 +103,13 @@ function createKerria(sign, setup) {
98
103
  hash,
99
104
  ...data
100
105
  };
101
- info.onCacheHit?.(cache);
106
+ info.cache?.(cache);
102
107
  caches[path] = cache;
103
108
  } else unlink(path, info);
104
109
  return true;
105
110
  }
106
111
  async function add(path, info) {
107
- const cache = caches[path];
108
- if (cache) return false;
112
+ if (caches[path]) return false;
109
113
  return await parse(path, info);
110
114
  }
111
115
  function unlink(path, info) {
@@ -130,7 +134,7 @@ function createKerria(sign, setup) {
130
134
  //#region src/core/useLoad.ts
131
135
  function useLoad(name, options) {
132
136
  const ctx = useCurrentContext();
133
- const { defaultValue = {}, onUpdate, beforeOutput } = options;
137
+ const { defaultValue = {}, update, output } = options;
134
138
  const src = options.src ? resolve(options.src) : void 0;
135
139
  const out = resolve(options.out);
136
140
  const info = {
@@ -138,14 +142,13 @@ function useLoad(name, options) {
138
142
  src,
139
143
  out,
140
144
  value: src ? readJsonSync(src) : defaultValue,
141
- onUpdate,
145
+ update,
142
146
  output() {
143
- const data = beforeOutput?.(info.value) ?? info.value;
144
- writeJsonSync(out, data);
147
+ writeJsonSync(out, output?.(info.value) ?? info.value);
145
148
  }
146
149
  };
147
150
  ctx.loadInfos.push(info);
148
- onUpdate?.(info.value, void 0);
151
+ update?.(info.value, void 0);
149
152
  return info;
150
153
  }
151
154
 
@@ -168,12 +171,10 @@ function useSource(kind, options) {
168
171
  deep,
169
172
  skip,
170
173
  filter(path) {
171
- const depth = path.split("/").length - folders[0].split("/").length;
172
- return skip < depth;
174
+ return skip < path.split("/").length - folders[0].split("/").length;
173
175
  },
174
176
  async output(path, data) {
175
- const outPath = path.replace(base, dist).replace(info.ext, ".json");
176
- await writeJson(outPath, data);
177
+ await writeJson(path.replace(base, dist).replace(info.ext, ".json"), data);
177
178
  }
178
179
  };
179
180
  ctx.sourceInfos.push(info);
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
1
  {
2
2
  "name": "kerria",
3
3
  "type": "module",
4
- "version": "0.3.4",
4
+ "version": "0.4.1",
5
5
  "description": "Composable source processor",
6
6
  "author": "KazariEX",
7
7
  "license": "MIT",
8
8
  "repository": "KazariEX/kerria",
9
9
  "keywords": [],
10
10
  "exports": {
11
- ".": "./dist/index.js"
11
+ ".": "./dist/index.mjs",
12
+ "./package.json": "./package.json"
12
13
  },
13
- "main": "./dist/index.js",
14
- "module": "./dist/index.js",
15
- "types": "./dist/index.d.ts",
14
+ "main": "./dist/index.mjs",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.mts",
16
17
  "files": [
17
18
  "dist"
18
19
  ],
19
20
  "dependencies": {
20
- "chokidar": "^4.0.3",
21
+ "chokidar": "^5.0.0",
21
22
  "consola": "^3.4.2",
22
23
  "empathic": "^2.0.0",
23
24
  "pathe": "^2.0.3",
24
- "tinyglobby": "^0.2.14"
25
+ "tinyglobby": "^0.2.15"
25
26
  },
26
27
  "devDependencies": {
27
- "@antfu/eslint-config": "^4.16.2",
28
- "@types/node": "^24.0.10",
29
- "@zinkawaii/eslint-config": "^0.3.0",
28
+ "@types/node": "^25.0.3",
29
+ "@zinkawaii/eslint-config": "^0.4.1",
30
30
  "@zinkawaii/tsconfig": "^0.0.2",
31
- "bumpp": "^10.2.0",
32
- "eslint": "^9.30.1",
33
- "tsdown": "^0.12.9"
31
+ "bumpp": "^10.3.2",
32
+ "eslint": "^9.39.2",
33
+ "tsdown": "^0.18.4"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsdown",