vscode-fs 0.0.3 → 0.0.4

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/dist/index.cjs CHANGED
@@ -46,6 +46,54 @@ let FileSystemProviderErrorCode = /* @__PURE__ */ function(FileSystemProviderErr
46
46
  FileSystemProviderErrorCode["Unknown"] = "Unknown";
47
47
  return FileSystemProviderErrorCode;
48
48
  }({});
49
+ /**
50
+ * A relative pattern is a helper to construct glob patterns that are matched
51
+ * relatively to a base file path. The base path can either be an absolute file
52
+ * path as string or uri or a {@link WorkspaceFolder workspace folder}, which is the
53
+ * preferred way of creating the relative pattern.
54
+ */
55
+ var RelativePattern = class {
56
+ /**
57
+ * A base file path to which this pattern will be matched against relatively. The
58
+ * file path must be absolute, should not have any trailing path separators and
59
+ * not include any relative segments (`.` or `..`).
60
+ */
61
+ baseUri;
62
+ /**
63
+ * A file glob pattern like `*.{ts,js}` that will be matched on file paths
64
+ * relative to the base path.
65
+ *
66
+ * Example: Given a base of `/home/work/folder` and a file path of `/home/work/folder/index.js`,
67
+ * the file glob pattern will match on `index.js`.
68
+ */
69
+ pattern;
70
+ /**
71
+ * Creates a new relative pattern object with a base file path and pattern to match. This pattern
72
+ * will be matched on file paths relative to the base.
73
+ *
74
+ * Example:
75
+ * ```ts
76
+ * const folder = vscode.workspace.workspaceFolders?.[0];
77
+ * if (folder) {
78
+ *
79
+ * // Match any TypeScript file in the root of this workspace folder
80
+ * const pattern1 = new vscode.RelativePattern(folder, '*.ts');
81
+ *
82
+ * // Match any TypeScript file in `someFolder` inside this workspace folder
83
+ * const pattern2 = new vscode.RelativePattern(folder, 'someFolder/*.ts');
84
+ * }
85
+ * ```
86
+ *
87
+ * @param base A base to which this pattern will be matched against relatively. It is recommended
88
+ * to pass in a {@link WorkspaceFolder workspace folder} if the pattern should match inside the workspace.
89
+ * Otherwise, a uri or string should only be used if the pattern is for a file path outside the workspace.
90
+ * @param pattern A file glob pattern like `*.{ts,js}` that will be matched on paths relative to the base.
91
+ */
92
+ constructor(base, pattern) {
93
+ this.baseUri = base;
94
+ this.pattern = pattern;
95
+ }
96
+ };
49
97
 
50
98
  //#endregion
51
99
  //#region src/error.ts
@@ -119,7 +167,11 @@ function joinPath(basePath, ...segments) {
119
167
  return vscode_uri.Utils.joinPath(vscode_uri.URI.file(basePath), ...segments).fsPath;
120
168
  }
121
169
  async function createNodeFileSystem() {
122
- const [fs, trash] = await Promise.all([import("node:fs"), import("trash").then((m) => m.default)]);
170
+ const [fs, trash, glob] = await Promise.all([
171
+ import("node:fs"),
172
+ import("trash").then((m) => m.default),
173
+ import("tinyglobby").then((m) => m.glob)
174
+ ]);
123
175
  async function resolveFileType(path) {
124
176
  const lstats = await fs.promises.lstat(path);
125
177
  if (!lstats.isSymbolicLink()) return {
@@ -169,6 +221,9 @@ async function createNodeFileSystem() {
169
221
  }
170
222
  throw createFileSystemError(`Unsupported file type: ${sourcePath}`, FileSystemProviderErrorCode.Unknown);
171
223
  }
224
+ function pathToUris(pathToUris) {
225
+ return pathToUris.map((path) => vscode_uri.URI.file(path));
226
+ }
172
227
  return {
173
228
  stat: (uri) => wrap(async () => {
174
229
  const { type, stats } = await resolveFileType(uri.fsPath);
@@ -254,6 +309,21 @@ async function createNodeFileSystem() {
254
309
  } catch {
255
310
  return false;
256
311
  }
312
+ },
313
+ glob: async (pattern, options) => {
314
+ return pathToUris(await glob(pattern.pattern, {
315
+ absolute: true,
316
+ cwd: pattern.baseUri.fsPath,
317
+ onlyFiles: options?.onlyFiles,
318
+ onlyDirectories: options?.onlyDirectories,
319
+ followSymbolicLinks: options?.followSymbolicLinks,
320
+ ignore: options?.ignore,
321
+ dot: options?.dot,
322
+ expandDirectories: options?.expandDirectories,
323
+ extglob: options?.extglob,
324
+ deep: options?.deep,
325
+ fs
326
+ }));
257
327
  }
258
328
  };
259
329
  }
@@ -267,6 +337,7 @@ Object.defineProperty(exports, 'FileSystemError', {
267
337
  });
268
338
  exports.FileSystemProviderErrorCode = FileSystemProviderErrorCode;
269
339
  exports.FileType = FileType;
340
+ exports.RelativePattern = RelativePattern;
270
341
  exports.createFileSystemError = createFileSystemError;
271
342
  exports.createNodeFileSystem = createNodeFileSystem;
272
343
  exports.toFileSystemError = toFileSystemError;
package/dist/index.d.cts CHANGED
@@ -146,6 +146,72 @@ interface FileSystem {
146
146
  * @throws It will not throw any errors if the file, directory, or symbolic link does not exist.
147
147
  */
148
148
  exists(uri: URI): Promise<FileStat | false>;
149
+ /**
150
+ * Glob files by pattern.
151
+ *
152
+ * @param globPattern The glob pattern.
153
+ * @param options The options for the glob.
154
+ * @param options.onlyFiles Only include files in the results. Default is `true`.
155
+ * @param options.onlyDirectories Only include directories in the results. Default is `false`.
156
+ * @param options.followSymbolicLinks Follow symbolic links in the results. Default is `true`.
157
+ * @param options.ignore Ignore files in the results. Default is `[]`.
158
+ * @param options.dot Include files and directories that start with a dot like `.gitignore`. Default is `true`.
159
+ * @param options.expandDirectories Whether to automatically expand directory patterns. Default is `true`. Important to disable if migrating from [`fast-glob`](https://github.com/mrmlnc/fast-glob).
160
+ * @param options.extglob Enables support for extglobs, like `+(pattern)`. Default is `true`.
161
+ * @param options.deep Maximum directory depth to crawl. Default is `Infinity`.
162
+ * @returns An array of file uris.
163
+ */
164
+ glob(globPattern: RelativePattern, options?: {
165
+ /**
166
+ * Only include files in the results.
167
+ *
168
+ * @default true
169
+ */
170
+ onlyFiles?: boolean;
171
+ /**
172
+ * Only include directories in the results.
173
+ *
174
+ * @default false
175
+ */
176
+ onlyDirectories?: boolean;
177
+ /**
178
+ * Follow symbolic links in the results.
179
+ *
180
+ * @default true
181
+ */
182
+ followSymbolicLinks?: boolean;
183
+ /**
184
+ * Ignore files in the results.
185
+ *
186
+ * @default []
187
+ */
188
+ ignore?: string[];
189
+ /**
190
+ * Include files and directories that start with a dot like `.gitignore`.
191
+ *
192
+ * @default true
193
+ */
194
+ dot?: boolean;
195
+ /**
196
+ * Whether to automatically expand directory patterns.
197
+ *
198
+ * Important to disable if migrating from [`fast-glob`](https://github.com/mrmlnc/fast-glob).
199
+ *
200
+ * @default true
201
+ */
202
+ expandDirectories?: boolean;
203
+ /**
204
+ * Enables support for extglobs, like `+(pattern)`.
205
+ *
206
+ * @default true
207
+ */
208
+ extglob?: boolean;
209
+ /**
210
+ * Maximum directory depth to crawl.
211
+ * @default Infinity
212
+ */
213
+ deep?: number;
214
+ }): Promise<URI[]>;
149
215
  }
150
216
  /**
151
217
  * The `FileStat`-type represents metadata about a file
@@ -218,6 +284,51 @@ declare enum FileSystemProviderErrorCode {
218
284
  Unavailable = "Unavailable",
219
285
  Unknown = "Unknown"
220
286
  }
287
+ /**
288
+ * A relative pattern is a helper to construct glob patterns that are matched
289
+ * relatively to a base file path. The base path can either be an absolute file
290
+ * path as string or uri or a {@link WorkspaceFolder workspace folder}, which is the
291
+ * preferred way of creating the relative pattern.
292
+ */
293
+ declare class RelativePattern {
294
+ /**
295
+ * A base file path to which this pattern will be matched against relatively. The
296
+ * file path must be absolute, should not have any trailing path separators and
297
+ * not include any relative segments (`.` or `..`).
298
+ */
299
+ baseUri: URI;
300
+ /**
301
+ * A file glob pattern like `*.{ts,js}` that will be matched on file paths
302
+ * relative to the base path.
303
+ *
304
+ * Example: Given a base of `/home/work/folder` and a file path of `/home/work/folder/index.js`,
305
+ * the file glob pattern will match on `index.js`.
306
+ */
307
+ pattern: string;
308
+ /**
309
+ * Creates a new relative pattern object with a base file path and pattern to match. This pattern
310
+ * will be matched on file paths relative to the base.
311
+ *
312
+ * Example:
313
+ * ```ts
314
+ * const folder = vscode.workspace.workspaceFolders?.[0];
315
+ * if (folder) {
316
+ *
317
+ * // Match any TypeScript file in the root of this workspace folder
318
+ * const pattern1 = new vscode.RelativePattern(folder, '*.ts');
319
+ *
320
+ * // Match any TypeScript file in `someFolder` inside this workspace folder
321
+ * const pattern2 = new vscode.RelativePattern(folder, 'someFolder/*.ts');
322
+ * }
323
+ * ```
324
+ *
325
+ * @param base A base to which this pattern will be matched against relatively. It is recommended
326
+ * to pass in a {@link WorkspaceFolder workspace folder} if the pattern should match inside the workspace.
327
+ * Otherwise, a uri or string should only be used if the pattern is for a file path outside the workspace.
328
+ * @param pattern A file glob pattern like `*.{ts,js}` that will be matched on paths relative to the base.
329
+ */
330
+ constructor(base: URI, pattern: string);
331
+ }
221
332
  //#endregion
222
333
  //#region src/error.d.ts
223
334
  declare function createFileSystemError(error: Error | string, code: FileSystemProviderErrorCode): FileSystemError;
@@ -226,4 +337,4 @@ declare function toFileSystemError(error: NodeJS.ErrnoException): FileSystemErro
226
337
  //#region src/node.d.ts
227
338
  declare function createNodeFileSystem(): Promise<FileSystem>;
228
339
  //#endregion
229
- export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, IsDirectory, IsFile, IsSymbolicLink, createFileSystemError, createNodeFileSystem, toFileSystemError };
340
+ export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, IsDirectory, IsFile, IsSymbolicLink, RelativePattern, createFileSystemError, createNodeFileSystem, toFileSystemError };
package/dist/index.d.mts CHANGED
@@ -146,6 +146,72 @@ interface FileSystem {
146
146
  * @throws It will not throw any errors if the file, directory, or symbolic link does not exist.
147
147
  */
148
148
  exists(uri: URI): Promise<FileStat | false>;
149
+ /**
150
+ * Glob files by pattern.
151
+ *
152
+ * @param globPattern The glob pattern.
153
+ * @param options The options for the glob.
154
+ * @param options.onlyFiles Only include files in the results. Default is `true`.
155
+ * @param options.onlyDirectories Only include directories in the results. Default is `false`.
156
+ * @param options.followSymbolicLinks Follow symbolic links in the results. Default is `true`.
157
+ * @param options.ignore Ignore files in the results. Default is `[]`.
158
+ * @param options.dot Include files and directories that start with a dot like `.gitignore`. Default is `true`.
159
+ * @param options.expandDirectories Whether to automatically expand directory patterns. Default is `true`. Important to disable if migrating from [`fast-glob`](https://github.com/mrmlnc/fast-glob).
160
+ * @param options.extglob Enables support for extglobs, like `+(pattern)`. Default is `true`.
161
+ * @param options.deep Maximum directory depth to crawl. Default is `Infinity`.
162
+ * @returns An array of file uris.
163
+ */
164
+ glob(globPattern: RelativePattern, options?: {
165
+ /**
166
+ * Only include files in the results.
167
+ *
168
+ * @default true
169
+ */
170
+ onlyFiles?: boolean;
171
+ /**
172
+ * Only include directories in the results.
173
+ *
174
+ * @default false
175
+ */
176
+ onlyDirectories?: boolean;
177
+ /**
178
+ * Follow symbolic links in the results.
179
+ *
180
+ * @default true
181
+ */
182
+ followSymbolicLinks?: boolean;
183
+ /**
184
+ * Ignore files in the results.
185
+ *
186
+ * @default []
187
+ */
188
+ ignore?: string[];
189
+ /**
190
+ * Include files and directories that start with a dot like `.gitignore`.
191
+ *
192
+ * @default true
193
+ */
194
+ dot?: boolean;
195
+ /**
196
+ * Whether to automatically expand directory patterns.
197
+ *
198
+ * Important to disable if migrating from [`fast-glob`](https://github.com/mrmlnc/fast-glob).
199
+ *
200
+ * @default true
201
+ */
202
+ expandDirectories?: boolean;
203
+ /**
204
+ * Enables support for extglobs, like `+(pattern)`.
205
+ *
206
+ * @default true
207
+ */
208
+ extglob?: boolean;
209
+ /**
210
+ * Maximum directory depth to crawl.
211
+ * @default Infinity
212
+ */
213
+ deep?: number;
214
+ }): Promise<URI[]>;
149
215
  }
150
216
  /**
151
217
  * The `FileStat`-type represents metadata about a file
@@ -218,6 +284,51 @@ declare enum FileSystemProviderErrorCode {
218
284
  Unavailable = "Unavailable",
219
285
  Unknown = "Unknown"
220
286
  }
287
+ /**
288
+ * A relative pattern is a helper to construct glob patterns that are matched
289
+ * relatively to a base file path. The base path can either be an absolute file
290
+ * path as string or uri or a {@link WorkspaceFolder workspace folder}, which is the
291
+ * preferred way of creating the relative pattern.
292
+ */
293
+ declare class RelativePattern {
294
+ /**
295
+ * A base file path to which this pattern will be matched against relatively. The
296
+ * file path must be absolute, should not have any trailing path separators and
297
+ * not include any relative segments (`.` or `..`).
298
+ */
299
+ baseUri: URI;
300
+ /**
301
+ * A file glob pattern like `*.{ts,js}` that will be matched on file paths
302
+ * relative to the base path.
303
+ *
304
+ * Example: Given a base of `/home/work/folder` and a file path of `/home/work/folder/index.js`,
305
+ * the file glob pattern will match on `index.js`.
306
+ */
307
+ pattern: string;
308
+ /**
309
+ * Creates a new relative pattern object with a base file path and pattern to match. This pattern
310
+ * will be matched on file paths relative to the base.
311
+ *
312
+ * Example:
313
+ * ```ts
314
+ * const folder = vscode.workspace.workspaceFolders?.[0];
315
+ * if (folder) {
316
+ *
317
+ * // Match any TypeScript file in the root of this workspace folder
318
+ * const pattern1 = new vscode.RelativePattern(folder, '*.ts');
319
+ *
320
+ * // Match any TypeScript file in `someFolder` inside this workspace folder
321
+ * const pattern2 = new vscode.RelativePattern(folder, 'someFolder/*.ts');
322
+ * }
323
+ * ```
324
+ *
325
+ * @param base A base to which this pattern will be matched against relatively. It is recommended
326
+ * to pass in a {@link WorkspaceFolder workspace folder} if the pattern should match inside the workspace.
327
+ * Otherwise, a uri or string should only be used if the pattern is for a file path outside the workspace.
328
+ * @param pattern A file glob pattern like `*.{ts,js}` that will be matched on paths relative to the base.
329
+ */
330
+ constructor(base: URI, pattern: string);
331
+ }
221
332
  //#endregion
222
333
  //#region src/error.d.ts
223
334
  declare function createFileSystemError(error: Error | string, code: FileSystemProviderErrorCode): FileSystemError;
@@ -226,4 +337,4 @@ declare function toFileSystemError(error: NodeJS.ErrnoException): FileSystemErro
226
337
  //#region src/node.d.ts
227
338
  declare function createNodeFileSystem(): Promise<FileSystem>;
228
339
  //#endregion
229
- export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, IsDirectory, IsFile, IsSymbolicLink, createFileSystemError, createNodeFileSystem, toFileSystemError };
340
+ export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, IsDirectory, IsFile, IsSymbolicLink, RelativePattern, createFileSystemError, createNodeFileSystem, toFileSystemError };
package/dist/index.mjs CHANGED
@@ -45,6 +45,54 @@ let FileSystemProviderErrorCode = /* @__PURE__ */ function(FileSystemProviderErr
45
45
  FileSystemProviderErrorCode["Unknown"] = "Unknown";
46
46
  return FileSystemProviderErrorCode;
47
47
  }({});
48
+ /**
49
+ * A relative pattern is a helper to construct glob patterns that are matched
50
+ * relatively to a base file path. The base path can either be an absolute file
51
+ * path as string or uri or a {@link WorkspaceFolder workspace folder}, which is the
52
+ * preferred way of creating the relative pattern.
53
+ */
54
+ var RelativePattern = class {
55
+ /**
56
+ * A base file path to which this pattern will be matched against relatively. The
57
+ * file path must be absolute, should not have any trailing path separators and
58
+ * not include any relative segments (`.` or `..`).
59
+ */
60
+ baseUri;
61
+ /**
62
+ * A file glob pattern like `*.{ts,js}` that will be matched on file paths
63
+ * relative to the base path.
64
+ *
65
+ * Example: Given a base of `/home/work/folder` and a file path of `/home/work/folder/index.js`,
66
+ * the file glob pattern will match on `index.js`.
67
+ */
68
+ pattern;
69
+ /**
70
+ * Creates a new relative pattern object with a base file path and pattern to match. This pattern
71
+ * will be matched on file paths relative to the base.
72
+ *
73
+ * Example:
74
+ * ```ts
75
+ * const folder = vscode.workspace.workspaceFolders?.[0];
76
+ * if (folder) {
77
+ *
78
+ * // Match any TypeScript file in the root of this workspace folder
79
+ * const pattern1 = new vscode.RelativePattern(folder, '*.ts');
80
+ *
81
+ * // Match any TypeScript file in `someFolder` inside this workspace folder
82
+ * const pattern2 = new vscode.RelativePattern(folder, 'someFolder/*.ts');
83
+ * }
84
+ * ```
85
+ *
86
+ * @param base A base to which this pattern will be matched against relatively. It is recommended
87
+ * to pass in a {@link WorkspaceFolder workspace folder} if the pattern should match inside the workspace.
88
+ * Otherwise, a uri or string should only be used if the pattern is for a file path outside the workspace.
89
+ * @param pattern A file glob pattern like `*.{ts,js}` that will be matched on paths relative to the base.
90
+ */
91
+ constructor(base, pattern) {
92
+ this.baseUri = base;
93
+ this.pattern = pattern;
94
+ }
95
+ };
48
96
 
49
97
  //#endregion
50
98
  //#region src/error.ts
@@ -118,7 +166,11 @@ function joinPath(basePath, ...segments) {
118
166
  return Utils.joinPath(URI.file(basePath), ...segments).fsPath;
119
167
  }
120
168
  async function createNodeFileSystem() {
121
- const [fs, trash] = await Promise.all([import("node:fs"), import("trash").then((m) => m.default)]);
169
+ const [fs, trash, glob] = await Promise.all([
170
+ import("node:fs"),
171
+ import("trash").then((m) => m.default),
172
+ import("tinyglobby").then((m) => m.glob)
173
+ ]);
122
174
  async function resolveFileType(path) {
123
175
  const lstats = await fs.promises.lstat(path);
124
176
  if (!lstats.isSymbolicLink()) return {
@@ -168,6 +220,9 @@ async function createNodeFileSystem() {
168
220
  }
169
221
  throw createFileSystemError(`Unsupported file type: ${sourcePath}`, FileSystemProviderErrorCode.Unknown);
170
222
  }
223
+ function pathToUris(pathToUris) {
224
+ return pathToUris.map((path) => URI.file(path));
225
+ }
171
226
  return {
172
227
  stat: (uri) => wrap(async () => {
173
228
  const { type, stats } = await resolveFileType(uri.fsPath);
@@ -253,9 +308,24 @@ async function createNodeFileSystem() {
253
308
  } catch {
254
309
  return false;
255
310
  }
311
+ },
312
+ glob: async (pattern, options) => {
313
+ return pathToUris(await glob(pattern.pattern, {
314
+ absolute: true,
315
+ cwd: pattern.baseUri.fsPath,
316
+ onlyFiles: options?.onlyFiles,
317
+ onlyDirectories: options?.onlyDirectories,
318
+ followSymbolicLinks: options?.followSymbolicLinks,
319
+ ignore: options?.ignore,
320
+ dot: options?.dot,
321
+ expandDirectories: options?.expandDirectories,
322
+ extglob: options?.extglob,
323
+ deep: options?.deep,
324
+ fs
325
+ }));
256
326
  }
257
327
  };
258
328
  }
259
329
 
260
330
  //#endregion
261
- export { FileSystemError, FileSystemProviderErrorCode, FileType, createFileSystemError, createNodeFileSystem, toFileSystemError };
331
+ export { FileSystemError, FileSystemProviderErrorCode, FileType, RelativePattern, createFileSystemError, createNodeFileSystem, toFileSystemError };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vscode-fs",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.4",
5
5
  "description": "VSCode like simple、serializable and cross-platform file system utilities.",
6
6
  "author": "Naily Zero <zero@naily.cc> (https://naily.cc)",
7
7
  "license": "MIT",
@@ -45,6 +45,7 @@
45
45
  "vscode-uri": "^3.1.0"
46
46
  },
47
47
  "dependencies": {
48
+ "tinyglobby": "^0.2.15",
48
49
  "trash": "^10.1.1"
49
50
  },
50
51
  "devDependencies": {