UNPKG

@velcro/strategy-fs

Version:

Velcro resolver strategy backed by an fs-compatible object

86 lines (82 loc) 2.53 kB
import { Uri, NotResolvableError } from '@velcro/common'; import { AbstractResolverStrategyWithRoot, ResolverStrategy } from '@velcro/resolver'; class FsStrategy extends AbstractResolverStrategyWithRoot { constructor(options) { super(options.rootUri || Uri.file('/')); this.fs = options.fs; } ensureUriUnderRoot(uri) { if (!Uri.isPrefixOf(this.rootUri, uri)) { return new NotResolvableError(`The URI '${uri}' is not under the root for this resolver strategy '${this.rootUri}'`); } } async getCanonicalUrl(_ctx, uri) { const err = this.ensureUriUnderRoot(uri); if (err) { throw err; } try { const realpath = await this.fs.promises.realpath(uri.fsPath); return { uri: Uri.file(realpath), }; } catch (err) { if (err && err.code === 'ENOENT') { return { uri, }; } throw err; } } getRootUrl() { return { uri: this.rootUri }; } getResolveRoot(_ctx, uri) { const err = this.ensureUriUnderRoot(uri); if (err) { return Promise.reject(err); } return { uri: this.rootUri }; } async listEntries(_ctx, uri) { const err = this.ensureUriUnderRoot(uri); if (err) { throw err; } const fsEntries = await this.fs.promises.readdir(uri.fsPath, { encoding: 'utf-8', withFileTypes: true, }); const result = { entries: [] }; for (const entry of fsEntries) { if (entry.isDirectory()) { result.entries.push({ type: ResolverStrategy.EntryKind.Directory, uri: Uri.joinPath(uri, entry.name), }); } else if (entry.isFile()) { result.entries.push({ type: ResolverStrategy.EntryKind.File, uri: Uri.joinPath(uri, entry.name), }); } } return result; } async readFileContent(_ctx, uri) { const err = this.ensureUriUnderRoot(uri); if (err) { throw err; } const content = await this.fs.promises.readFile(uri.fsPath); return { content, }; } } const version = '0.56.2'; export { FsStrategy, version }; //# sourceMappingURL=index.js.map