kerria 0.3.3 → 0.4.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 CHANGED
@@ -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,12 +32,12 @@ 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
- //#region src/core/processor.d.ts
40
+ //#region src/core/kerria.d.ts
41
41
  interface KerriaContext {
42
42
  sign: string;
43
43
  loadInfos: LoadInfo[];
@@ -49,4 +49,4 @@ declare function createKerria(sign: string, setup: (ctx: KerriaContext) => void)
49
49
  watch: () => Promise<void>;
50
50
  };
51
51
  //#endregion
52
- export { LoadInfo, SourceInfo, UseLoadOptions, createKerria, useCurrentContext, useLoad, useSource };
52
+ export { KerriaContext, LoadInfo, SourceInfo, UseLoadOptions, UseSourceOptions, createKerria, useCurrentContext, useLoad, useSource };
package/dist/index.js CHANGED
@@ -4,9 +4,8 @@ import { readFile, stat, writeFile } from "node:fs/promises";
4
4
  import chokidar from "chokidar";
5
5
  import consola from "consola";
6
6
  import * as pkg from "empathic/package";
7
- import { join, resolve } from "pathe";
7
+ import { dirname, join, resolve } from "pathe";
8
8
  import { glob } from "tinyglobby";
9
- import { dirname } from "node:path";
10
9
 
11
10
  //#region src/utils.ts
12
11
  const isDev = process.env.NODE_ENV === "development";
@@ -33,7 +32,7 @@ function writeJsonSync(path, data) {
33
32
  }
34
33
 
35
34
  //#endregion
36
- //#region src/core/processor.ts
35
+ //#region src/core/kerria.ts
37
36
  let currentContext = null;
38
37
  function useCurrentContext() {
39
38
  return currentContext;
@@ -79,7 +78,7 @@ function createKerria(sign, setup) {
79
78
  if (!info.src) continue;
80
79
  chokidar.watch(info.src, { ignoreInitial: true }).on("change", async () => {
81
80
  const newVal = await readJson(info.src);
82
- info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
81
+ info.value = info.update?.(newVal, info.value) ?? newVal;
83
82
  info.output();
84
83
  consola.success(`[${sign}] Change "${info.src}"`);
85
84
  });
@@ -90,17 +89,16 @@ function createKerria(sign, setup) {
90
89
  const hash = createHash("md5").update(stats.mtimeMs.toString()).digest("hex");
91
90
  let cache = caches[path];
92
91
  if (isDev && cache?.hash === hash) {
93
- info.onCacheHit?.(cache);
92
+ info.cache?.(cache);
94
93
  return false;
95
94
  }
96
- cache = { hash };
97
95
  const data = await info.parse(path, info);
98
96
  if (data !== null) {
99
97
  cache = {
100
- ...cache,
101
- ...data ?? {}
98
+ hash,
99
+ ...data
102
100
  };
103
- info.onCacheHit?.(cache);
101
+ info.cache?.(cache);
104
102
  caches[path] = cache;
105
103
  } else unlink(path, info);
106
104
  return true;
@@ -132,7 +130,7 @@ function createKerria(sign, setup) {
132
130
  //#region src/core/useLoad.ts
133
131
  function useLoad(name, options) {
134
132
  const ctx = useCurrentContext();
135
- const { defaultValue = {}, onUpdate, beforeOutput } = options;
133
+ const { defaultValue = {}, update, output } = options;
136
134
  const src = options.src ? resolve(options.src) : void 0;
137
135
  const out = resolve(options.out);
138
136
  const info = {
@@ -140,14 +138,14 @@ function useLoad(name, options) {
140
138
  src,
141
139
  out,
142
140
  value: src ? readJsonSync(src) : defaultValue,
143
- onUpdate,
141
+ update,
144
142
  output() {
145
- const data = beforeOutput?.(info.value) ?? info.value;
143
+ const data = output?.(info.value) ?? info.value;
146
144
  writeJsonSync(out, data);
147
145
  }
148
146
  };
149
147
  ctx.loadInfos.push(info);
150
- onUpdate?.(info.value, void 0);
148
+ update?.(info.value, void 0);
151
149
  return info;
152
150
  }
153
151
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kerria",
3
3
  "type": "module",
4
- "version": "0.3.3",
4
+ "version": "0.4.0",
5
5
  "description": "Composable source processor",
6
6
  "author": "KazariEX",
7
7
  "license": "MIT",
@@ -19,18 +19,17 @@
19
19
  "dependencies": {
20
20
  "chokidar": "^4.0.3",
21
21
  "consola": "^3.4.2",
22
- "empathic": "^1.1.0",
22
+ "empathic": "^2.0.0",
23
23
  "pathe": "^2.0.3",
24
24
  "tinyglobby": "^0.2.14"
25
25
  },
26
26
  "devDependencies": {
27
- "@antfu/eslint-config": "^4.13.2",
28
- "@types/node": "^22.15.29",
29
- "@zinkawaii/eslint-config": "^0.3.0",
27
+ "@types/node": "^24.3.0",
28
+ "@zinkawaii/eslint-config": "^0.4.1",
30
29
  "@zinkawaii/tsconfig": "^0.0.2",
31
- "bumpp": "^10.1.1",
32
- "eslint": "^9.28.0",
33
- "tsdown": "^0.12.5"
30
+ "bumpp": "^10.2.3",
31
+ "eslint": "^9.33.0",
32
+ "tsdown": "^0.14.1"
34
33
  },
35
34
  "scripts": {
36
35
  "build": "tsdown",