UNPKG

ttsc

Version:

General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.

70 lines 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.packageRootDir = packageRootDir; exports.findNearestGoMod = findNearestGoMod; exports.isOutsideRelativePath = isOutsideRelativePath; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); /** * Walk up the directory tree from `__dirname` until a directory that contains * both `package.json` and `go.mod` is found. This is the `packages/ttsc` root * and must be passed to `buildNativeCompiler` so it can locate `cmd/ttsc`. * * Uses `fs.realpathSync.native` when available to resolve symlinks identically * to the Go toolchain's own path resolution. * * Throws when the root cannot be found (i.e. the package is not in a Go * workspace), which would indicate a broken installation. */ function packageRootDir() { let current = node_path_1.default.resolve(__dirname); while (true) { if (node_fs_1.default.existsSync(node_path_1.default.join(current, "package.json")) && node_fs_1.default.existsSync(node_path_1.default.join(current, "go.mod"))) { return node_fs_1.default.realpathSync.native?.(current) ?? node_fs_1.default.realpathSync(current); } const parent = node_path_1.default.dirname(current); if (parent === current) { throw new Error("ttsc: package root not found for native compiler build"); } current = parent; } } /** * Walk up the directory tree from `from` looking for a `go.mod` file. Stops * after `maxDepth` hops so callers can bound the search to a known * neighbourhood of the plugin source directory. * * Returns the absolute path to the first `go.mod` found, or `null` when no * `go.mod` exists within the depth limit or filesystem root is reached first. */ function findNearestGoMod(from, maxDepth) { let current = node_path_1.default.resolve(from); let depth = 0; while (true) { const candidate = node_path_1.default.join(current, "go.mod"); if (node_fs_1.default.existsSync(candidate)) return candidate; if (depth >= maxDepth) return null; const parent = node_path_1.default.dirname(current); if (parent === current) return null; current = parent; depth += 1; } } /** * Return `true` when a relative path escapes its base directory — i.e. starts * with `..` or is an absolute path. Used by callers that need to guard against * path traversal before building output keys or spawning subprocesses. */ function isOutsideRelativePath(relative) { return (relative === ".." || relative.startsWith(`..${node_path_1.default.sep}`) || node_path_1.default.isAbsolute(relative)); } //# sourceMappingURL=paths.js.map