vscode-fs 0.0.2 → 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);
@@ -199,7 +254,77 @@ async function createNodeFileSystem() {
199
254
  copy: (source, target, options) => wrap(async () => {
200
255
  const overwrite = options?.overwrite !== false;
201
256
  await copyRecursive(source.fsPath, target.fsPath, overwrite);
202
- })
257
+ }),
258
+ isFile: async (uri) => {
259
+ try {
260
+ const { type, stats } = await resolveFileType(uri.fsPath);
261
+ if (type !== FileType.File) return false;
262
+ return {
263
+ type,
264
+ ctime: stats.birthtime.getTime(),
265
+ mtime: stats.mtime.getTime(),
266
+ size: stats.size
267
+ };
268
+ } catch {
269
+ return false;
270
+ }
271
+ },
272
+ isDirectory: async (uri) => {
273
+ try {
274
+ const { type, stats } = await resolveFileType(uri.fsPath);
275
+ if (type !== FileType.Directory) return false;
276
+ return {
277
+ type,
278
+ ctime: stats.birthtime.getTime(),
279
+ mtime: stats.mtime.getTime(),
280
+ size: stats.size
281
+ };
282
+ } catch {
283
+ return false;
284
+ }
285
+ },
286
+ isSymbolicLink: async (uri) => {
287
+ try {
288
+ const { type, stats } = await resolveFileType(uri.fsPath);
289
+ if (type !== FileType.SymbolicLink) return false;
290
+ return {
291
+ type,
292
+ ctime: stats.birthtime.getTime(),
293
+ mtime: stats.mtime.getTime(),
294
+ size: stats.size
295
+ };
296
+ } catch {
297
+ return false;
298
+ }
299
+ },
300
+ exists: async (uri) => {
301
+ try {
302
+ const { type, stats } = await resolveFileType(uri.fsPath);
303
+ return {
304
+ type,
305
+ ctime: stats.birthtime.getTime(),
306
+ mtime: stats.mtime.getTime(),
307
+ size: stats.size
308
+ };
309
+ } catch {
310
+ return false;
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
+ }));
327
+ }
203
328
  };
204
329
  }
205
330
 
@@ -212,6 +337,7 @@ Object.defineProperty(exports, 'FileSystemError', {
212
337
  });
213
338
  exports.FileSystemProviderErrorCode = FileSystemProviderErrorCode;
214
339
  exports.FileType = FileType;
340
+ exports.RelativePattern = RelativePattern;
215
341
  exports.createFileSystemError = createFileSystemError;
216
342
  exports.createNodeFileSystem = createNodeFileSystem;
217
343
  exports.toFileSystemError = toFileSystemError;
package/dist/index.d.cts CHANGED
@@ -114,6 +114,104 @@ interface FileSystem {
114
114
  */
115
115
  overwrite?: boolean;
116
116
  }): Promise<void>;
117
+ /**
118
+ * Check if a file exists.
119
+ *
120
+ * @param uri The uri of the file.
121
+ * @returns True if the file exists, false otherwise.
122
+ * @throws It will not throw any errors if the file does not exist.
123
+ */
124
+ isFile(uri: URI): Promise<IsFile | false>;
125
+ /**
126
+ * Check if a directory exists.
127
+ *
128
+ * @param uri The uri of the directory.
129
+ * @returns True if the directory exists, false otherwise.
130
+ * @throws It will not throw any errors if the directory does not exist.
131
+ */
132
+ isDirectory(uri: URI): Promise<IsDirectory | false>;
133
+ /**
134
+ * Check if a symbolic link exists.
135
+ *
136
+ * @param uri The uri of the symbolic link.
137
+ * @returns True if the symbolic link exists, false otherwise.
138
+ * @throws It will not throw any errors if the symbolic link does not exist.
139
+ */
140
+ isSymbolicLink(uri: URI): Promise<IsSymbolicLink | false>;
141
+ /**
142
+ * Check if the path is exists.
143
+ *
144
+ * @param uri The uri of the file, directory, or symbolic link.
145
+ * @returns True if the file, directory, or symbolic link exists, false otherwise.
146
+ * @throws It will not throw any errors if the file, directory, or symbolic link does not exist.
147
+ */
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[]>;
117
215
  }
118
216
  /**
119
217
  * The `FileStat`-type represents metadata about a file
@@ -147,6 +245,15 @@ interface FileStat {
147
245
  */
148
246
  size: number;
149
247
  }
248
+ type IsFile = Omit<FileStat, 'type'> & {
249
+ type: FileType.File;
250
+ };
251
+ type IsDirectory = Omit<FileStat, 'type'> & {
252
+ type: FileType.Directory;
253
+ };
254
+ type IsSymbolicLink = Omit<FileStat, 'type'> & {
255
+ type: FileType.SymbolicLink;
256
+ };
150
257
  /**
151
258
  * A type that filesystem providers should use to signal errors.
152
259
  *
@@ -177,6 +284,51 @@ declare enum FileSystemProviderErrorCode {
177
284
  Unavailable = "Unavailable",
178
285
  Unknown = "Unknown"
179
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
+ }
180
332
  //#endregion
181
333
  //#region src/error.d.ts
182
334
  declare function createFileSystemError(error: Error | string, code: FileSystemProviderErrorCode): FileSystemError;
@@ -185,4 +337,4 @@ declare function toFileSystemError(error: NodeJS.ErrnoException): FileSystemErro
185
337
  //#region src/node.d.ts
186
338
  declare function createNodeFileSystem(): Promise<FileSystem>;
187
339
  //#endregion
188
- export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, createFileSystemError, createNodeFileSystem, toFileSystemError };
340
+ export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, IsDirectory, IsFile, IsSymbolicLink, RelativePattern, createFileSystemError, createNodeFileSystem, toFileSystemError };
package/dist/index.d.mts CHANGED
@@ -114,6 +114,104 @@ interface FileSystem {
114
114
  */
115
115
  overwrite?: boolean;
116
116
  }): Promise<void>;
117
+ /**
118
+ * Check if a file exists.
119
+ *
120
+ * @param uri The uri of the file.
121
+ * @returns True if the file exists, false otherwise.
122
+ * @throws It will not throw any errors if the file does not exist.
123
+ */
124
+ isFile(uri: URI): Promise<IsFile | false>;
125
+ /**
126
+ * Check if a directory exists.
127
+ *
128
+ * @param uri The uri of the directory.
129
+ * @returns True if the directory exists, false otherwise.
130
+ * @throws It will not throw any errors if the directory does not exist.
131
+ */
132
+ isDirectory(uri: URI): Promise<IsDirectory | false>;
133
+ /**
134
+ * Check if a symbolic link exists.
135
+ *
136
+ * @param uri The uri of the symbolic link.
137
+ * @returns True if the symbolic link exists, false otherwise.
138
+ * @throws It will not throw any errors if the symbolic link does not exist.
139
+ */
140
+ isSymbolicLink(uri: URI): Promise<IsSymbolicLink | false>;
141
+ /**
142
+ * Check if the path is exists.
143
+ *
144
+ * @param uri The uri of the file, directory, or symbolic link.
145
+ * @returns True if the file, directory, or symbolic link exists, false otherwise.
146
+ * @throws It will not throw any errors if the file, directory, or symbolic link does not exist.
147
+ */
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[]>;
117
215
  }
118
216
  /**
119
217
  * The `FileStat`-type represents metadata about a file
@@ -147,6 +245,15 @@ interface FileStat {
147
245
  */
148
246
  size: number;
149
247
  }
248
+ type IsFile = Omit<FileStat, 'type'> & {
249
+ type: FileType.File;
250
+ };
251
+ type IsDirectory = Omit<FileStat, 'type'> & {
252
+ type: FileType.Directory;
253
+ };
254
+ type IsSymbolicLink = Omit<FileStat, 'type'> & {
255
+ type: FileType.SymbolicLink;
256
+ };
150
257
  /**
151
258
  * A type that filesystem providers should use to signal errors.
152
259
  *
@@ -177,6 +284,51 @@ declare enum FileSystemProviderErrorCode {
177
284
  Unavailable = "Unavailable",
178
285
  Unknown = "Unknown"
179
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
+ }
180
332
  //#endregion
181
333
  //#region src/error.d.ts
182
334
  declare function createFileSystemError(error: Error | string, code: FileSystemProviderErrorCode): FileSystemError;
@@ -185,4 +337,4 @@ declare function toFileSystemError(error: NodeJS.ErrnoException): FileSystemErro
185
337
  //#region src/node.d.ts
186
338
  declare function createNodeFileSystem(): Promise<FileSystem>;
187
339
  //#endregion
188
- export { FileStat, FileSystem, FileSystemError, FileSystemProviderErrorCode, FileType, 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);
@@ -198,9 +253,79 @@ async function createNodeFileSystem() {
198
253
  copy: (source, target, options) => wrap(async () => {
199
254
  const overwrite = options?.overwrite !== false;
200
255
  await copyRecursive(source.fsPath, target.fsPath, overwrite);
201
- })
256
+ }),
257
+ isFile: async (uri) => {
258
+ try {
259
+ const { type, stats } = await resolveFileType(uri.fsPath);
260
+ if (type !== FileType.File) return false;
261
+ return {
262
+ type,
263
+ ctime: stats.birthtime.getTime(),
264
+ mtime: stats.mtime.getTime(),
265
+ size: stats.size
266
+ };
267
+ } catch {
268
+ return false;
269
+ }
270
+ },
271
+ isDirectory: async (uri) => {
272
+ try {
273
+ const { type, stats } = await resolveFileType(uri.fsPath);
274
+ if (type !== FileType.Directory) return false;
275
+ return {
276
+ type,
277
+ ctime: stats.birthtime.getTime(),
278
+ mtime: stats.mtime.getTime(),
279
+ size: stats.size
280
+ };
281
+ } catch {
282
+ return false;
283
+ }
284
+ },
285
+ isSymbolicLink: async (uri) => {
286
+ try {
287
+ const { type, stats } = await resolveFileType(uri.fsPath);
288
+ if (type !== FileType.SymbolicLink) return false;
289
+ return {
290
+ type,
291
+ ctime: stats.birthtime.getTime(),
292
+ mtime: stats.mtime.getTime(),
293
+ size: stats.size
294
+ };
295
+ } catch {
296
+ return false;
297
+ }
298
+ },
299
+ exists: async (uri) => {
300
+ try {
301
+ const { type, stats } = await resolveFileType(uri.fsPath);
302
+ return {
303
+ type,
304
+ ctime: stats.birthtime.getTime(),
305
+ mtime: stats.mtime.getTime(),
306
+ size: stats.size
307
+ };
308
+ } catch {
309
+ return false;
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
+ }));
326
+ }
202
327
  };
203
328
  }
204
329
 
205
330
  //#endregion
206
- 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.2",
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": {