aico-pack
Version:
A tool to pack repository contents to single file for AI consumption
104 lines • 4.94 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import path from 'node:path';
import readline from 'node:readline/promises';
import { RepomixError } from '../../shared/errorHandle.js';
import { logger } from '../../shared/logger.js';
/**
* Filters and validates lines from stdin input.
* Removes empty lines and comments (lines starting with #).
*/
export const filterValidLines = (lines) => {
return lines.map((line) => line.trim()).filter((line) => line && !line.startsWith('#'));
};
/**
* Resolves relative paths to absolute paths and deduplicates them.
*/
export const resolveAndDeduplicatePaths = (lines, cwd) => {
const resolvedPaths = lines.map((line) => {
const filePath = path.isAbsolute(line) ? path.normalize(line) : path.normalize(path.resolve(cwd, line));
logger.trace(`Resolved path: ${line} -> ${filePath}`);
return filePath;
});
return [...new Set(resolvedPaths)];
};
/**
* Reads lines from a readable stream using readline interface.
*/
export const readLinesFromStream = (input_1, ...args_1) => __awaiter(void 0, [input_1, ...args_1], void 0, function* (input, createInterface = readline.createInterface) {
var _a, e_1, _b, _c;
const rl = createInterface({ input });
const lines = [];
try {
for (var _d = true, rl_1 = __asyncValues(rl), rl_1_1; rl_1_1 = yield rl_1.next(), _a = rl_1_1.done, !_a; _d = true) {
_c = rl_1_1.value;
_d = false;
const line = _c;
lines.push(line);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = rl_1.return)) yield _b.call(rl_1);
}
finally { if (e_1) throw e_1.error; }
}
return lines;
});
/**
* Reads file paths from stdin, one per line.
* Filters out empty lines and comments (lines starting with #).
* Converts relative paths to absolute paths based on the current working directory.
*/
export const readFilePathsFromStdin = (cwd_1, ...args_1) => __awaiter(void 0, [cwd_1, ...args_1], void 0, function* (cwd, deps = {
stdin: process.stdin,
createReadlineInterface: readline.createInterface,
}) {
logger.trace('Reading file paths from stdin...');
try {
const { stdin, createReadlineInterface } = deps;
// Check if stdin is a TTY (interactive mode)
if (stdin.isTTY) {
throw new RepomixError('No data provided via stdin. Please pipe file paths to repomix when using --stdin flag.');
}
// Read all lines from stdin
const rawLines = yield readLinesFromStream(stdin, createReadlineInterface);
// Filter out empty lines and comments
const validLines = filterValidLines(rawLines);
if (validLines.length === 0) {
throw new RepomixError('No valid file paths found in stdin input.');
}
// Convert relative paths to absolute paths and deduplicate
const filePaths = resolveAndDeduplicatePaths(validLines, cwd);
logger.trace(`Found ${filePaths.length} file paths from stdin`);
return {
filePaths,
emptyDirPaths: [], // Empty directories not supported with stdin input
};
}
catch (error) {
if (error instanceof RepomixError) {
throw error;
}
if (error instanceof Error) {
throw new RepomixError(`Failed to read file paths from stdin: ${error.message}`);
}
throw new RepomixError('An unexpected error occurred while reading from stdin.');
}
});
//# sourceMappingURL=fileStdin.js.map