@iqai/adk-cli
Version:
CLI tool for creating, running, and testing ADK-TS agents
72 lines • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPathMappingPlugin = createPathMappingPlugin;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const tsconfig_1 = require("./tsconfig");
const utils_1 = require("./utils");
function createPathMappingPlugin(projectRoot, opts = {}) {
const { baseUrl, paths } = (0, tsconfig_1.parseTsConfigPaths)(projectRoot, opts.logger);
const resolvedBaseUrl = baseUrl ? (0, node_path_1.resolve)(projectRoot, baseUrl) : projectRoot;
const logger = opts.logger;
const quiet = !!opts.quiet;
return {
name: "typescript-path-mapping",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (!quiet) {
logger?.debug(`Resolving import: "${args.path}" from "${args.importer || "unknown"}"`);
}
if (paths && !args.path.startsWith(".") && !(0, node_path_1.isAbsolute)(args.path)) {
for (const [alias, mappings] of Object.entries(paths)) {
const aliasPattern = alias.replace("*", "(.*)");
const aliasRegex = new RegExp(`^${aliasPattern}$`);
const match = args.path.match(aliasRegex);
if (match) {
for (const mapping of mappings) {
let resolvedPath = mapping;
if (match[1] && mapping.includes("*")) {
resolvedPath = mapping.replace("*", match[1]);
}
const fullPath = (0, node_path_1.normalize)((0, node_path_1.resolve)(resolvedBaseUrl, resolvedPath));
const extensions = [".ts", ".js", ".tsx", ".jsx", ""];
for (const ext of extensions) {
const pathWithExt = ext ? fullPath + ext : fullPath;
if ((0, node_fs_1.existsSync)(pathWithExt)) {
logger?.debug(`Path mapping resolved: ${args.path} -> ${pathWithExt}`);
return { path: (0, utils_1.normalizePathForEsbuild)(pathWithExt) };
}
}
}
}
}
}
if (args.path === "env" && baseUrl) {
const envPath = (0, node_path_1.resolve)(resolvedBaseUrl, "env");
const extensions = [".ts", ".js"];
for (const ext of extensions) {
const pathWithExt = (0, node_path_1.normalize)(envPath + ext);
if ((0, node_fs_1.existsSync)(pathWithExt)) {
logger?.debug(`Direct env import resolved: ${args.path} -> ${pathWithExt}`);
return { path: (0, utils_1.normalizePathForEsbuild)(pathWithExt) };
}
}
}
if (baseUrl && args.path.startsWith("../")) {
const relativePath = args.path.replace("../", "");
const fullPath = (0, node_path_1.resolve)(resolvedBaseUrl, relativePath);
const extensions = [".ts", ".js", ".tsx", ".jsx", ""];
for (const ext of extensions) {
const pathWithExt = (0, node_path_1.normalize)(ext ? fullPath + ext : fullPath);
if ((0, node_fs_1.existsSync)(pathWithExt)) {
logger?.debug(`Relative import resolved via baseUrl: ${args.path} -> ${pathWithExt}`);
return { path: (0, utils_1.normalizePathForEsbuild)(pathWithExt) };
}
}
}
return;
});
},
};
}
//# sourceMappingURL=path-plugin.js.map