kerria 0.4.0 → 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.
@@ -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,29 +55,39 @@ 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
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);
@@ -104,8 +109,7 @@ function createKerria(sign, setup) {
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) {
@@ -140,8 +144,7 @@ function useLoad(name, options) {
140
144
  value: src ? readJsonSync(src) : defaultValue,
141
145
  update,
142
146
  output() {
143
- const data = output?.(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);
@@ -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,35 +1,36 @@
1
1
  {
2
2
  "name": "kerria",
3
3
  "type": "module",
4
- "version": "0.4.0",
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
- "@types/node": "^24.3.0",
28
+ "@types/node": "^25.0.3",
28
29
  "@zinkawaii/eslint-config": "^0.4.1",
29
30
  "@zinkawaii/tsconfig": "^0.0.2",
30
- "bumpp": "^10.2.3",
31
- "eslint": "^9.33.0",
32
- "tsdown": "^0.14.1"
31
+ "bumpp": "^10.3.2",
32
+ "eslint": "^9.39.2",
33
+ "tsdown": "^0.18.4"
33
34
  },
34
35
  "scripts": {
35
36
  "build": "tsdown",