-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·297 lines (257 loc) · 9.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env node
import { program } from "commander";
import fs from "fs-extra";
import path from "path";
import chalk from "chalk";
import simpleGit from "simple-git";
import mime from "mime-types";
import iconv from "iconv-lite";
import { globby } from "globby";
import ProgressBar from "progress";
import ignore from "ignore";
class RepositoryConverter {
constructor(options = {}) {
this.maxFileSize = (options.maxFileSize || 100) * 1024; // Default: 100 KB
this.baseExcludePatterns = [
"**/node_modules/**",
"**/.git/**",
"**/.venv/**",
"**/venv/**",
"**/__pycache__/**",
"**/dist/**",
"**/build/**",
"**/*.exe",
"**/*.dll",
"**/*.so",
"**/*.dylib",
"**/*.pyc",
"**/*.pyo",
"**/*.class",
"**/*.jar",
];
this.logger = this.createLogger();
this.ignoreFilter = null;
}
createLogger() {
return {
info: (message) => console.log(chalk.blue(message)),
success: (message) => console.log(chalk.green(message)),
warn: (message) => console.warn(chalk.yellow(message)),
error: (message) => console.error(chalk.red(message)),
};
}
async loadGitignore(repoPath) {
const gitignorePath = path.join(repoPath, ".gitignore");
try {
const gitignoreContent = await fs.readFile(gitignorePath, "utf8");
this.ignoreFilter = ignore().add(gitignoreContent);
} catch {
this.ignoreFilter = ignore(); // Fallback to an empty ignore filter
}
}
async filterFiles(basePath) {
await this.loadGitignore(basePath);
try {
const files = await globby(["**/*"], {
cwd: basePath,
onlyFiles: true,
absolute: true,
ignore: this.baseExcludePatterns,
dot: true,
followSymbolicLinks: false,
});
return files.filter((file) => {
const relativePath = path.relative(basePath, file);
if (this.ignoreFilter.ignores(relativePath)) return false;
const stats = fs.statSync(file);
if (stats.size > this.maxFileSize) return false;
const mimeType = mime.lookup(file) || "text/plain"; // Default to text if MIME type isn't found
return (
mimeType.startsWith("text/") || mimeType.includes("json")
);
});
} catch (error) {
this.logger.error(`Error filtering files: ${error.message}`);
return [];
}
}
async readFileWithEncoding(filePath, encoding = "utf8") {
try {
const buffer = await fs.readFile(filePath);
return iconv.decode(buffer, encoding);
} catch (error) {
this.logger.warn(
`Error reading file ${filePath}: ${error.message}`
);
return "";
}
}
async convertRepository(repoPath, outputPath) {
this.logger.info(`Processing repository: ${repoPath}`);
const outputStream = fs.createWriteStream(outputPath, {
encoding: "utf8",
});
outputStream.write(`# Repository: ${repoPath}\n`);
outputStream.write("=".repeat(50) + "\n\n");
const files = await this.filterFiles(repoPath);
if (!files.length) {
this.logger.warn("No valid files found to process.");
outputStream.write("No valid files found to process.\n");
outputStream.end();
return;
}
const progressBar = new ProgressBar(
"Processing [:bar] :percent :etas",
{
complete: "=",
incomplete: " ",
width: 40,
total: files.length,
}
);
for (const file of files) {
try {
const relativePath = path.relative(repoPath, file);
const content = await this.readFileWithEncoding(file);
outputStream.write(`### SOURCE FILE: ${relativePath}\n`);
outputStream.write("-".repeat(50) + "\n");
outputStream.write(content + "\n\n");
progressBar.tick();
} catch (error) {
this.logger.warn(`Error processing ${file}: ${error.message}`);
}
}
outputStream.end();
this.logger.success(
`Conversion complete. Output written to ${outputPath}`
);
}
async cloneAndConvert(repoUrl, outputPath) {
const tempDir = await fs.mkdtemp(path.join(process.cwd(), "totxt-"));
try {
this.logger.info(`Cloning repository: ${repoUrl}`);
await simpleGit().clone(repoUrl, tempDir);
await this.convertRepository(tempDir, outputPath);
} catch (error) {
this.logger.error(`Cloning failed: ${error.message}`);
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
}
}
class RepositoryStructureRecreator {
constructor() {
this.logger = this.createLogger();
}
createLogger() {
return {
info: (message) => console.log(chalk.blue(message)),
success: (message) => console.log(chalk.green(message)),
warn: (message) => console.warn(chalk.yellow(message)),
error: (message) => console.error(chalk.red(message)),
};
}
async generateStructureFromOutput(txtFile, basePath) {
try {
await fs.access(txtFile);
const outputDir = path.join(
basePath,
path.basename(txtFile, ".txt")
);
await fs.ensureDir(outputDir);
this.logger.info(`Creating structure in: ${outputDir}`);
const fileContent = await fs.readFile(txtFile, "utf8");
const lines = fileContent.split("\n");
let currentFilePath = null;
let currentFileContent = [];
for (const line of lines) {
if (line.startsWith("### SOURCE FILE:")) {
if (currentFilePath) {
await this.writeFile(
outputDir,
currentFilePath,
currentFileContent
);
}
currentFilePath = line.split("### SOURCE FILE:")[1].trim();
currentFileContent = [];
} else if (currentFilePath) {
if (!line.startsWith("----") && line.trim()) {
currentFileContent.push(line);
}
}
}
if (currentFilePath) {
await this.writeFile(
outputDir,
currentFilePath,
currentFileContent
);
}
this.logger.success(
`Structure recreated successfully in ${outputDir}`
);
} catch (error) {
this.logger.error(`Error recreating structure: ${error.message}`);
process.exit(1);
}
}
async writeFile(outputDir, filePath, content) {
const fullPath = path.join(outputDir, filePath);
try {
await fs.ensureDir(path.dirname(fullPath));
await fs.writeFile(fullPath, content.join("\n"));
this.logger.info(`Created file: ${fullPath}`);
} catch (error) {
this.logger.warn(
`Could not create file ${fullPath}: ${error.message}`
);
}
}
}
program
.name("totxt")
.description("Convert repository to text and recreate structure")
.version("2.2.0");
program
.command("create")
.description("Convert repository to text file")
.argument("<repo-path>", "Local path or GitHub repository URL")
.option("-m, --max-size <size>", "Maximum file size in KB", "100")
.option("-o, --output <filename>", "Custom output filename")
.action(async (repoPath, options) => {
const converter = new RepositoryConverter({
maxFileSize: parseInt(options.maxSize, 10),
});
// Resolve directory name for output file
const resolvedPath = path.resolve(
repoPath === "." ? process.cwd() : repoPath
);
const dirName = path.basename(resolvedPath);
const outputFile =
options.output ||
`${dirName.replace(/[^a-z0-9]/gi, "_")}_output.txt`;
try {
if (repoPath.includes("github.com")) {
await converter.cloneAndConvert(repoPath, outputFile);
} else {
await converter.convertRepository(resolvedPath, outputFile);
}
} catch (error) {
console.error("Conversion failed:", error);
}
});
program
.command("recreate")
.description("Recreate repository structure from text file")
.argument("<txt-file>", "Text file containing repository contents")
.option(
"-b, --base-path <path>",
"Base path where structure will be created",
"."
)
.action(async (txtFile, options) => {
const recreator = new RepositoryStructureRecreator();
await recreator.generateStructureFromOutput(txtFile, options.basePath);
});
program.parse(process.argv);