vscode-find-up 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 vida xie
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,59 @@
1
+ # vscode-find-up
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm bundle size][npm-bundle-size-src]][npm-bundle-size-href]
5
+ [![License][license-src]][license-href]
6
+
7
+ Zero-dependency [find-up](https://github.com/sindresorhus/find-up) for VS Code extension environment. Uses `vscode.workspace.fs` API to support all [file system providers](https://code.visualstudio.com/api/references/vscode-api#FileSystemProvider) (local, remote, virtual, etc.).
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install vscode-find-up
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { findUp, walkUp } from 'vscode-find-up'
19
+
20
+ // Find a file by walking up from cwd
21
+ const uri = await findUp('package.json', {
22
+ cwd: currentFileUri,
23
+ })
24
+
25
+ // Walk parent directories as a generator
26
+ for (const dir of walkUp(startUri)) {
27
+ // ...
28
+ }
29
+ ```
30
+
31
+ ## API
32
+
33
+ ### `findUp(name, options?): Promise<Uri | undefined>`
34
+
35
+ Find a file by name, walking parent directories until found.
36
+
37
+ ### `walkUp(base, options?): Generator<Uri>`
38
+
39
+ Generator that yields each directory from `base` up to the root (or `last`).
40
+
41
+ ### Options
42
+
43
+ | Option | Type | Default | Description |
44
+ | --- | --- | --- | --- |
45
+ | `cwd` | `Uri` | — | The starting directory |
46
+ | `last` | `string \| Uri` | `"/"` | Stop directory (inclusive) |
47
+
48
+ ## License
49
+
50
+ [MIT](./LICENSE) License &copy; 2026-PRESENT [Vida Xie](https://github.com/9romise)
51
+
52
+ <!-- Badges -->
53
+
54
+ [npm-version-src]: https://npmx.dev/api/registry/badge/version/vscode-find-up
55
+ [npm-version-href]: https://npmx.dev/package/vscode-find-up
56
+ [npm-bundle-size-src]: https://npmx.dev/api/registry/badge/size/vscode-find-up
57
+ [npm-bundle-size-href]: https://npmx.dev/package/vscode-find-up
58
+ [license-src]: https://npmx.dev/api/registry/badge/license/vscode-find-up
59
+ [license-href]: https://opensource.org/licenses/MIT
@@ -0,0 +1,36 @@
1
+ import { FileType, Uri } from "vscode";
2
+
3
+ //#region src/access.d.ts
4
+ declare function ok(uri: Uri, type?: FileType): Promise<boolean>;
5
+ //#endregion
6
+ //#region src/walk.d.ts
7
+ interface Options {
8
+ /**
9
+ *
10
+ * @default workspace.getWorkspaceFolder(base)?.uri ?? Uri.file('/')
11
+ */
12
+ cwd?: Uri;
13
+ /**
14
+ * @default options.cwd
15
+ */
16
+ stopAt?: Uri | string;
17
+ }
18
+ declare function up$1(base: Uri, options?: Options): Generator<Uri>;
19
+ //#endregion
20
+ //#region src/find.d.ts
21
+ /**
22
+ * Find an item by name, walking parent directories until found.
23
+ *
24
+ * @param name The item name to find.
25
+ * @returns The {@link Uri} of the item, if found.
26
+ */
27
+ declare function up(name: string, options?: Options): Promise<Uri | undefined>;
28
+ /**
29
+ * Get the first path that matches any of the names provided.
30
+ *
31
+ * @param names The item names to find.
32
+ * @returns The {@link Uri} of the first item found, if any.
33
+ */
34
+ declare function any(names: string[], options?: Options): Promise<Uri | undefined>;
35
+ //#endregion
36
+ export { type Options, ok as accessOk, any as findAny, up as findUp, up$1 as walkUp };
package/dist/index.mjs ADDED
@@ -0,0 +1,68 @@
1
+ import { Uri, workspace } from "vscode";
2
+
3
+ //#region src/access.ts
4
+ async function ok(uri, type) {
5
+ try {
6
+ const stat = await workspace.fs.stat(uri);
7
+ if (type) return stat.type === type;
8
+ return true;
9
+ } catch {
10
+ return false;
11
+ }
12
+ }
13
+
14
+ //#endregion
15
+ //#region src/walk.ts
16
+ function* up$1(base, options = {}) {
17
+ const { cwd } = options;
18
+ const root = (cwd ? workspace.getWorkspaceFolder(cwd)?.uri : workspace.workspaceFolders?.[0].uri) ?? Uri.file("/");
19
+ const last = typeof options.stopAt === "string" ? Uri.joinPath(root, options.stopAt) : options.stopAt ?? root;
20
+ let current = base;
21
+ while (true) {
22
+ yield current;
23
+ if (current.path === last.path) return;
24
+ const parent = Uri.joinPath(current, "..");
25
+ if (parent.path === current.path) return;
26
+ current = parent;
27
+ }
28
+ }
29
+
30
+ //#endregion
31
+ //#region src/find.ts
32
+ /**
33
+ * Find an item by name, walking parent directories until found.
34
+ *
35
+ * @param name The item name to find.
36
+ * @returns The {@link Uri} of the item, if found.
37
+ */
38
+ async function up(name, options = {}) {
39
+ const { cwd } = options;
40
+ const folders = cwd ? [cwd] : workspace.workspaceFolders?.map(({ uri }) => uri);
41
+ if (!folders) return;
42
+ let folder, dir, tmp;
43
+ for (folder of folders) for (dir of up$1(folder, options)) {
44
+ tmp = Uri.joinPath(dir, name);
45
+ if (await ok(tmp)) return tmp;
46
+ }
47
+ }
48
+ /**
49
+ * Get the first path that matches any of the names provided.
50
+ *
51
+ * @param names The item names to find.
52
+ * @returns The {@link Uri} of the first item found, if any.
53
+ */
54
+ async function any(names, options = {}) {
55
+ const { cwd } = options;
56
+ const folders = cwd ? [cwd] : workspace.workspaceFolders?.map(({ uri }) => uri);
57
+ if (!folders) return;
58
+ let folder, dir, tmp;
59
+ let j = 0;
60
+ const len = names.length;
61
+ for (folder of folders) for (dir of up$1(folder, options)) for (j = 0; j < len; j++) {
62
+ tmp = Uri.joinPath(dir, names[j]);
63
+ if (await ok(tmp)) return tmp;
64
+ }
65
+ }
66
+
67
+ //#endregion
68
+ export { ok as accessOk, any as findAny, up as findUp, up$1 as walkUp };
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "vscode-find-up",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "packageManager": "pnpm@10.30.3",
6
+ "author": {
7
+ "name": "Vida Xie",
8
+ "email": "vida_2020@163.com",
9
+ "url": "https://github.com/9romise"
10
+ },
11
+ "license": "MIT",
12
+ "homepage": "https://github.com/9romise/vscode-find-up#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/9romise/vscode-find-up.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/9romise/vscode-find-up/issues"
19
+ },
20
+ "keywords": [
21
+ "vscode",
22
+ "find-up"
23
+ ],
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": "./dist/index.mjs",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "types": "./dist/index.d.mts",
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "dev": "tsdown --watch",
35
+ "build": "tsdown",
36
+ "test": "vitest",
37
+ "lint": "eslint .",
38
+ "lint:fix": "pnpm lint --fix",
39
+ "typecheck": "tsc --noEmit",
40
+ "prepublishOnly": "npm run build"
41
+ },
42
+ "peerDependencies": {
43
+ "@types/vscode": "*"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "catalog:dev",
47
+ "@vida0905/eslint-config": "catalog:dev",
48
+ "eslint": "catalog:dev",
49
+ "jest-mock-vscode": "catalog:dev",
50
+ "nano-staged": "catalog:dev",
51
+ "simple-git-hooks": "catalog:dev",
52
+ "tsdown": "catalog:dev",
53
+ "typescript": "catalog:dev",
54
+ "vitest": "catalog:dev"
55
+ },
56
+ "simple-git-hooks": {
57
+ "pre-commit": "npx nano-staged"
58
+ },
59
+ "nano-staged": {
60
+ "*": "eslint --fix"
61
+ }
62
+ }