kerria 0.0.1 → 0.1.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 ADDED
@@ -0,0 +1,17 @@
1
+ # Kerria
2
+
3
+ [![version](https://img.shields.io/npm/v/kerria?color=FFEF78&labelColor=E48748&label=npm)](https://www.npmjs.com/package/kerria)
4
+ [![downloads](https://img.shields.io/npm/dm/kerria?color=FFEF78&labelColor=E48748&label=downloads)](https://www.npmjs.com/package/kerria)
5
+ [![license](https://img.shields.io/npm/l/kerria?color=FFEF78&labelColor=E48748&label=license)](/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.cjs CHANGED
@@ -48,6 +48,9 @@ var import_tinyglobby = require("tinyglobby");
48
48
 
49
49
  // src/utils.ts
50
50
  var isDev = process.env.NODE_ENV === "development";
51
+ function capitalize(str) {
52
+ return str[0].toUpperCase() + str.slice(1);
53
+ }
51
54
 
52
55
  // src/core/processor.ts
53
56
  var currentContext = null;
@@ -101,7 +104,7 @@ function createProcessor(sign, setup) {
101
104
  return false;
102
105
  }
103
106
  outputLoads();
104
- import_consola.default.success(`[${sign}] ${event} "${path}"`);
107
+ import_consola.default.success(`[${sign}] ${capitalize(event)} "${path}"`);
105
108
  });
106
109
  }
107
110
  for (const info of ctx.loadInfos) {
@@ -112,7 +115,7 @@ function createProcessor(sign, setup) {
112
115
  ignoreInitial: true
113
116
  }).on("change", async () => {
114
117
  const newVal = await import_fs_extra.default.readJson(info.src);
115
- info.value = info.onUpdate(newVal, info.value);
118
+ info.value = info.onUpdate?.(newVal, info.value) ?? newVal;
116
119
  info.output();
117
120
  import_consola.default.success(`[${sign}] Change "${info.src}"`);
118
121
  });
@@ -176,7 +179,7 @@ function useLoad(name, options) {
176
179
  const src = options.src ? (0, import_pathe2.resolve)(options.src) : void 0;
177
180
  const out = (0, import_pathe2.resolve)(options.out);
178
181
  const {
179
- defaultValue,
182
+ defaultValue = {},
180
183
  onUpdate,
181
184
  beforeOutput
182
185
  } = options;
@@ -184,15 +187,15 @@ function useLoad(name, options) {
184
187
  name,
185
188
  src,
186
189
  out,
187
- value: src ? import_fs_extra2.default.readJsonSync(src) : defaultValue ?? {},
190
+ value: src ? import_fs_extra2.default.readJsonSync(src) : defaultValue,
191
+ onUpdate,
188
192
  output() {
189
193
  const data = beforeOutput?.(info.value) ?? info.value;
190
- import_fs_extra2.default.outputJsonSync(info.out, data);
191
- },
192
- onUpdate
194
+ import_fs_extra2.default.outputJsonSync(out, data);
195
+ }
193
196
  };
194
197
  ctx.loadInfos.push(info);
195
- onUpdate(info.value, void 0);
198
+ onUpdate?.(info.value, void 0);
196
199
  return info;
197
200
  }
198
201
 
package/dist/index.d.cts CHANGED
@@ -7,7 +7,7 @@ interface UseLoadOptions {
7
7
  src?: string;
8
8
  out: string;
9
9
  defaultValue?: unknown;
10
- onUpdate: (newVal: any, oldVal: any) => any;
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.d.ts CHANGED
@@ -7,7 +7,7 @@ interface UseLoadOptions {
7
7
  src?: string;
8
8
  out: string;
9
9
  defaultValue?: unknown;
10
- onUpdate: (newVal: any, oldVal: any) => any;
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(info.out, data);
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,7 +1,7 @@
1
1
  {
2
2
  "name": "kerria",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.1.0",
5
5
  "description": "",
6
6
  "author": "KazariEX",
7
7
  "license": "MIT",
@@ -35,7 +35,6 @@
35
35
  "scripts": {
36
36
  "build": "tsup-node",
37
37
  "dev": "tsup-node --watch",
38
- "release": "bumpp --no-push -c \"release: v%s\"",
39
- "test": "vitest"
38
+ "release": "bumpp --no-push -c \"release: v%s\""
40
39
  }
41
40
  }