-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcmd-index.ts
More file actions
288 lines (267 loc) · 10 KB
/
cmd-index.ts
File metadata and controls
288 lines (267 loc) · 10 KB
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
/**
* Index command - Index a data source
*/
import { Command } from "commander";
import { Indexer } from "../core/indexer.js";
import { Source } from "../sources/types.js";
import { FilesystemStore } from "../stores/filesystem.js";
import { getS3Config } from "../stores/s3-config.js";
import { buildClientUserAgent } from "../core/utils.js";
import { parseSourceUrl } from "../core/url-parser.js";
// Shared store options
interface StoreOptions {
index?: string;
store: string;
storePath?: string;
}
function addStoreOptions(cmd: Command): Command {
return cmd
.option("-i, --index <name>", "Index name (creates named index in indexes/ subdirectory)")
.option("--store <type>", "Store type: filesystem, s3 (S3 requires CC_S3_* env vars)", "filesystem")
.option("--store-path <path>", "Store base path (files stored directly here if no --index)");
}
/**
* Create a store based on options.
*/
async function createStore(options: StoreOptions) {
if (options.store === "filesystem") {
return new FilesystemStore({ basePath: options.storePath });
} else if (options.store === "s3") {
const s3Config = getS3Config();
if (!s3Config.bucket) {
console.error("S3 store requires CC_S3_BUCKET environment variable");
process.exit(1);
}
const { S3Store } = await import("../stores/s3.js");
return new S3Store(s3Config);
} else {
console.error(`Unknown store type: ${options.store}`);
process.exit(1);
}
}
async function runIndex(
source: Source,
store: Awaited<ReturnType<typeof createStore>>,
indexKey: string,
sourceType: string
) {
console.log(`Indexing ${sourceType} source...`);
const indexer = new Indexer({
clientUserAgent: buildClientUserAgent("cli"),
});
const result = await indexer.index(source, store, indexKey);
console.log(`\nIndexing complete!`);
console.log(` Type: ${result.type}`);
console.log(` Duration: ${result.duration}ms`);
console.log();
console.log(`Summary:`);
console.log(` Total files: ${result.filesIndexed}`);
if (result.filesNewOrModified > 0) {
console.log(` - New/modified: ${result.filesNewOrModified} (uploaded and indexed)`);
}
if (result.filesUnchanged > 0) {
console.log(` - Unchanged: ${result.filesUnchanged} (skipped)`);
}
if (result.filesRemoved > 0) {
console.log(` Files removed: ${result.filesRemoved}`);
}
}
// GitHub subcommand
const githubCommand = new Command("github")
.description("Index a GitHub repository")
.requiredOption("--owner <owner>", "Repository owner")
.requiredOption("--repo <repo>", "Repository name")
.option("--ref <ref>", "Branch, tag, or commit", "HEAD");
addStoreOptions(githubCommand);
githubCommand.action(async (options) => {
try {
const { GitHubSource } = await import("../sources/github.js");
const source = new GitHubSource({
owner: options.owner,
repo: options.repo,
ref: options.ref,
});
const store = await createStore(options);
const indexKey = options.index || ".";
await runIndex(source, store, indexKey, "github");
} catch (error) {
console.error("Indexing failed:", error);
process.exit(1);
}
});
// GitLab subcommand
const gitlabCommand = new Command("gitlab")
.description("Index a GitLab project")
.requiredOption("--project <id>", "Project ID or path (e.g., group/project)")
.option("--ref <ref>", "Branch, tag, or commit", "HEAD")
.option("--gitlab-url <url>", "GitLab base URL (for self-hosted)", "https://gitlab.com");
addStoreOptions(gitlabCommand);
gitlabCommand.action(async (options) => {
try {
const { GitLabSource } = await import("../sources/gitlab.js");
const source = new GitLabSource({
baseUrl: options.gitlabUrl,
projectId: options.project,
ref: options.ref,
});
const store = await createStore(options);
const indexKey = options.index || ".";
await runIndex(source, store, indexKey, "gitlab");
} catch (error) {
console.error("Indexing failed:", error);
process.exit(1);
}
});
// BitBucket subcommand
const bitbucketCommand = new Command("bitbucket")
.description("Index a Bitbucket repository")
.requiredOption("--workspace <slug>", "Workspace slug")
.requiredOption("--repo <repo>", "Repository name")
.option("--ref <ref>", "Branch, tag, or commit", "HEAD")
.option("--bitbucket-url <url>", "Bitbucket base URL (for Server/Data Center)", "https://api.bitbucket.org/2.0");
addStoreOptions(bitbucketCommand);
bitbucketCommand.action(async (options) => {
try {
const { BitBucketSource } = await import("../sources/bitbucket.js");
const source = new BitBucketSource({
baseUrl: options.bitbucketUrl,
workspace: options.workspace,
repo: options.repo,
ref: options.ref,
});
const store = await createStore(options);
const indexKey = options.index || ".";
await runIndex(source, store, indexKey, "bitbucket");
} catch (error) {
console.error("Indexing failed:", error);
process.exit(1);
}
});
// Website subcommand
const websiteCommand = new Command("website")
.description("Crawl and index a website")
.requiredOption("--url <url>", "Website URL to crawl")
.option("--max-depth <n>", "Maximum crawl depth", (v) => parseInt(v, 10), 3)
.option("--max-pages <n>", "Maximum pages to crawl", (v) => parseInt(v, 10), 100)
.option("--include <patterns...>", "URL patterns to include (glob)")
.option("--exclude <patterns...>", "URL patterns to exclude (glob)")
.option("--save-content <dir>", "[Debug] Save crawled content to directory for inspection");
addStoreOptions(websiteCommand);
websiteCommand.action(async (options) => {
try {
const { WebsiteSource } = await import("../sources/website.js");
const source = new WebsiteSource({
url: options.url,
maxDepth: options.maxDepth,
maxPages: options.maxPages,
includePaths: options.include,
excludePaths: options.exclude,
});
// Save content locally if requested (this also triggers the crawl)
if (options.saveContent) {
const fs = await import("fs/promises");
const path = await import("path");
const files = await source.fetchAll();
const dir = path.resolve(options.saveContent);
await fs.mkdir(dir, { recursive: true });
let savedCount = 0;
for (const file of files) {
// Sanitize file.path to prevent path traversal attacks:
// 1. Normalize to resolve any .. segments
// 2. Ensure the result stays within the target directory
const safePath = path.normalize(file.path).replace(/^(\.\.[/\\])+/, "");
const filePath = path.resolve(dir, safePath);
if (!filePath.startsWith(dir + path.sep) && filePath !== dir) {
console.warn(`Skipping file with unsafe path: ${file.path}`);
continue;
}
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, file.contents, "utf-8");
savedCount++;
}
console.log(`Saved ${savedCount} files to ${dir}`);
// Note: source.fetchAll() caches results internally, so subsequent calls
// in runIndex will reuse the crawled data
}
const store = await createStore(options);
const indexKey = options.index || ".";
await runIndex(source, store, indexKey, "website");
} catch (error) {
console.error("Indexing failed:", error);
process.exit(1);
}
});
// URL-based indexing command (auto-detects source type)
const urlCommand = new Command("url")
.description("Index from URL with auto-detection (used internally when URL is passed directly)")
.argument("<url>", "URL of the repository or website to index")
.option("--ref <ref>", "Branch, tag, or commit (overrides URL-detected ref)");
addStoreOptions(urlCommand);
urlCommand.action(async (url: string, options) => {
try {
// Parse the URL to determine source type and config
const parsed = parseSourceUrl(url);
const indexKey = options.index || parsed.defaultIndexName;
let source: Source;
switch (parsed.type) {
case "github": {
const { GitHubSource } = await import("../sources/github.js");
const config = parsed.config as import("../sources/github.js").GitHubSourceConfig;
source = new GitHubSource({
...config,
ref: options.ref || config.ref,
});
break;
}
case "gitlab": {
const { GitLabSource } = await import("../sources/gitlab.js");
const config = parsed.config as import("../sources/gitlab.js").GitLabSourceConfig;
source = new GitLabSource({
...config,
ref: options.ref || config.ref,
});
break;
}
case "bitbucket": {
const { BitBucketSource } = await import("../sources/bitbucket.js");
const config = parsed.config as import("../sources/bitbucket.js").BitBucketSourceConfig;
source = new BitBucketSource({
...config,
ref: options.ref || config.ref,
});
break;
}
case "website": {
const { WebsiteSource } = await import("../sources/website.js");
const config = parsed.config as import("../sources/website.js").WebsiteSourceConfig;
source = new WebsiteSource(config);
break;
}
default:
throw new Error(`Unknown source type: ${parsed.type}`);
}
const store = await createStore(options);
await runIndex(source, store, indexKey, parsed.type);
} catch (error) {
if (error instanceof Error && error.message.includes("Invalid")) {
console.error(`Error parsing URL: ${error.message}`);
} else {
console.error("Indexing failed:", error);
}
process.exit(1);
}
});
// Main index command
export const indexCommand = new Command("index")
.usage("<url | source> [options]")
.description("Index a data source")
.addHelpText('after', `
Examples:
ctxc index https://github.com/owner/repo
ctxc index https://github.com/owner/repo -i myindex
ctxc index github --owner x --repo y`)
.addCommand(urlCommand, { hidden: true })
.addCommand(githubCommand)
.addCommand(gitlabCommand)
.addCommand(bitbucketCommand)
.addCommand(websiteCommand);