zhegu 3.8.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/CHANGELOG.md ADDED
@@ -0,0 +1,47 @@
1
+ # Changelog
2
+
3
+ ## 2024-03-22 v3.2.1
4
+
5
+ - rename to `searchDirUp`, prev version not relased as `searchDirUp`
6
+
7
+ ## 2024-03-22 v3.2.0
8
+
9
+ - feat: add `searchDirUp`
10
+
11
+ ## 2024-03-19 v3.1.0
12
+
13
+ - turn to async to fix broken cjs build
14
+
15
+ ## 2024-03-19 v3.0.2
16
+
17
+ - add package.json `main` & `types` field for used in cjs env like nestjs.
18
+
19
+ ## 2024-03-17 v3.0.1
20
+
21
+ - add tests, pass tests
22
+
23
+ ## 2024-03-06 v3.0.0
24
+
25
+ - rm previous content
26
+ - add `lnsfSafe` symlink function
27
+
28
+ ## 2016-08-30 v2.0.0
29
+
30
+ - rm `req.endAsync`
31
+
32
+ ## 2016-03-09 v1.2.0
33
+
34
+ - update `promise.ify`, include bug fix
35
+
36
+ ## 2016-03-09 v1.1.0
37
+
38
+ - instead `bluebird` with native Promise, and use `promise.ify`
39
+ - remove co
40
+
41
+ ## unknown date v1.0.0
42
+
43
+ - it's 1.0
44
+
45
+ ## 2016-03-06 v0.1.0
46
+
47
+ - 其他都使用这个了
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Magicdawn(magicdawn@qq.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # needle-kit
2
+
3
+ > Arya's needle, little but powerful.
4
+
5
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/magicdawn/needle-kit/ci.yml?style=flat-square&branch=main)](https://github.com/magicdawn/needle-kit/actions/workflows/ci.yml)
6
+ [![Coverage Status](https://img.shields.io/codecov/c/github/magicdawn/needle-kit.svg?style=flat-square)](https://codecov.io/gh/magicdawn/needle-kit)
7
+ [![npm version](https://img.shields.io/npm/v/needle-kit.svg?style=flat-square)](https://www.npmjs.com/package/needle-kit)
8
+ [![npm downloads](https://img.shields.io/npm/dm/needle-kit.svg?style=flat-square)](https://www.npmjs.com/package/needle-kit)
9
+ [![npm license](https://img.shields.io/npm/l/needle-kit.svg?style=flat-square)](http://magicdawn.mit-license.org)
10
+
11
+ ![](https://raw.githubusercontent.com/magicdawn/magicdawn/master/images/202403061424318.png)
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ $ pnpm add needle-kit
17
+ ```
18
+
19
+ ## API
20
+
21
+ ```js
22
+ const needleKit = require('needle-kit')
23
+ ```
24
+
25
+ ## Changelog
26
+
27
+ [CHANGELOG.md](CHANGELOG.md)
28
+
29
+ ## License
30
+
31
+ the MIT License http://magicdawn.mit-license.org
@@ -0,0 +1,104 @@
1
+ import tildify from "tildify";
2
+ import untildify from "untildify";
3
+ import fg from "fast-glob";
4
+ import PQueue from "p-queue";
5
+ import shellEscape from "shell-escape";
6
+ //#region src/fs/dir.d.ts
7
+ /**
8
+ * search `searchName` in `currentPath`
9
+ * @example searchDirUp('/a/b/c/d', 'b') => '/a/b'
10
+ */
11
+ export declare function searchDirUp(currentPath: string, searchName: string): string;
12
+ /**
13
+ * Convert between `tilde path (~/foo)` and `absolute path (/Users/sindresorhus/foo)`
14
+ */
15
+ export declare const HomeDir: {
16
+ /**
17
+ * Convert a tilde path to an absolute path: `~/dev` → `/Users/sindresorhus/dev`.
18
+ *
19
+ * Also expands `~username` when the username matches the current user.
20
+ */
21
+ expand: typeof untildify;
22
+ /**
23
+ * Convert an absolute path to a tilde path: `/Users/sindresorhus/dev` → `~/dev`.
24
+ */
25
+ fold: typeof tildify;
26
+ };
27
+ //#endregion
28
+ //#region src/fs/glob.d.ts
29
+ export declare function customFinderSort(files: string[]): string[];
30
+ export declare function matchFromList(list: string[], pattern: string | string[], fgOptions?: Partial<fg.Options>): Promise<string[]>;
31
+ //#endregion
32
+ //#region src/fs/legacy-file.d.ts
33
+ export declare function processLegacyFiles(targetFile: string, legacyFiles: string[]): Promise<void>;
34
+ //#endregion
35
+ //#region src/fs/local-file.d.ts
36
+ export declare class LocalFile {
37
+ filePath: string;
38
+ /** 目录 */
39
+ dir: string;
40
+ /** 字段包含 name: 表示长名, 包括扩展名 */
41
+ basename: string;
42
+ /** 字段不含 name: 表示短名, 不包括扩展名 */
43
+ base: string;
44
+ /** 字段包含 name: 表示长名, 包括 . */
45
+ extname: string;
46
+ /** 字段不含 name: 表示短名, 不包括 . */
47
+ ext: string;
48
+ constructor(filePath: string);
49
+ }
50
+ export declare const localFile: (filePath: string) => LocalFile;
51
+ //#endregion
52
+ //#region src/fs/rename.d.ts
53
+ export declare function transformFilename(file: string, { baseSuffix, transformBase, newExt }: {
54
+ baseSuffix?: string;
55
+ transformBase?: (base: string) => string;
56
+ newExt?: string;
57
+ }): string;
58
+ //#endregion
59
+ //#region src/fs/symlink.d.ts
60
+ /**
61
+ * `ln -sf`, but safe
62
+ *
63
+ * ## About Safe
64
+ * 1. if `path` not exists, create the symlink. (✅ safe)
65
+ * 2. if `path` exists as a symlink, delete existing symlink & create new one. (✅ safe)
66
+ * 3. if `path` exists as a normal file(not a symlink), an error will be thrown. (❌ not safe, since u may lost data)
67
+ *
68
+ * ## Features
69
+ * - `path` support tilde(~)
70
+ */
71
+ export declare function lnsfSafe(target: string, path: string, { onExistingFile }?: {
72
+ onExistingFile?: 'delete' | 'throw';
73
+ }): Promise<void>;
74
+ /**
75
+ * `ln -sf`, but safe
76
+ *
77
+ * ## About Safe
78
+ * 1. if `path` not exists, create the symlink. (✅ safe)
79
+ * 2. if `path` exists as a symlink, delete existing symlink & create new one. (✅ safe)
80
+ * 3. if `path` exists as a normal file(not a symlink), an error will be thrown. (❌ not safe, since u may lost data)
81
+ *
82
+ * ## Features
83
+ * - `path` support tilde(~)
84
+ */
85
+ export declare function lnsfSafeSync(target: string, path: string, { onExistingFile }?: {
86
+ onExistingFile?: 'delete' | 'throw';
87
+ }): void;
88
+ export declare function symlinkExists(symlinkPath: string): Promise<boolean>;
89
+ export declare function symlinkExistsSync(symlinkPath: string): boolean;
90
+ //#endregion
91
+ //#region src/parallel.d.ts
92
+ export declare function getConcurrency(): number;
93
+ export declare function newPQueue(): PQueue<import("p-queue").PriorityQueue, import("p-queue").QueueAddOptions>;
94
+ //#endregion
95
+ //#region src/shell/index.d.ts
96
+ /**
97
+ * @deprecated use shlex.quote instead
98
+ */
99
+ export declare function escapeShellArg(val: string): string;
100
+ /**
101
+ * @deprecated use shlex.quote instead
102
+ */
103
+ export declare const escapeShellArgs: typeof shellEscape;
104
+ //#endregion
package/dist/index.js ADDED
@@ -0,0 +1,212 @@
1
+ import tildify from "tildify";
2
+ import untildify from "untildify";
3
+ import path, { dirname } from "node:path";
4
+ import { finderSort } from "@magicdawn/finder-sort";
5
+ import fg from "fast-glob";
6
+ import mm from "micromatch";
7
+ import { isDirectory, isFile, isSymlink } from "path-type";
8
+ import fse, { ensureDir, ensureDirSync } from "fs-extra";
9
+ import memoize from "memoize";
10
+ import { lstatSync, rmSync, symlinkSync } from "node:fs";
11
+ import { lstat, rm, symlink } from "node:fs/promises";
12
+ import { availableParallelism } from "node:os";
13
+ import PQueue from "p-queue";
14
+ import z from "zod";
15
+ import shellEscape from "shell-escape";
16
+ //#region src/fs/dir.ts
17
+ /**
18
+ * search `searchName` in `currentPath`
19
+ * @example searchDirUp('/a/b/c/d', 'b') => '/a/b'
20
+ */
21
+ function searchDirUp(currentPath, searchName) {
22
+ const parts = currentPath.split("/");
23
+ if (!parts.includes(searchName)) throw new Error(`searchName(${searchName}) not in currentPath(${currentPath})`);
24
+ while (parts.length && parts.at(-1) !== searchName) parts.pop();
25
+ return parts.join("/");
26
+ }
27
+ /**
28
+ * Convert between `tilde path (~/foo)` and `absolute path (/Users/sindresorhus/foo)`
29
+ */
30
+ const HomeDir = {
31
+ /**
32
+ * Convert a tilde path to an absolute path: `~/dev` → `/Users/sindresorhus/dev`.
33
+ *
34
+ * Also expands `~username` when the username matches the current user.
35
+ */
36
+ expand: untildify,
37
+ /**
38
+ * Convert an absolute path to a tilde path: `/Users/sindresorhus/dev` → `~/dev`.
39
+ */
40
+ fold: tildify
41
+ };
42
+ //#endregion
43
+ //#region src/fs/glob.ts
44
+ function customFinderSort(files) {
45
+ return finderSort(files, { locale: "zh-CN" });
46
+ }
47
+ async function matchFromList(list, pattern, fgOptions) {
48
+ const hasSlash = [pattern].flat().some((p) => p.includes("/"));
49
+ const baseNameMatch = fgOptions?.baseNameMatch ?? !hasSlash;
50
+ const caseSensitive = fgOptions?.caseSensitiveMatch ?? true;
51
+ list = list.map((x) => path.resolve(x));
52
+ const queue = [];
53
+ for (const item of list) {
54
+ if (await isSymlink(item)) continue;
55
+ if (await isDirectory(item)) {
56
+ const files = await fg(pattern, {
57
+ cwd: item,
58
+ absolute: true,
59
+ onlyFiles: true,
60
+ followSymbolicLinks: false,
61
+ caseSensitiveMatch: caseSensitive,
62
+ baseNameMatch,
63
+ ...fgOptions
64
+ });
65
+ queue.push(...customFinderSort(files));
66
+ } else if (await isFile(item)) queue.push(...mm([item], pattern, {
67
+ basename: baseNameMatch,
68
+ nocase: !caseSensitive
69
+ }));
70
+ }
71
+ return queue;
72
+ }
73
+ //#endregion
74
+ //#region src/fs/legacy-file.ts
75
+ async function processLegacyFiles(targetFile, legacyFiles) {
76
+ for (const legacyFile of legacyFiles) if (await fse.exists(legacyFile)) {
77
+ if (!await fse.exists(targetFile)) await fse.rename(legacyFile, targetFile);
78
+ else await fse.rm(legacyFile);
79
+ }
80
+ }
81
+ //#endregion
82
+ //#region src/fs/local-file.ts
83
+ var LocalFile = class {
84
+ filePath;
85
+ /** 目录 */
86
+ dir;
87
+ /** 字段包含 name: 表示长名, 包括扩展名 */
88
+ basename;
89
+ /** 字段不含 name: 表示短名, 不包括扩展名 */
90
+ base;
91
+ /** 字段包含 name: 表示长名, 包括 . */
92
+ extname;
93
+ /** 字段不含 name: 表示短名, 不包括 . */
94
+ ext;
95
+ constructor(filePath) {
96
+ this.filePath = filePath;
97
+ this.filePath = path.resolve(filePath).normalize();
98
+ const file = this.filePath;
99
+ this.dir = path.dirname(file);
100
+ this.basename = path.basename(file);
101
+ this.base = path.basename(file, path.extname(file));
102
+ this.extname = path.extname(file);
103
+ this.ext = path.extname(file).slice(1);
104
+ }
105
+ };
106
+ const newLocalFile = (filePath) => new LocalFile(filePath);
107
+ const localFile = memoize(newLocalFile);
108
+ //#endregion
109
+ //#region src/fs/rename.ts
110
+ function transformFilename(file, { baseSuffix = "", transformBase, newExt }) {
111
+ const dir = path.dirname(file);
112
+ let ext = path.extname(file);
113
+ let base = path.basename(file, ext);
114
+ if (newExt) {
115
+ ext = newExt;
116
+ if (!ext.startsWith(".")) ext = `.${ext}`;
117
+ }
118
+ if (baseSuffix) base += baseSuffix;
119
+ if (transformBase) base = transformBase(base);
120
+ return path.join(dir, base + ext);
121
+ }
122
+ //#endregion
123
+ //#region src/fs/symlink.ts
124
+ /**
125
+ * `ln -sf`, but safe
126
+ *
127
+ * ## About Safe
128
+ * 1. if `path` not exists, create the symlink. (✅ safe)
129
+ * 2. if `path` exists as a symlink, delete existing symlink & create new one. (✅ safe)
130
+ * 3. if `path` exists as a normal file(not a symlink), an error will be thrown. (❌ not safe, since u may lost data)
131
+ *
132
+ * ## Features
133
+ * - `path` support tilde(~)
134
+ */
135
+ async function lnsfSafe(target, path, { onExistingFile = "throw" } = {}) {
136
+ if (path.startsWith("~")) path = untildify(path);
137
+ await ensureDir(dirname(path));
138
+ if (!await symlinkExists(path)) {
139
+ await symlink(target, path);
140
+ return;
141
+ }
142
+ if ((await lstat(path)).isSymbolicLink()) await rm(path);
143
+ else if (onExistingFile === "delete") await rm(path);
144
+ else throw new Error(`path (${path}) in \`symlink(target,path)\` exists, it's a normal file(none symlink)`);
145
+ await symlink(target, path);
146
+ }
147
+ /**
148
+ * `ln -sf`, but safe
149
+ *
150
+ * ## About Safe
151
+ * 1. if `path` not exists, create the symlink. (✅ safe)
152
+ * 2. if `path` exists as a symlink, delete existing symlink & create new one. (✅ safe)
153
+ * 3. if `path` exists as a normal file(not a symlink), an error will be thrown. (❌ not safe, since u may lost data)
154
+ *
155
+ * ## Features
156
+ * - `path` support tilde(~)
157
+ */
158
+ function lnsfSafeSync(target, path, { onExistingFile = "throw" } = {}) {
159
+ if (path.startsWith("~")) path = untildify(path);
160
+ ensureDirSync(dirname(path));
161
+ if (!symlinkExistsSync(path)) {
162
+ symlinkSync(target, path);
163
+ return;
164
+ }
165
+ if (lstatSync(path).isSymbolicLink()) rmSync(path);
166
+ else if (onExistingFile === "delete") rmSync(path);
167
+ else throw new Error(`path (${path}) in \`symlink(target,path)\` exists, it's a normal file(none symlink)`);
168
+ symlinkSync(target, path);
169
+ }
170
+ async function symlinkExists(symlinkPath) {
171
+ try {
172
+ await lstat(symlinkPath);
173
+ } catch {
174
+ return false;
175
+ }
176
+ return true;
177
+ }
178
+ function symlinkExistsSync(symlinkPath) {
179
+ try {
180
+ lstatSync(symlinkPath);
181
+ } catch {
182
+ return false;
183
+ }
184
+ return true;
185
+ }
186
+ //#endregion
187
+ //#region src/parallel.ts
188
+ function getConcurrency() {
189
+ let concurrency = availableParallelism();
190
+ if (process.env.UV_THREADPOOL_SIZE) {
191
+ const parsed = z.coerce.number().min(concurrency).safeParse(process.env.UV_THREADPOOL_SIZE);
192
+ if (parsed.success) concurrency = parsed.data;
193
+ }
194
+ return concurrency;
195
+ }
196
+ function newPQueue() {
197
+ return new PQueue({ concurrency: getConcurrency() });
198
+ }
199
+ //#endregion
200
+ //#region src/shell/index.ts
201
+ /**
202
+ * @deprecated use shlex.quote instead
203
+ */
204
+ function escapeShellArg(val) {
205
+ return shellEscape([val]);
206
+ }
207
+ /**
208
+ * @deprecated use shlex.quote instead
209
+ */
210
+ const escapeShellArgs = shellEscape;
211
+ //#endregion
212
+ export { HomeDir, LocalFile, customFinderSort, escapeShellArg, escapeShellArgs, getConcurrency, lnsfSafe, lnsfSafeSync, localFile, matchFromList, newPQueue, processLegacyFiles, searchDirUp, symlinkExists, symlinkExistsSync, transformFilename };
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "zhegu",
3
+ "type": "module",
4
+ "version": "3.8.0",
5
+ "description": "Arya's needle, little but powerful.",
6
+ "author": "magicdawn",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/magicdawn/zhegu",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/magicdawn/zhegu.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/magicdawn/zhegu/issues"
15
+ },
16
+ "keywords": [
17
+ "needle",
18
+ "kit"
19
+ ],
20
+ "exports": {
21
+ "./package.json": "./package.json",
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ }
26
+ },
27
+ "main": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "files": [
30
+ "CHANGELOG.md",
31
+ "LICENSE",
32
+ "README.md",
33
+ "dist/",
34
+ "package.json"
35
+ ],
36
+ "publishConfig": {
37
+ "registry": "https://registry.npmjs.org",
38
+ "access": "public"
39
+ },
40
+ "engines": {
41
+ "node": ">=20"
42
+ },
43
+ "dependencies": {
44
+ "@humanfs/node": "^0.17.0",
45
+ "@magicdawn/finder-sort": "^0.4.0",
46
+ "fast-glob": "^3.3.3",
47
+ "fs-extra": "^11.4.0",
48
+ "memoize": "^11.0.0",
49
+ "micromatch": "^4.0.8",
50
+ "p-queue": "^9.3.3",
51
+ "path-type": "^6.0.0",
52
+ "shell-escape": "^0.2.0",
53
+ "tildify": "^3.0.0",
54
+ "untildify": "^6.0.0",
55
+ "zod": "^4.6.2"
56
+ },
57
+ "devDependencies": {
58
+ "@magicdawn/eslint-config": "^1.6.17",
59
+ "@magicdawn/prettier-config": "^0.1.0",
60
+ "@types/fs-extra": "^11.0.4",
61
+ "@types/micromatch": "^4.0.10",
62
+ "@types/node": "^26.5.1",
63
+ "@types/shell-escape": "^0.2.3",
64
+ "@typescript-eslint/eslint-plugin": "^8.70.0",
65
+ "@typescript-eslint/parser": "^8.70.0",
66
+ "@typescript/native-preview": "7.0.0-dev.20260707.2",
67
+ "@vitest/coverage-v8": "^5.0.0",
68
+ "eslint": "^10.10.0",
69
+ "eslint-config-prettier": "^10.1.8",
70
+ "husky": "^9.1.7",
71
+ "lint-staged": "^17.5.1",
72
+ "prettier": "^3.9.6",
73
+ "tsdown": "^0.23.0",
74
+ "typescript": "^6.0.3",
75
+ "vitest": "^5.0.0"
76
+ },
77
+ "lint-staged": {
78
+ "*": [
79
+ "eslint --fix --no-warn-ignored",
80
+ "prettier --write --ignore-unknown"
81
+ ]
82
+ },
83
+ "scripts": {
84
+ "test": "vitest run",
85
+ "test:dev": "vitest",
86
+ "test-cover": "vitest run --coverage",
87
+ "dev": "tsdown --watch",
88
+ "build": "tsdown",
89
+ "typeswrong": "attw --pack .",
90
+ "typecheck": "tsgo --noEmit"
91
+ }
92
+ }