UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

57 lines • 2.58 kB
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= import { PathSpec } from "../../../types.js"; import { hasGlob, unmarkGlobs } from "../../../utils/glob_walk.js"; import { dottedSpelling, posixNormpath } from "../../../utils/path.js"; import { rstripSlash, stripSlash } from "../../../utils/slash.js"; /** * Build the PathSpec for a word typed relative to cwd. * * The typed word and the cwd it was typed under are two halves of one * path: `virtual` resolves the pair to an absolute path, `rawPath` * keeps the typed spelling for display, and `dotted` the spelling a walk * proves when the word steps through a name with `.` or `..`. Glob chars * in the word make a pattern spec (unresolved), its matches respelled from * the walk; words whose resolved path has no mount stay plain text. */ export function relativeSpec(word, registry, cwd, access) { const path = posixNormpath(`${rstripSlash(cwd)}/${word}`); if (registry.tryMountFor(unmarkGlobs(path)) === null) return word; const lastSlash = path.lastIndexOf('/'); if (hasGlob(word, access)) { return new PathSpec({ vfsPath: stripSlash(path), virtual: path, directory: path.slice(0, lastSlash + 1), pattern: path.slice(lastSlash + 1), resolved: false, rawPath: word, dotted: dottedSpelling(word, cwd), }); } // The empty name joins onto the directory as the directory itself, a // path the kernel walk never reaches (POSIX: a null pathname does not // resolve), so it rides along refused rather than as the cwd. return new PathSpec({ vfsPath: stripSlash(path), virtual: path, directory: path.slice(0, lastSlash + 1), resolved: true, rawPath: word, dotted: dottedSpelling(word, cwd), walkError: word === '' ? 'ENOENT' : null, }); } //# sourceMappingURL=relative.js.map