katanakit-js 4.0.4 → 5.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/README.md +184 -10
- package/dist/adapters/insforge/index.d.ts +3 -0
- package/dist/adapters/insforge/index.d.ts.map +1 -0
- package/dist/adapters/insforge/index.js +2 -0
- package/dist/adapters/insforge/index.js.map +1 -0
- package/dist/adapters/insforge/insforge.service.d.ts +116 -0
- package/dist/adapters/insforge/insforge.service.d.ts.map +1 -0
- package/dist/adapters/insforge/insforge.service.js +437 -0
- package/dist/adapters/insforge/insforge.service.js.map +1 -0
- package/dist/adapters/notion/notion.service.d.ts.map +1 -1
- package/dist/adapters/notion/notion.service.js +72 -54
- package/dist/adapters/notion/notion.service.js.map +1 -1
- package/dist/adapters/wordpress/wordpress.service.d.ts.map +1 -1
- package/dist/adapters/wordpress/wordpress.service.js +163 -85
- package/dist/adapters/wordpress/wordpress.service.js.map +1 -1
- package/dist/config/seo.service.d.ts.map +1 -1
- package/dist/config/seo.service.js +42 -4
- package/dist/config/seo.service.js.map +1 -1
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/services/error.service.d.ts +21 -1
- package/dist/core/services/error.service.d.ts.map +1 -1
- package/dist/core/services/error.service.js +39 -0
- package/dist/core/services/error.service.js.map +1 -1
- package/dist/core/services/faker.service.d.ts +160 -0
- package/dist/core/services/faker.service.d.ts.map +1 -0
- package/dist/core/services/faker.service.js +235 -0
- package/dist/core/services/faker.service.js.map +1 -0
- package/dist/core/services/http.service.d.ts.map +1 -1
- package/dist/core/services/http.service.js +10 -16
- package/dist/core/services/http.service.js.map +1 -1
- package/dist/core/services/media.service.d.ts +97 -0
- package/dist/core/services/media.service.d.ts.map +1 -0
- package/dist/core/services/media.service.js +239 -0
- package/dist/core/services/media.service.js.map +1 -0
- package/dist/core/services/reactive.service.d.ts.map +1 -1
- package/dist/core/services/reactive.service.js +5 -14
- package/dist/core/services/reactive.service.js.map +1 -1
- package/dist/core/services/result.service.d.ts +41 -0
- package/dist/core/services/result.service.d.ts.map +1 -0
- package/dist/core/services/result.service.js +60 -0
- package/dist/core/services/result.service.js.map +1 -0
- package/dist/core/services/validation.service.d.ts +22 -0
- package/dist/core/services/validation.service.d.ts.map +1 -0
- package/dist/core/services/validation.service.js +41 -0
- package/dist/core/services/validation.service.js.map +1 -0
- package/dist/core/services/youtube.service.d.ts +79 -0
- package/dist/core/services/youtube.service.d.ts.map +1 -0
- package/dist/core/services/youtube.service.js +388 -0
- package/dist/core/services/youtube.service.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/filesystem/filesystem.service.d.ts +267 -0
- package/dist/infrastructure/filesystem/filesystem.service.d.ts.map +1 -0
- package/dist/infrastructure/filesystem/filesystem.service.js +453 -0
- package/dist/infrastructure/filesystem/filesystem.service.js.map +1 -0
- package/dist/infrastructure/filesystem/path.service.d.ts +115 -0
- package/dist/infrastructure/filesystem/path.service.d.ts.map +1 -0
- package/dist/infrastructure/filesystem/path.service.js +154 -0
- package/dist/infrastructure/filesystem/path.service.js.map +1 -0
- package/dist/infrastructure/index.d.ts +3 -0
- package/dist/infrastructure/index.d.ts.map +1 -1
- package/dist/infrastructure/index.js +3 -0
- package/dist/infrastructure/index.js.map +1 -1
- package/dist/infrastructure/media/media.service.d.ts +15 -0
- package/dist/infrastructure/media/media.service.d.ts.map +1 -0
- package/dist/infrastructure/media/media.service.js +44 -0
- package/dist/infrastructure/media/media.service.js.map +1 -0
- package/dist/infrastructure/storage/storage.service.d.ts.map +1 -1
- package/dist/infrastructure/storage/storage.service.js +10 -8
- package/dist/infrastructure/storage/storage.service.js.map +1 -1
- package/dist/schemas/access.schema.d.ts +69 -0
- package/dist/schemas/access.schema.d.ts.map +1 -0
- package/dist/schemas/access.schema.js +36 -0
- package/dist/schemas/access.schema.js.map +1 -0
- package/dist/schemas/ai.schema.d.ts +53 -0
- package/dist/schemas/ai.schema.d.ts.map +1 -0
- package/dist/schemas/ai.schema.js +34 -0
- package/dist/schemas/ai.schema.js.map +1 -0
- package/dist/schemas/common.schema.d.ts +49 -0
- package/dist/schemas/common.schema.d.ts.map +1 -0
- package/dist/schemas/common.schema.js +23 -0
- package/dist/schemas/common.schema.js.map +1 -0
- package/dist/schemas/core.schema.d.ts +164 -0
- package/dist/schemas/core.schema.d.ts.map +1 -0
- package/dist/schemas/core.schema.js +109 -0
- package/dist/schemas/core.schema.js.map +1 -0
- package/dist/schemas/faker.schema.d.ts +12 -0
- package/dist/schemas/faker.schema.d.ts.map +1 -0
- package/dist/schemas/faker.schema.js +12 -0
- package/dist/schemas/faker.schema.js.map +1 -0
- package/dist/schemas/filesystem.schema.d.ts +71 -0
- package/dist/schemas/filesystem.schema.d.ts.map +1 -0
- package/dist/schemas/filesystem.schema.js +56 -0
- package/dist/schemas/filesystem.schema.js.map +1 -0
- package/dist/schemas/index.d.ts +13 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +13 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/insforge.schema.d.ts +69 -0
- package/dist/schemas/insforge.schema.d.ts.map +1 -0
- package/dist/schemas/insforge.schema.js +82 -0
- package/dist/schemas/insforge.schema.js.map +1 -0
- package/dist/schemas/media.schema.d.ts +51 -0
- package/dist/schemas/media.schema.d.ts.map +1 -0
- package/dist/schemas/media.schema.js +50 -0
- package/dist/schemas/media.schema.js.map +1 -0
- package/dist/schemas/notion.schema.d.ts +1058 -0
- package/dist/schemas/notion.schema.d.ts.map +1 -0
- package/dist/schemas/notion.schema.js +248 -0
- package/dist/schemas/notion.schema.js.map +1 -0
- package/dist/schemas/rss.schema.d.ts +35 -0
- package/dist/schemas/rss.schema.d.ts.map +1 -0
- package/dist/schemas/rss.schema.js +44 -0
- package/dist/schemas/rss.schema.js.map +1 -0
- package/dist/schemas/wordpress.schema.d.ts +1304 -0
- package/dist/schemas/wordpress.schema.d.ts.map +1 -0
- package/dist/schemas/wordpress.schema.js +404 -0
- package/dist/schemas/wordpress.schema.js.map +1 -0
- package/dist/schemas/youtube.schema.d.ts +122 -0
- package/dist/schemas/youtube.schema.d.ts.map +1 -0
- package/dist/schemas/youtube.schema.js +113 -0
- package/dist/schemas/youtube.schema.js.map +1 -0
- package/dist/types/index.d.ts +191 -800
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +40 -15
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { useGetDirname, useIsNode, useJoinPath, useResolvePath } from "./path.service.js";
|
|
2
|
+
/** Internal error carrying `code`/`details` through `runFs`. */
|
|
3
|
+
class FilesystemOperationError extends Error {
|
|
4
|
+
code;
|
|
5
|
+
details;
|
|
6
|
+
path;
|
|
7
|
+
constructor(code, message, path, details) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "FilesystemOperationError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.path = path;
|
|
12
|
+
this.details = details;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** Normalizes any thrown value into a serializable {@link FilesystemError}. */
|
|
16
|
+
function toFilesystemError(error, fallbackPath) {
|
|
17
|
+
if (error instanceof FilesystemOperationError) {
|
|
18
|
+
return {
|
|
19
|
+
code: error.code,
|
|
20
|
+
message: error.message,
|
|
21
|
+
path: error.path ?? fallbackPath,
|
|
22
|
+
details: error.details,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const errno = error;
|
|
26
|
+
return {
|
|
27
|
+
code: typeof errno?.code === "string" ? errno.code : "ERR_FILESYSTEM",
|
|
28
|
+
message: error instanceof Error ? error.message : "Unknown filesystem error",
|
|
29
|
+
path: typeof errno?.path === "string" ? errno.path : fallbackPath,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Safe Result used when `node:fs` is not available (browser/edge). */
|
|
33
|
+
function createUnavailableError() {
|
|
34
|
+
return {
|
|
35
|
+
code: "ERR_FS_UNAVAILABLE",
|
|
36
|
+
message: "[Filesystem] A Node.js-compatible runtime is required for filesystem operations.",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Internal runner: guards the runtime, loads `node:fs/promises` lazily and
|
|
41
|
+
* wraps the operation into a Safe Result (`{ data, error, ok }`).
|
|
42
|
+
*/
|
|
43
|
+
async function runFs(targetPath, operation) {
|
|
44
|
+
if (!useIsNode()) {
|
|
45
|
+
return { data: null, error: createUnavailableError(), ok: false };
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const fs = await import("node:fs/promises");
|
|
49
|
+
return { data: await operation(fs), error: null, ok: true };
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
return { data: null, error: toFilesystemError(error, targetPath), ok: false };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Parses JSON text, optionally validating it against a Zod schema. */
|
|
56
|
+
function parseJson(raw, path, schema) {
|
|
57
|
+
let parsed;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(raw);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw new FilesystemOperationError("ERR_INVALID_JSON", `[Filesystem] Invalid JSON in "${path}": ${error instanceof Error ? error.message : "parse error"}`, path);
|
|
63
|
+
}
|
|
64
|
+
if (!schema)
|
|
65
|
+
return parsed;
|
|
66
|
+
const result = schema.safeParse(parsed);
|
|
67
|
+
if (!result.success) {
|
|
68
|
+
throw new FilesystemOperationError("ERR_VALIDATION", `[Filesystem] JSON in "${path}" does not match the expected schema.`, path, result.error.issues);
|
|
69
|
+
}
|
|
70
|
+
return result.data;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reads a text file.
|
|
74
|
+
*
|
|
75
|
+
* @param path - File path (relative paths resolve against `process.cwd()`).
|
|
76
|
+
* @param encoding - Text encoding (default: `"utf8"`).
|
|
77
|
+
* @returns Safe Result with the file contents as a string.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { useReadFile } from "katanakit-js";
|
|
82
|
+
*
|
|
83
|
+
* const result = await useReadFile("data/books.json");
|
|
84
|
+
* if (result.ok) console.log(result.data);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export async function useReadFile(path, encoding = "utf8") {
|
|
88
|
+
return runFs(path, (fs) => fs.readFile(path, encoding));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads a file as raw bytes (no encoding).
|
|
92
|
+
*
|
|
93
|
+
* @param path - File path.
|
|
94
|
+
* @returns Safe Result with the contents as a `Uint8Array`.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const result = await useReadFileBuffer("assets/logo.png");
|
|
99
|
+
* if (result.ok) upload(result.data);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export async function useReadFileBuffer(path) {
|
|
103
|
+
return runFs(path, (fs) => fs.readFile(path));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Streams a file and returns its checksum as a lowercase hex digest.
|
|
107
|
+
*
|
|
108
|
+
* @param path - File path.
|
|
109
|
+
* @param algorithm - `"md5" | "sha1" | "sha256" | "sha512"` (default: `"sha256"`).
|
|
110
|
+
* @returns Safe Result with the hex digest.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* const result = await useHashFile("image.iso"); // sha256
|
|
115
|
+
* const md5 = await useHashFile("image.iso", "md5");
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export async function useHashFile(path, algorithm = "sha256") {
|
|
119
|
+
return runFs(path, async () => {
|
|
120
|
+
const [{ createHash }, { createReadStream }] = await Promise.all([
|
|
121
|
+
import("node:crypto"),
|
|
122
|
+
import("node:fs"),
|
|
123
|
+
]);
|
|
124
|
+
const hash = createHash(algorithm);
|
|
125
|
+
for await (const chunk of createReadStream(path)) {
|
|
126
|
+
hash.update(chunk);
|
|
127
|
+
}
|
|
128
|
+
return hash.digest("hex");
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Verifies a file checksum against an expected value (case-insensitive).
|
|
133
|
+
*
|
|
134
|
+
* @param path - File path.
|
|
135
|
+
* @param expected - Expected hex digest (from a `.sha256` sidecar, release page, …).
|
|
136
|
+
* @param algorithm - Algorithm used for both values (default: `"sha256"`).
|
|
137
|
+
* @returns Safe Result with `true` when the digests match.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const check = await useVerifyFileHash("image.iso", "e3b0c442…");
|
|
142
|
+
* if (check.ok && !check.data) throw new Error("checksum mismatch");
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export async function useVerifyFileHash(path, expected, algorithm = "sha256") {
|
|
146
|
+
const hashed = await useHashFile(path, algorithm);
|
|
147
|
+
if (!hashed.ok)
|
|
148
|
+
return hashed;
|
|
149
|
+
return {
|
|
150
|
+
data: hashed.data.toLowerCase() === expected.trim().toLowerCase(),
|
|
151
|
+
error: null,
|
|
152
|
+
ok: true,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Writes (or overwrites) a file, creating it when missing.
|
|
157
|
+
*
|
|
158
|
+
* @param path - Destination file path.
|
|
159
|
+
* @param data - Text or bytes to write.
|
|
160
|
+
* @param options - Encoding, mode and flag overrides.
|
|
161
|
+
* @returns Safe Result with the written path.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const result = await useWriteFile("logs/app.log", "started\n");
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export async function useWriteFile(path, data, options) {
|
|
169
|
+
return runFs(path, async (fs) => {
|
|
170
|
+
await fs.writeFile(path, data, options);
|
|
171
|
+
return path;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Appends text or bytes to a file, creating it when missing.
|
|
176
|
+
*
|
|
177
|
+
* @param path - Destination file path.
|
|
178
|
+
* @param data - Text or bytes to append.
|
|
179
|
+
* @param options - Encoding, mode and flag overrides.
|
|
180
|
+
* @returns Safe Result with the written path.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* await useAppendFile("logs/app.log", "request handled\n");
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export async function useAppendFile(path, data, options) {
|
|
188
|
+
return runFs(path, async (fs) => {
|
|
189
|
+
await fs.appendFile(path, data, options);
|
|
190
|
+
return path;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Reads and parses a JSON file, optionally validating it with a Zod schema.
|
|
195
|
+
*
|
|
196
|
+
* @typeParam T - Expected parsed (and validated) shape.
|
|
197
|
+
* @param path - File path.
|
|
198
|
+
* @param schema - Optional Zod schema; invalid shapes fail with `ERR_VALIDATION`.
|
|
199
|
+
* @returns Safe Result with the parsed data, or `ERR_INVALID_JSON` / `ERR_VALIDATION`.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* import { z } from "zod";
|
|
204
|
+
*
|
|
205
|
+
* const BookSchema = z.object({ title: z.string() });
|
|
206
|
+
* const result = await useReadJsonFile("data/books.json", BookSchema);
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export async function useReadJsonFile(path, schema) {
|
|
210
|
+
return runFs(path, async (fs) => parseJson(await fs.readFile(path, "utf8"), path, schema));
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Serializes data to JSON and writes it to a file (pretty-printed by default).
|
|
214
|
+
*
|
|
215
|
+
* @param path - Destination file path.
|
|
216
|
+
* @param data - Value to serialize (`JSON.stringify` rules apply).
|
|
217
|
+
* @param options - Set `pretty: false` for compact output.
|
|
218
|
+
* @returns Safe Result with the written path.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* await useWriteJsonFile("data/books.json", books);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export async function useWriteJsonFile(path, data, options) {
|
|
226
|
+
return runFs(path, async (fs) => {
|
|
227
|
+
const spaces = options?.pretty === false ? undefined : 2;
|
|
228
|
+
await fs.writeFile(path, `${JSON.stringify(data, null, spaces)}\n`, "utf8");
|
|
229
|
+
return path;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Lists a directory with type flags resolved per entry.
|
|
234
|
+
*
|
|
235
|
+
* @param path - Directory path.
|
|
236
|
+
* @param options - `recursive: true` walks nested directories.
|
|
237
|
+
* @returns Safe Result with `{ name, path, isFile, isDirectory }` entries.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* const result = await useReadDir("content", { recursive: true });
|
|
242
|
+
* if (result.ok) result.data.filter((entry) => entry.isFile);
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export async function useReadDir(path, options) {
|
|
246
|
+
return runFs(path, async (fs) => {
|
|
247
|
+
const entries = await fs.readdir(path, {
|
|
248
|
+
recursive: options?.recursive ?? false,
|
|
249
|
+
withFileTypes: true,
|
|
250
|
+
});
|
|
251
|
+
return Promise.all(entries.map(async (entry) => ({
|
|
252
|
+
name: entry.name,
|
|
253
|
+
path: await useJoinPath(entry.parentPath ?? path, entry.name),
|
|
254
|
+
isFile: entry.isFile(),
|
|
255
|
+
isDirectory: entry.isDirectory(),
|
|
256
|
+
})));
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Creates a directory (and any missing parents).
|
|
261
|
+
*
|
|
262
|
+
* @param path - Directory path to ensure.
|
|
263
|
+
* @returns Safe Result with the directory path.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* await useEnsureDir("data/cache/books");
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
export async function useEnsureDir(path) {
|
|
271
|
+
return runFs(path, async (fs) => {
|
|
272
|
+
await fs.mkdir(path, { recursive: true });
|
|
273
|
+
return path;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Checks whether a path exists and is accessible.
|
|
278
|
+
* Never throws: returns `false` for missing paths and non-Node runtimes.
|
|
279
|
+
*
|
|
280
|
+
* @param path - Path to check.
|
|
281
|
+
* @returns `true` when the path exists.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* if (await useFileExists("data/books.json")) {
|
|
286
|
+
* // read it
|
|
287
|
+
* }
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export async function useFileExists(path) {
|
|
291
|
+
if (!useIsNode())
|
|
292
|
+
return false;
|
|
293
|
+
try {
|
|
294
|
+
const fs = await import("node:fs/promises");
|
|
295
|
+
await fs.access(path);
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Returns a portable stats subset for a file or directory.
|
|
304
|
+
*
|
|
305
|
+
* @param path - Path to inspect (follows symlinks).
|
|
306
|
+
* @returns Safe Result with size, type flags and epoch-ms timestamps.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* const result = await useGetFileStats("assets/logo.png");
|
|
311
|
+
* if (result.ok) console.log(result.data.size);
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export async function useGetFileStats(path) {
|
|
315
|
+
return runFs(path, async (fs) => {
|
|
316
|
+
const stats = await fs.stat(path);
|
|
317
|
+
return {
|
|
318
|
+
size: stats.size,
|
|
319
|
+
isFile: stats.isFile(),
|
|
320
|
+
isDirectory: stats.isDirectory(),
|
|
321
|
+
isSymbolicLink: stats.isSymbolicLink(),
|
|
322
|
+
createdAt: stats.birthtimeMs,
|
|
323
|
+
modifiedAt: stats.mtimeMs,
|
|
324
|
+
accessedAt: stats.atimeMs,
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Copies a file. Existing destinations are overwritten unless
|
|
330
|
+
* `overwrite: false` is passed (`COPYFILE_EXCL`).
|
|
331
|
+
*
|
|
332
|
+
* @param source - Source file path.
|
|
333
|
+
* @param destination - Destination file path.
|
|
334
|
+
* @param options - Set `overwrite: false` to fail when the target exists.
|
|
335
|
+
* @returns Safe Result with the destination path.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```ts
|
|
339
|
+
* await useCopyFile("data/books.json", "backup/books.json");
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export async function useCopyFile(source, destination, options) {
|
|
343
|
+
return runFs(source, async (fs) => {
|
|
344
|
+
const mode = options?.overwrite === false ? fs.constants.COPYFILE_EXCL : 0;
|
|
345
|
+
await fs.copyFile(source, destination, mode);
|
|
346
|
+
return destination;
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Moves (renames) a file or directory.
|
|
351
|
+
*
|
|
352
|
+
* @param source - Source path.
|
|
353
|
+
* @param destination - Destination path.
|
|
354
|
+
* @returns Safe Result with the destination path.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* await useMoveFile("data/draft.json", "data/books.json");
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
export async function useMoveFile(source, destination) {
|
|
362
|
+
return runFs(source, async (fs) => {
|
|
363
|
+
await fs.rename(source, destination);
|
|
364
|
+
return destination;
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Removes a file.
|
|
369
|
+
*
|
|
370
|
+
* @param path - File path to remove.
|
|
371
|
+
* @returns Safe Result with the removed path.
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* await useRemoveFile("data/cache/books.json");
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
export async function useRemoveFile(path) {
|
|
379
|
+
return runFs(path, async (fs) => {
|
|
380
|
+
await fs.unlink(path);
|
|
381
|
+
return path;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Removes a directory tree. Recursive by default; `force: false`
|
|
386
|
+
* surfaces `ENOENT` instead of ignoring missing paths.
|
|
387
|
+
*
|
|
388
|
+
* @param path - Directory path to remove.
|
|
389
|
+
* @param options - `recursive` (default `true`) and `force` (default `false`).
|
|
390
|
+
* @returns Safe Result with the removed path.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* await useRemoveDir("data/cache");
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
export async function useRemoveDir(path, options) {
|
|
398
|
+
return runFs(path, async (fs) => {
|
|
399
|
+
await fs.rm(path, {
|
|
400
|
+
force: options?.force ?? false,
|
|
401
|
+
recursive: options?.recursive ?? true,
|
|
402
|
+
});
|
|
403
|
+
return path;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
/** Resolves `relativePath` against the directory of `importMetaUrl`. */
|
|
407
|
+
async function resolveModulePath(importMetaUrl, relativePath) {
|
|
408
|
+
const base = await useGetDirname(importMetaUrl);
|
|
409
|
+
return useResolvePath(base, relativePath);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Reads a file resolved from the caller's module directory — the
|
|
413
|
+
* `__dirname` + relative path pattern, without `__dirname`.
|
|
414
|
+
*
|
|
415
|
+
* @param importMetaUrl - Your module's `import.meta.url`.
|
|
416
|
+
* @param relativePath - Path relative to that module (e.g. `"../data/books.json"`).
|
|
417
|
+
* @param encoding - Text encoding (default: `"utf8"`).
|
|
418
|
+
* @returns Safe Result with the file contents as a string.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```ts
|
|
422
|
+
* const css = await useReadModuleFile(import.meta.url, "./theme.css");
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
export async function useReadModuleFile(importMetaUrl, relativePath, encoding = "utf8") {
|
|
426
|
+
return runFs(relativePath, async (fs) => {
|
|
427
|
+
const absolute = await resolveModulePath(importMetaUrl, relativePath);
|
|
428
|
+
return fs.readFile(absolute, encoding);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Reads and parses a JSON file resolved from the caller's module directory,
|
|
433
|
+
* optionally validating it with a Zod schema.
|
|
434
|
+
*
|
|
435
|
+
* @typeParam T - Expected parsed (and validated) shape.
|
|
436
|
+
* @param importMetaUrl - Your module's `import.meta.url`.
|
|
437
|
+
* @param relativePath - Path relative to that module.
|
|
438
|
+
* @param schema - Optional Zod schema for runtime validation.
|
|
439
|
+
* @returns Safe Result with the parsed data.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```ts
|
|
443
|
+
* const result = await useReadModuleJson<Book[]>(import.meta.url, "../data/books.json");
|
|
444
|
+
* if (result.ok) initBooks(result.data);
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
export async function useReadModuleJson(importMetaUrl, relativePath, schema) {
|
|
448
|
+
return runFs(relativePath, async (fs) => {
|
|
449
|
+
const absolute = await resolveModulePath(importMetaUrl, relativePath);
|
|
450
|
+
return parseJson(await fs.readFile(absolute, "utf8"), absolute, schema);
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
//# sourceMappingURL=filesystem.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesystem.service.js","sourceRoot":"","sources":["../../../src/infrastructure/filesystem/filesystem.service.ts"],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5
|
+
* @returns `true` when `process.versions.node` is available.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { useIsNode } from "katanakit-js";
|
|
10
|
+
*
|
|
11
|
+
* if (useIsNode()) {
|
|
12
|
+
* // safe to use node:fs / node:path
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const useIsNode: () => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the directory of a module from its `import.meta.url`.
|
|
19
|
+
*
|
|
20
|
+
* Unlike `import.meta.dirname`, this works for any module you pass in,
|
|
21
|
+
* so a consumer's app resolves its own paths — never KatanaKit's `dist/`.
|
|
22
|
+
*
|
|
23
|
+
* @param importMetaUrl - Your module's `import.meta.url`.
|
|
24
|
+
* @returns The absolute directory that contains the module.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { useGetDirname } from "katanakit-js";
|
|
29
|
+
*
|
|
30
|
+
* const dir = await useGetDirname(import.meta.url);
|
|
31
|
+
* // "<project>/src/features/books"
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useGetDirname(importMetaUrl: string): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Resolves a sequence of path segments into an absolute path.
|
|
37
|
+
* Relative segments resolve against `process.cwd()`; if the first
|
|
38
|
+
* segment is absolute (like a `useGetDirname` result), cwd is ignored.
|
|
39
|
+
*
|
|
40
|
+
* @param segments - Path segments to resolve.
|
|
41
|
+
* @returns The resolved absolute path.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const base = await useGetDirname(import.meta.url);
|
|
46
|
+
* const file = await useResolvePath(base, "../data/books.json");
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function useResolvePath(...segments: string[]): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Joins path segments without resolving against `process.cwd()`.
|
|
52
|
+
*
|
|
53
|
+
* @param segments - Path segments to join.
|
|
54
|
+
* @returns The joined (possibly relative) path.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* await useJoinPath("data", "books.json"); // "data/books.json"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function useJoinPath(...segments: string[]): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Computes the relative path from `from` to `to`.
|
|
64
|
+
*
|
|
65
|
+
* @param from - Origin path.
|
|
66
|
+
* @param to - Destination path.
|
|
67
|
+
* @returns The relative path between both.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* await useGetRelativePath("/app/src", "/app/data/books.json"); // "../data/books.json"
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function useGetRelativePath(from: string, to: string): Promise<string>;
|
|
75
|
+
/**
|
|
76
|
+
* Returns the last portion of a path (file name).
|
|
77
|
+
*
|
|
78
|
+
* @param filePath - The path to inspect.
|
|
79
|
+
* @param suffix - Optional suffix to strip (e.g. `".json"`).
|
|
80
|
+
* @returns The base name.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* await useGetBasename("/data/books.json"); // "books.json"
|
|
85
|
+
* await useGetBasename("/data/books.json", ".json"); // "books"
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function useGetBasename(filePath: string, suffix?: string): Promise<string>;
|
|
89
|
+
/**
|
|
90
|
+
* Returns the extension of a path, including the leading dot.
|
|
91
|
+
*
|
|
92
|
+
* @param filePath - The path to inspect.
|
|
93
|
+
* @returns The extension (`""` when there is none).
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* await useGetFileExtension("/data/books.json"); // ".json"
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare function useGetFileExtension(filePath: string): Promise<string>;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the current working directory of the process (where the
|
|
103
|
+
* command was launched), or `null` outside a Node-compatible runtime.
|
|
104
|
+
* Use it for project-relative paths; use {@link useGetDirname} for
|
|
105
|
+
* file-relative paths.
|
|
106
|
+
*
|
|
107
|
+
* @returns The cwd, or `null` in browsers/edge runtimes.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const cwd = useGetCwd(); // "/app" when launched from the project root
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export declare function useGetCwd(): string | null;
|
|
115
|
+
//# sourceMappingURL=path.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.service.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/filesystem/path.service.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,SAAS,QAAO,OACgD,CAAC;AAkB9E;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,aAAa,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG1E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,cAAc,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3E;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,WAAW,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAGxE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGlF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGvF;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,IAAI,MAAM,GAAG,IAAI,CAEzC"}
|