@iqai/adk-cli
Version:
CLI tool for creating, running, and testing ADK-TS agents
84 lines • 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathUtils = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
class PathUtils {
logger;
quiet;
constructor(logger, quiet = false) {
this.logger = logger;
this.quiet = quiet;
}
normalizePath(path) {
return path.replace(/\\/g, "/");
}
parseTsConfigPaths(projectRoot) {
const tsconfigPath = (0, node_path_1.join)(projectRoot, "tsconfig.json");
if (!(0, node_fs_1.existsSync)(tsconfigPath))
return {};
try {
const tsconfig = JSON.parse((0, node_fs_1.readFileSync)(tsconfigPath, "utf-8"));
const { baseUrl, paths } = tsconfig.compilerOptions || {};
return { baseUrl, paths };
}
catch (error) {
this.logger.warn(`Failed to parse tsconfig.json: ${String(error)}`);
return {};
}
}
createExternalizePlugin() {
return {
name: "externalize-bare-imports",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const isAbsolutePath = /^[a-zA-Z]:/.test(args.path);
if (args.path.startsWith(".") ||
args.path.startsWith("/") ||
args.path.startsWith("..") ||
isAbsolutePath)
return;
return { path: args.path, external: true };
});
},
};
}
createPathMappingPlugin(projectRoot) {
const { baseUrl, paths } = this.parseTsConfigPaths(projectRoot);
const resolvedBaseUrl = baseUrl
? (0, node_path_1.resolve)(projectRoot, baseUrl)
: projectRoot;
const logger = this.logger;
const quiet = this.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 regex = new RegExp(`^${alias.replace("*", "(.*)")}$`);
const match = args.path.match(regex);
if (match) {
for (const mapping of mappings) {
const resolvedPath = mapping.includes("*")
? mapping.replace("*", match[1])
: mapping;
const fullPath = (0, node_path_1.normalize)((0, node_path_1.resolve)(resolvedBaseUrl, resolvedPath));
for (const ext of [".ts", ".js", ".tsx", ".jsx", ""]) {
if ((0, node_fs_1.existsSync)(fullPath + ext))
return { path: fullPath + ext };
}
}
}
}
}
return;
});
},
};
}
}
exports.PathUtils = PathUtils;
//# sourceMappingURL=path-utils.js.map