@weapp-core/shared 3.0.3 → 3.0.5

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 CHANGED
@@ -3,12 +3,14 @@
3
3
  ## 简介
4
4
 
5
5
  `@weapp-core/shared` 汇总 weapp-vite 生态内的通用工具函数与依赖封装,供多个包复用。
6
+ 根入口默认保持小程序运行时安全;Node 专用能力通过独立子入口导出。
6
7
 
7
8
  ## 特性
8
9
 
9
10
  - 常用字符串与路径处理工具
10
11
  - 对象合并与判定工具
11
- - 统一导出常用第三方工具(`defu`、`get`、`set`、`object-hash`)
12
+ - 统一导出常用第三方工具(`defu`、`get`、`set`)
13
+ - 通过 `@weapp-core/shared/node` 提供 Node 专用哈希与文件系统能力
12
14
 
13
15
  ## 安装
14
16
 
@@ -23,13 +25,14 @@ import {
23
25
  addExtension,
24
26
  defu,
25
27
  escapeStringRegexp,
26
- objectHash,
27
28
  removeExtension,
28
29
  } from '@weapp-core/shared'
30
+ import { fs, objectHash } from '@weapp-core/shared/node'
29
31
 
30
32
  const name = addExtension('pages/index/index')
31
33
  const cleaned = removeExtension('app.json')
32
34
  const hash = objectHash({ foo: 'bar' })
35
+ const exists = await fs.pathExists('app.json')
33
36
  ```
34
37
 
35
38
  ## 配置
@@ -0,0 +1,93 @@
1
+ import { Dirent, OpenMode, PathLike, Stats, closeSync, constants, existsSync, mkdirSync, openSync, promises, readFileSync, unlinkSync, utimesSync, watch, writeFileSync } from "node:fs";
2
+ import { FileHandle } from "node:fs/promises";
3
+
4
+ //#region src/fs/index.d.ts
5
+ interface JsonReadOptions {
6
+ encoding?: BufferEncoding;
7
+ throws?: boolean;
8
+ }
9
+ interface JsonWriteOptions {
10
+ encoding?: BufferEncoding;
11
+ replacer?: ((this: any, key: string, value: any) => any) | Array<number | string> | null;
12
+ spaces?: number | string;
13
+ }
14
+ interface CopyOptions {
15
+ dereference?: boolean;
16
+ errorOnExist?: boolean;
17
+ filter?: (src: string, dest: string) => boolean | Promise<boolean>;
18
+ overwrite?: boolean;
19
+ preserveTimestamps?: boolean;
20
+ verbatimSymlinks?: boolean;
21
+ }
22
+ interface MoveOptions {
23
+ overwrite?: boolean;
24
+ }
25
+ declare function pathExists(target: string): Promise<boolean>;
26
+ declare function pathExistsSync(target: string): boolean;
27
+ declare function exists(target: string): Promise<boolean>;
28
+ declare function ensureDir(dir: string): Promise<void>;
29
+ declare function ensureFile(file: string): Promise<void>;
30
+ declare function remove(target: string): Promise<void>;
31
+ declare function ensureSymlink(target: string, file: string): Promise<void>;
32
+ declare function emptyDir(dir: string): Promise<void>;
33
+ declare function outputFile(file: string, data: string | NodeJS.ArrayBufferView, options?: BufferEncoding): Promise<void>;
34
+ declare function appendFile(file: string, data: string | NodeJS.ArrayBufferView, options?: BufferEncoding): Promise<void>;
35
+ declare function writeJson(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
36
+ declare function writeJSON(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
37
+ declare function outputJson(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
38
+ declare function outputJSON(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
39
+ declare function readJson<T = unknown>(file: string, options?: JsonReadOptions): Promise<T | null>;
40
+ declare function readJSON<T = unknown>(file: string, options?: JsonReadOptions): Promise<T | null>;
41
+ declare function readJsonSync<T = unknown>(file: string, options?: JsonReadOptions): T | null;
42
+ declare function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
43
+ declare function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
44
+ declare function close$1(handle: number | FileHandle): Promise<void>;
45
+ declare const fs: {
46
+ Dirent: typeof Dirent;
47
+ Stats: typeof Stats;
48
+ access: typeof promises.access;
49
+ appendFile: typeof appendFile;
50
+ close: typeof close$1;
51
+ closeSync: typeof closeSync;
52
+ constants: typeof constants;
53
+ copy: typeof copy;
54
+ emptyDir: typeof emptyDir;
55
+ ensureDir: typeof ensureDir;
56
+ ensureFile: typeof ensureFile;
57
+ ensureSymlink: typeof ensureSymlink;
58
+ exists: typeof exists;
59
+ existsSync: typeof existsSync;
60
+ mkdir: typeof promises.mkdir;
61
+ mkdirSync: typeof mkdirSync;
62
+ mkdtemp: typeof promises.mkdtemp;
63
+ move: typeof move;
64
+ open: (file: PathLike, flags: OpenMode) => Promise<FileHandle>;
65
+ openSync: typeof openSync;
66
+ outputFile: typeof outputFile;
67
+ outputJSON: typeof outputJSON;
68
+ outputJson: typeof outputJson;
69
+ pathExists: typeof pathExists;
70
+ pathExistsSync: typeof pathExistsSync;
71
+ readFile: typeof promises.readFile;
72
+ readFileSync: typeof readFileSync;
73
+ readJSON: typeof readJSON;
74
+ readJson: typeof readJson;
75
+ readJsonSync: typeof readJsonSync;
76
+ readdir: typeof promises.readdir;
77
+ rm: typeof promises.rm;
78
+ remove: typeof remove;
79
+ stat: typeof promises.stat;
80
+ symlink: typeof promises.symlink;
81
+ unlinkSync: typeof unlinkSync;
82
+ utimes: typeof promises.utimes;
83
+ utimesSync: typeof utimesSync;
84
+ watch: typeof watch;
85
+ writeFile: typeof promises.writeFile;
86
+ writeFileSync: typeof writeFileSync;
87
+ writeJSON: typeof writeJSON;
88
+ writeJson: typeof writeJson;
89
+ };
90
+ type FsDirent = Dirent;
91
+ type FsStats = Stats;
92
+ //#endregion
93
+ export { CopyOptions, FsDirent, FsStats, JsonReadOptions, JsonWriteOptions, MoveOptions, fs };
@@ -0,0 +1,198 @@
1
+ import { Dirent, Stats, close, closeSync, constants, existsSync, mkdirSync, openSync, promises, readFileSync, unlinkSync, utimesSync, watch, writeFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ //#region src/fs/index.ts
4
+ function normalizeJsonEncoding(encoding) {
5
+ return encoding ?? "utf8";
6
+ }
7
+ function normalizeJsonText(source) {
8
+ return source.charCodeAt(0) === 65279 ? source.slice(1) : source;
9
+ }
10
+ function serializeJson(data, options) {
11
+ const replacer = options?.replacer;
12
+ if (typeof replacer === "function") return JSON.stringify(data, replacer, options?.spaces);
13
+ if (Array.isArray(replacer)) return JSON.stringify(data, replacer, options?.spaces);
14
+ return JSON.stringify(data, null, options?.spaces);
15
+ }
16
+ async function pathExists(target) {
17
+ try {
18
+ await promises.access(target);
19
+ return true;
20
+ } catch {
21
+ return false;
22
+ }
23
+ }
24
+ function pathExistsSync(target) {
25
+ try {
26
+ return existsSync(target);
27
+ } catch {
28
+ return false;
29
+ }
30
+ }
31
+ async function exists(target) {
32
+ return pathExists(target);
33
+ }
34
+ async function ensureDir(dir) {
35
+ await promises.mkdir(dir, { recursive: true });
36
+ }
37
+ async function ensureFile(file) {
38
+ await ensureDir(path.dirname(file));
39
+ await (await promises.open(file, "a")).close();
40
+ }
41
+ async function remove(target) {
42
+ await promises.rm(target, {
43
+ force: true,
44
+ recursive: true
45
+ });
46
+ }
47
+ async function ensureSymlink(target, file) {
48
+ await ensureDir(path.dirname(file));
49
+ try {
50
+ if (await promises.readlink(file) === target) return;
51
+ await remove(file);
52
+ } catch (error) {
53
+ if (error?.code !== "ENOENT") throw error;
54
+ }
55
+ await promises.symlink(target, file);
56
+ }
57
+ async function emptyDir(dir) {
58
+ await ensureDir(dir);
59
+ const entries = await promises.readdir(dir, { withFileTypes: true });
60
+ await Promise.all(entries.map((entry) => {
61
+ return remove(path.join(dir, entry.name));
62
+ }));
63
+ }
64
+ async function outputFile(file, data, options) {
65
+ await ensureDir(path.dirname(file));
66
+ await promises.writeFile(file, data, options);
67
+ }
68
+ async function appendFile(file, data, options) {
69
+ await ensureDir(path.dirname(file));
70
+ await promises.appendFile(file, data, options);
71
+ }
72
+ async function writeJson(file, data, options) {
73
+ const source = serializeJson(data, options);
74
+ await promises.writeFile(file, source, normalizeJsonEncoding(options?.encoding));
75
+ }
76
+ async function writeJSON(file, data, options) {
77
+ await writeJson(file, data, options);
78
+ }
79
+ async function outputJson(file, data, options) {
80
+ await ensureDir(path.dirname(file));
81
+ await writeJson(file, data, options);
82
+ }
83
+ async function outputJSON(file, data, options) {
84
+ await outputJson(file, data, options);
85
+ }
86
+ async function readJson(file, options) {
87
+ try {
88
+ const source = await promises.readFile(file, normalizeJsonEncoding(options?.encoding));
89
+ return JSON.parse(normalizeJsonText(source));
90
+ } catch (error) {
91
+ if (options?.throws === false && error instanceof SyntaxError) return null;
92
+ throw error;
93
+ }
94
+ }
95
+ async function readJSON(file, options) {
96
+ return readJson(file, options);
97
+ }
98
+ function readJsonSync(file, options) {
99
+ try {
100
+ const source = readFileSync(file, normalizeJsonEncoding(options?.encoding));
101
+ return JSON.parse(normalizeJsonText(source));
102
+ } catch (error) {
103
+ if (options?.throws === false && error instanceof SyntaxError) return null;
104
+ throw error;
105
+ }
106
+ }
107
+ async function copy(src, dest, options) {
108
+ await promises.cp(src, dest, {
109
+ ...typeof options?.dereference === "boolean" ? { dereference: options.dereference } : {},
110
+ ...typeof options?.errorOnExist === "boolean" ? { errorOnExist: options.errorOnExist } : {},
111
+ ...typeof options?.filter === "function" ? { filter: options.filter } : {},
112
+ ...typeof options?.preserveTimestamps === "boolean" ? { preserveTimestamps: options.preserveTimestamps } : {},
113
+ ...typeof options?.verbatimSymlinks === "boolean" ? { verbatimSymlinks: options.verbatimSymlinks } : {},
114
+ force: options?.overwrite ?? true,
115
+ recursive: true
116
+ });
117
+ }
118
+ async function move(src, dest, options) {
119
+ if (src === dest) return;
120
+ if (await pathExists(dest)) {
121
+ if (!options?.overwrite) {
122
+ const error = /* @__PURE__ */ new Error(`dest already exists: ${dest}`);
123
+ error.code = "EEXIST";
124
+ throw error;
125
+ }
126
+ await remove(dest);
127
+ }
128
+ await ensureDir(path.dirname(dest));
129
+ try {
130
+ await promises.rename(src, dest);
131
+ } catch (error) {
132
+ if (error?.code !== "EXDEV") throw error;
133
+ await copy(src, dest, { overwrite: options?.overwrite ?? true });
134
+ await remove(src);
135
+ }
136
+ }
137
+ async function close$1(handle) {
138
+ if (typeof handle === "number") {
139
+ await new Promise((resolve, reject) => {
140
+ close(handle, (error) => {
141
+ if (error) {
142
+ reject(error);
143
+ return;
144
+ }
145
+ resolve();
146
+ });
147
+ });
148
+ return;
149
+ }
150
+ await handle.close();
151
+ }
152
+ const fs = {
153
+ Dirent,
154
+ Stats,
155
+ access: promises.access,
156
+ appendFile,
157
+ close: close$1,
158
+ closeSync,
159
+ constants,
160
+ copy,
161
+ emptyDir,
162
+ ensureDir,
163
+ ensureFile,
164
+ ensureSymlink,
165
+ exists,
166
+ existsSync,
167
+ mkdir: promises.mkdir,
168
+ mkdirSync,
169
+ mkdtemp: promises.mkdtemp,
170
+ move,
171
+ open: (file, flags) => promises.open(file, flags),
172
+ openSync,
173
+ outputFile,
174
+ outputJSON,
175
+ outputJson,
176
+ pathExists,
177
+ pathExistsSync,
178
+ readFile: promises.readFile,
179
+ readFileSync,
180
+ readJSON,
181
+ readJson,
182
+ readJsonSync,
183
+ readdir: promises.readdir,
184
+ rm: promises.rm,
185
+ remove,
186
+ stat: promises.stat,
187
+ symlink: promises.symlink,
188
+ unlinkSync,
189
+ utimes: promises.utimes,
190
+ utimesSync,
191
+ watch,
192
+ writeFile: promises.writeFile,
193
+ writeFileSync,
194
+ writeJSON,
195
+ writeJson
196
+ };
197
+ //#endregion
198
+ export { fs };