-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
428 lines (354 loc) · 13.5 KB
/
index.js
File metadata and controls
428 lines (354 loc) · 13.5 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const { loadConfig, mergeWithFlags } = require('./lib/config.js');
const { scanProject, buildFileTree, filterFiles } = require('./lib/scanner.js');
const { analyzeFile, analyzeProject, extractSymbols, buildDependencyGraph } = require('./lib/analyzer.js');
const { createBundle, formatAsJSON, formatAsMarkdown } = require('./lib/bundler.js');
const { validateBundle, checkCircularDependencies, checkSymbolConflicts } = require('./lib/validator.js');
const { estimateTokens, calculateBundleSize, estimateSavings } = require('./lib/tokenizer.js');
const { getLicenseStatus, assertProLicense, showUpgradePrompt } = require('./lib/license.js');
const TOOL_NAME = 'contextpack';
const VERSION = (() => {
try {
return require('./package.json').version;
} catch {
return '0.0.0';
}
})();
function printHelp() {
console.log(`
${TOOL_NAME} v${VERSION}
Analyze your codebase and generate compact, token-optimized context bundles for AI coding sessions.
USAGE:
${TOOL_NAME} [command] [options]
COMMANDS:
scan Scan project structure and build file tree
analyze Analyze files, extract symbols and dependencies
bundle Generate a context bundle from analysis
validate Validate an existing context bundle
run Run the full pipeline: scan → summarize → bundle → validate
info Show token estimates and bundle size info for a bundle file
OPTIONS:
-d, --dir <path> Root directory to scan (default: current working directory)
-o, --output <file> Output file path (default: contextpack.json)
-f, --format <fmt> Output format: json | markdown (default: json)
-i, --include <globs> Comma-separated glob patterns to include
-e, --exclude <globs> Comma-separated glob patterns to exclude
-t, --types <exts> Comma-separated file extensions to include (e.g. js,ts,py)
--config <file> Path to config file (default: .contextpackrc or contextpack.config.js)
--no-validate Skip validation step in full pipeline
--no-symbols Skip symbol extraction
--no-deps Skip dependency graph generation
--pro Assert Pro license features
-v, --version Print version number
-h, --help Show this help message
EXAMPLES:
# Run full pipeline on current directory
${TOOL_NAME} run
# Scan a specific directory and output as markdown
${TOOL_NAME} run --dir ./src --format markdown --output context.md
# Only scan and analyze, skip bundling
${TOOL_NAME} analyze --dir ./src --output analysis.json
# Validate an existing bundle
${TOOL_NAME} validate --dir . contextpack.json
# Get token estimates for a bundle
${TOOL_NAME} info contextpack.json
# Include only JS and TS files, exclude tests
${TOOL_NAME} run --types js,ts --exclude "**/*.test.*,**/__tests__/**"
# Use a custom config file
${TOOL_NAME} run --config ./my-contextpack.config.js
`);
}
function printVersion() {
console.log(`${TOOL_NAME} v${VERSION}`);
}
function parseArgs(argv) {
const args = argv.slice(2);
const flags = {
command: null,
dir: process.cwd(),
output: 'contextpack.json',
format: 'json',
include: null,
exclude: null,
types: null,
config: null,
validate: true,
symbols: true,
deps: true,
pro: false,
help: false,
version: false,
positional: [],
};
const commands = new Set(['scan', 'analyze', 'bundle', 'validate', 'run', 'info']);
let i = 0;
while (i < args.length) {
const arg = args[i];
if (arg === '-h' || arg === '--help') {
flags.help = true;
} else if (arg === '-v' || arg === '--version') {
flags.version = true;
} else if (arg === '--no-validate') {
flags.validate = false;
} else if (arg === '--no-symbols') {
flags.symbols = false;
} else if (arg === '--no-deps') {
flags.deps = false;
} else if (arg === '--pro') {
flags.pro = true;
} else if ((arg === '-d' || arg === '--dir') && args[i + 1]) {
flags.dir = path.resolve(args[++i]);
} else if ((arg === '-o' || arg === '--output') && args[i + 1]) {
flags.output = args[++i];
} else if ((arg === '-f' || arg === '--format') && args[i + 1]) {
flags.format = args[++i];
} else if ((arg === '-i' || arg === '--include') && args[i + 1]) {
flags.include = args[++i].split(',').map(s => s.trim());
} else if ((arg === '-e' || arg === '--exclude') && args[i + 1]) {
flags.exclude = args[++i].split(',').map(s => s.trim());
} else if ((arg === '-t' || arg === '--types') && args[i + 1]) {
flags.types = args[++i].split(',').map(s => s.trim().replace(/^\./, ''));
} else if (arg === '--config' && args[i + 1]) {
flags.config = path.resolve(args[++i]);
} else if (!arg.startsWith('-')) {
if (commands.has(arg) && flags.command === null) {
flags.command = arg;
} else {
flags.positional.push(arg);
}
} else {
console.warn(`Warning: Unknown flag "${arg}" — ignoring.`);
}
i++;
}
return flags;
}
function resolveOutputPath(flags) {
return path.resolve(flags.dir, flags.output);
}
function writeOutput(content, outputPath) {
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(outputPath, content, 'utf8');
}
async function cmdScan(flags, config) {
console.log(`[scan] Scanning project at: ${flags.dir}`);
const rawFiles = await scanProject(flags.dir, config);
const fileTree = buildFileTree(rawFiles, flags.dir);
const filterOptions = {
include: flags.include || config.include,
exclude: flags.exclude || config.exclude,
types: flags.types || config.types,
};
const filteredFiles = filterFiles(rawFiles, filterOptions);
console.log(`[scan] Found ${rawFiles.length} files, ${filteredFiles.length} after filtering.`);
return { rawFiles, fileTree, filteredFiles };
}
async function cmdAnalyze(flags, config, filteredFiles) {
console.log(`[analyze] Analyzing ${filteredFiles.length} files...`);
const projectAnalysis = await analyzeProject(filteredFiles, config);
let symbols = null;
if (flags.symbols) {
console.log('[analyze] Extracting symbols...');
symbols = await extractSymbols(filteredFiles, config);
}
let depGraph = null;
if (flags.deps) {
console.log('[analyze] Building dependency graph...');
depGraph = await buildDependencyGraph(filteredFiles, config);
}
return { projectAnalysis, symbols, depGraph };
}
async function cmdBundle(flags, config, scanResult, analyzeResult) {
console.log(`[bundle] Creating bundle (format: ${flags.format})...`);
const bundleInput = {
fileTree: scanResult.fileTree,
filteredFiles: scanResult.filteredFiles,
projectAnalysis: analyzeResult.projectAnalysis,
symbols: analyzeResult.symbols,
depGraph: analyzeResult.depGraph,
meta: {
generatedAt: new Date().toISOString(),
rootDir: flags.dir,
version: VERSION,
format: flags.format,
},
};
const bundle = await createBundle(bundleInput, config);
let formatted;
if (flags.format === 'markdown') {
formatted = formatAsMarkdown(bundle, config);
} else {
formatted = formatAsJSON(bundle, config);
}
const outputPath = resolveOutputPath(flags);
writeOutput(formatted, outputPath);
console.log(`[bundle] Bundle written to: ${outputPath}`);
return { bundle, formatted, outputPath };
}
async function cmdValidate(flags, config, bundle) {
console.log('[validate] Validating bundle...');
const validationResult = await validateBundle(bundle, config);
let circularResult = null;
if (bundle.depGraph) {
circularResult = await checkCircularDependencies(bundle.depGraph, config);
if (circularResult.hasCircular) {
console.warn(`[validate] Warning: ${circularResult.cycles.length} circular dependency cycle(s) detected.`);
} else {
console.log('[validate] No circular dependencies found.');
}
}
let conflictResult = null;
if (bundle.symbols) {
conflictResult = await checkSymbolConflicts(bundle.symbols, config);
if (conflictResult.hasConflicts) {
console.warn(`[validate] Warning: ${conflictResult.conflicts.length} symbol conflict(s) detected.`);
} else {
console.log('[validate] No symbol conflicts found.');
}
}
if (validationResult.valid) {
console.log('[validate] Bundle is valid.');
} else {
console.warn(`[validate] Bundle validation issues: ${validationResult.errors.length} error(s).`);
validationResult.errors.forEach(err => console.warn(` - ${err}`));
}
return { validationResult, circularResult, conflictResult };
}
async function cmdInfo(flags, config) {
const targetFile = flags.positional[0] || resolveOutputPath(flags);
const resolvedTarget = path.resolve(targetFile);
if (!fs.existsSync(resolvedTarget)) {
throw new Error(`Bundle file not found: ${resolvedTarget}`);
}
console.log(`[info] Reading bundle: ${resolvedTarget}`);
const content = fs.readFileSync(resolvedTarget, 'utf8');
const tokenCount = estimateTokens(content);
const bundleSize = calculateBundleSize(content);
const savings = estimateSavings(content, config);
console.log(`\nBundle Info:`);
console.log(` File: ${resolvedTarget}`);
console.log(` Size: ${(bundleSize / 1024).toFixed(2)} KB`);
console.log(` Estimated tokens: ${tokenCount.toLocaleString()}`);
console.log(` Estimated savings: ${savings.percentage}% vs raw source (${savings.rawTokens.toLocaleString()} raw tokens)`);
console.log('');
}
async function cmdRun(flags, config) {
console.log(`[run] Starting full pipeline for: ${flags.dir}`);
const scanResult = await cmdScan(flags, config);
const analyzeResult = await cmdAnalyze(flags, config, scanResult.filteredFiles);
const bundleResult = await cmdBundle(flags, config, scanResult, analyzeResult);
if (flags.validate) {
await cmdValidate(flags, config, bundleResult.bundle);
} else {
console.log('[run] Skipping validation (--no-validate).');
}
const tokenCount = estimateTokens(bundleResult.formatted);
const savings = estimateSavings(bundleResult.formatted, config);
console.log(`\n✓ ContextPack complete!`);
console.log(` Output: ${bundleResult.outputPath}`);
console.log(` Files bundled: ${scanResult.filteredFiles.length}`);
console.log(` Estimated tokens: ${tokenCount.toLocaleString()}`);
console.log(` Token savings: ~${savings.percentage}% vs raw source`);
console.log('');
}
async function main() {
const flags = parseArgs(process.argv);
if (flags.help || (!flags.command && flags.positional.length === 0)) {
printHelp();
process.exit(0);
}
if (flags.version) {
printVersion();
process.exit(0);
}
let config;
try {
const rawConfig = loadConfig(flags.config || flags.dir);
config = mergeWithFlags(rawConfig, flags);
} catch (err) {
console.warn(`[config] Could not load config file: ${err.message}. Using defaults.`);
config = mergeWithFlags({}, flags);
}
if (flags.pro) {
try {
assertProLicense(config);
} catch (err) {
console.error(`[license] Pro feature required: ${err.message}`);
showUpgradePrompt();
process.exit(1);
}
} else {
const licenseStatus = getLicenseStatus(config);
if (licenseStatus && licenseStatus.type === 'pro') {
console.log(`[license] Pro license active.`);
}
}
const command = flags.command || 'run';
try {
switch (command) {
case 'scan': {
const result = await cmdScan(flags, config);
const outputPath = resolveOutputPath(flags);
const out = JSON.stringify({ fileTree: result.fileTree, filteredFiles: result.filteredFiles }, null, 2);
writeOutput(out, outputPath);
console.log(`[scan] Scan result written to: ${outputPath}`);
break;
}
case 'analyze': {
const scanResult = await cmdScan(flags, config);
const analyzeResult = await cmdAnalyze(flags, config, scanResult.filteredFiles);
const outputPath = resolveOutputPath(flags);
const out = JSON.stringify(analyzeResult, null, 2);
writeOutput(out, outputPath);
console.log(`[analyze] Analysis written to: ${outputPath}`);
break;
}
case 'bundle': {
const scanResult = await cmdScan(flags, config);
const analyzeResult = await cmdAnalyze(flags, config, scanResult.filteredFiles);
await cmdBundle(flags, config, scanResult, analyzeResult);
break;
}
case 'validate': {
const targetFile = flags.positional[0] || resolveOutputPath(flags);
const resolvedTarget = path.resolve(targetFile);
if (!fs.existsSync(resolvedTarget)) {
throw new Error(`Bundle file not found: ${resolvedTarget}`);
}
const content = fs.readFileSync(resolvedTarget, 'utf8');
let bundle;
try {
bundle = JSON.parse(content);
} catch {
throw new Error(`Could not parse bundle as JSON: ${resolvedTarget}`);
}
await cmdValidate(flags, config, bundle);
break;
}
case 'info': {
await cmdInfo(flags, config);
break;
}
case 'run': {
await cmdRun(flags, config);
break;
}
default:
console.error(`Unknown command: "${command}". Run "${TOOL_NAME} --help" for usage.`);
process.exit(1);
}
} catch (err) {
console.error(`\n[error] ${err.message}`);
if (process.env.DEBUG) {
console.error(err.stack);
}
process.exit(1);
}
}
main();