UNPKG

@velcro/strategy-fs

Version:

Velcro resolver strategy backed by an fs-compatible object

91 lines (85 loc) 2.65 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var common = require('@velcro/common'); var resolver = require('@velcro/resolver'); class FsStrategy extends resolver.AbstractResolverStrategyWithRoot { constructor(options) { super(options.rootUri || common.Uri.file('/')); this.fs = options.fs; } ensureUriUnderRoot(uri) { if (!common.Uri.isPrefixOf(this.rootUri, uri)) { return new common.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: common.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: resolver.ResolverStrategy.EntryKind.Directory, uri: common.Uri.joinPath(uri, entry.name), }); } else if (entry.isFile()) { result.entries.push({ type: resolver.ResolverStrategy.EntryKind.File, uri: common.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'; exports.FsStrategy = FsStrategy; exports.version = version; //# sourceMappingURL=index.js.map