prompt-helper
Version:
A CLI tool to help you create and manage prompts for AI models.
128 lines • 4.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectTsconfig = collectTsconfig;
// src/collectors/tsconfigCollector.ts
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const jsonc_parser_1 = require("jsonc-parser");
/**
* Loads and recursively resolves a `tsconfig.json` file including:
* - `extends` inheritance
* - `references` for composite projects
* - Comment-stripped parsing using `jsonc-parser`
*
* @param tsconfigPath - The absolute path to the tsconfig file.
* @param visitedConfigs - Set of already visited configs to prevent circular merges.
* @returns A merged `Tsconfig` object.
*/
function collectTsconfigInfo(tsconfigPath, visitedConfigs = new Set()) {
if (visitedConfigs.has(tsconfigPath)) {
// Prevent circular references from breaking recursion
return {};
}
visitedConfigs.add(tsconfigPath);
let tsconfig;
try {
const tsconfigData = fs.readFileSync(tsconfigPath, 'utf8');
tsconfig = (0, jsonc_parser_1.parse)(tsconfigData);
}
catch (err) {
console.error(`Error reading or parsing ${tsconfigPath}:`, err);
return {};
}
let mergedConfig = { ...tsconfig };
// Handle "extends"
if (tsconfig.extends) {
const parentPath = path.resolve(path.dirname(tsconfigPath), tsconfig.extends);
const parentConfig = collectTsconfigInfo(parentPath, visitedConfigs);
mergedConfig = mergeTsconfigs(parentConfig, mergedConfig);
}
// Handle project "references"
if (Array.isArray(tsconfig.references)) {
for (const ref of tsconfig.references) {
const refPath = path.resolve(path.dirname(tsconfigPath), ref.path, 'tsconfig.json');
const refConfig = collectTsconfigInfo(refPath, visitedConfigs);
mergedConfig = mergeTsconfigs(mergedConfig, refConfig);
}
}
return mergedConfig;
}
/**
* Merges two tsconfig objects, preferring values from the `overrideConfig`.
*
* @param baseConfig - The base (inherited) config.
* @param overrideConfig - The overriding config.
* @returns The merged configuration.
*/
function mergeTsconfigs(baseConfig, overrideConfig) {
return {
...baseConfig,
...overrideConfig,
compilerOptions: {
...baseConfig.compilerOptions,
...overrideConfig.compilerOptions,
},
include: uniqueArray(baseConfig.include, overrideConfig.include),
exclude: uniqueArray(baseConfig.exclude, overrideConfig.exclude),
files: uniqueArray(baseConfig.files, overrideConfig.files),
};
}
/**
* Merges multiple arrays of strings and removes duplicates.
*
* @param arrays - Any number of string arrays.
* @returns A deduplicated array of all entries.
*/
function uniqueArray(...arrays) {
const filtered = arrays.flat().filter((arr) => !!arr);
return Array.from(new Set(filtered));
}
/**
* Collects TypeScript configuration from `tsconfig.json` if it exists.
*
* @param baseDir - The root directory of the project.
* @param projectInfo - The project info object to populate with tsconfig data.
*/
function collectTsconfig(baseDir, projectInfo) {
const tsconfigPath = path.join(baseDir, 'tsconfig.json');
if (fs.existsSync(tsconfigPath)) {
projectInfo.tsconfigJson = collectTsconfigInfo(tsconfigPath);
}
else {
console.error('Could not find tsconfig.json at project root.');
}
}
//# sourceMappingURL=tsconfigCollector.js.map