-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpackage-processor.ts
More file actions
571 lines (507 loc) · 15.4 KB
/
package-processor.ts
File metadata and controls
571 lines (507 loc) · 15.4 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import { Project, ts } from 'ts-morph';
import * as path from 'path';
import * as fs from 'fs';
import { Repository, Module } from '../types/uniast';
import { ModuleParser } from '../parser/ModuleParser';
import { MonorepoPackage } from './monorepo';
import { GraphBuilder } from './graph-builder';
import { TsConfigCache } from './tsconfig-cache';
export interface PackageProcessingOptions {
loadExternalSymbols?: boolean;
noDist?: boolean;
srcPatterns?: string[];
monorepoMode?: 'combined' | 'separate';
}
export interface PackageProcessingResult {
success: boolean;
module?: Module;
repository?: Repository;
outputPath?: string;
error?: Error;
packageInfo: {
name: string;
path: string;
fileCount: number;
size: number; // bytes
};
}
/**
* Package Processor - Encapsulates the complete processing workflow for a single package
*/
export class PackageProcessor {
private projectRoot: string;
constructor(projectRoot: string) {
this.projectRoot = projectRoot;
}
/**
* Process a single package
*/
async processPackage(
pkg: MonorepoPackage,
options: PackageProcessingOptions = {}
): Promise<PackageProcessingResult> {
try {
// 1. Analyze package information
const packageInfo = await this.analyzePackage(pkg);
// 2. Create project instance
const project = ProjectFactory.createProjectForPackage(pkg.absolutePath, pkg.name);
// 3. Parse module
const module = await this.parseModule(project, pkg, options);
// 4. Create independent repository (for separate mode)
const repository = this.createPackageRepository(pkg, module);
// 5. Build graph relationships
this.buildPackageGraph(repository);
// 6. Generate output file (only in separate mode)
let outputPath: string | undefined;
if (options.monorepoMode !== 'combined') {
outputPath = await this.generateOutput(pkg, repository);
}
return {
success: true,
module,
repository,
outputPath,
packageInfo,
};
} catch (error) {
return {
success: false,
error: error as Error,
packageInfo: {
name: pkg.name || path.basename(pkg.absolutePath),
path: pkg.path,
fileCount: 0,
size: 0,
},
};
}
}
/**
* Analyze package information
*/
private async analyzePackage(
pkg: MonorepoPackage
): Promise<PackageProcessingResult['packageInfo']> {
const packageInfo = {
name: pkg.name || path.basename(pkg.absolutePath),
path: pkg.path,
fileCount: 0,
size: 0,
};
try {
// Recursively collect file information
const stats = await this.getDirectoryStats(pkg.absolutePath);
packageInfo.fileCount = stats.fileCount;
packageInfo.size = stats.totalSize;
} catch (error) {
console.warn(`Failed to analyze package ${packageInfo.name}:`, error);
}
return packageInfo;
}
/**
* Get directory statistics
*/
private async getDirectoryStats(
dirPath: string
): Promise<{ fileCount: number; totalSize: number }> {
let fileCount = 0;
let totalSize = 0;
const processDirectory = async (currentPath: string): Promise<void> => {
try {
const entries = await fs.promises.readdir(currentPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
// Skip commonly ignored directories
if (entry.isDirectory()) {
if (!PackageProcessor.shouldIgnoreDirectory(entry.name, this.projectRoot)) {
await processDirectory(fullPath);
}
} else if (entry.isFile()) {
// Only count relevant file types
if (PackageProcessor.isRelevantFile(entry.name)) {
const stats = await fs.promises.stat(fullPath);
fileCount++;
totalSize += stats.size;
}
}
}
} catch (error) {
// Ignore permission errors etc.
console.debug(`Cannot access directory ${currentPath}:`, error);
}
};
await processDirectory(dirPath);
return { fileCount, totalSize };
}
/**
* Parse module
*/
private async parseModule(
project: Project,
pkg: MonorepoPackage,
options: PackageProcessingOptions
): Promise<Module> {
const moduleParser = new ModuleParser(project, this.projectRoot);
return await moduleParser.parseModule(pkg.absolutePath, pkg.path, options);
}
/**
* Create package repository
*/
private createPackageRepository(pkg: MonorepoPackage, module: Module): Repository {
return RepositoryFactory.createPackageRepository(pkg, module);
}
/**
* Build package graph relationships
*/
private buildPackageGraph(repository: Repository): void {
GraphBuilder.buildGraph(repository);
}
/**
* Generate output file
*/
private async generateOutput(pkg: MonorepoPackage, repository: Repository): Promise<string> {
const sanitizedPackageName = (pkg.name || path.basename(pkg.absolutePath)).replace(
/[/\\:*?"<>|@]/g,
'_'
);
// Create output directory
const outputDir = path.join(process.cwd(), 'output');
if (!fs.existsSync(outputDir)) {
await fs.promises.mkdir(outputDir, { recursive: true });
}
// Generate output file
const outputPath = path.join(outputDir, `${sanitizedPackageName}.json`);
const jsonOutput = JSON.stringify(repository, null, 2);
await fs.promises.writeFile(outputPath, jsonOutput, 'utf8');
console.log(`Package ${pkg.name || pkg.path} written to: ${outputPath}`);
return outputPath;
}
/**
* Check if directory should be ignored
*/
private static shouldIgnoreDirectory(dirName: string, projectRoot?: string): boolean {
const ignoreDirs = [
'node_modules',
'.git',
'.svn',
'dist',
'build',
'coverage',
'.nyc_output',
'tmp',
'temp',
'.cache',
'.next',
'.nuxt',
'out',
'public',
'static',
'__pycache__',
'.pytest_cache',
];
// Check basic ignore rules
if (ignoreDirs.includes(dirName) || dirName.startsWith('.')) {
return true;
}
// Check .gitignore file
if (projectRoot) {
return this.isIgnoredByGitignore(dirName, projectRoot);
}
return false;
}
/**
* Check if directory is ignored by .gitignore
*/
private static isIgnoredByGitignore(dirName: string, projectRoot: string): boolean {
try {
const gitignorePath = path.join(projectRoot, '.gitignore');
if (!fs.existsSync(gitignorePath)) {
return false;
}
const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8');
const ignorePatterns = gitignoreContent
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#')); // Filter empty lines and comments
for (const pattern of ignorePatterns) {
// Simple pattern matching: supports * wildcards and directory matching
if (this.matchGitignorePattern(dirName, pattern)) {
return true;
}
}
} catch (error) {
// Ignore errors reading .gitignore file
console.debug(`Cannot read .gitignore file: ${error}`);
}
return false;
}
/**
* Match gitignore pattern
*/
private static matchGitignorePattern(dirName: string, pattern: string): boolean {
// Remove trailing /
const cleanPattern = pattern.replace(/\/$/, '');
// Exact match
if (cleanPattern === dirName) {
return true;
}
// Wildcard match
if (cleanPattern.includes('*')) {
const regexPattern = cleanPattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(dirName);
}
return false;
}
/**
* Check if file is relevant
*/
private static isRelevantFile(fileName: string): boolean {
const relevantExtensions = [
'.ts',
'.tsx',
'.js',
'.jsx',
'.vue',
'.svelte',
'.astro',
'.json',
'.yaml',
'.yml',
'.md',
'.mdx',
'.css',
'.scss',
'.sass',
'.less',
'.html',
'.htm',
];
const ext = path.extname(fileName).toLowerCase();
return relevantExtensions.includes(ext);
}
}
/**
* Project Factory - Centralized Project creation logic
* Handles TypeScript project creation with various configurations
*/
export class ProjectFactory {
/**
* Create a project with default compiler options
* Used when no tsconfig.json is available or as fallback
*/
static createDefaultProject(): Project {
return new Project({
compilerOptions: {
target: 99, // ESNext
module: 1, // CommonJS
allowJs: true,
checkJs: false,
skipLibCheck: true,
skipDefaultLibCheck: true,
strict: false,
noImplicitAny: false,
strictNullChecks: false,
strictFunctionTypes: false,
strictBindCallApply: false,
strictPropertyInitialization: false,
noImplicitReturns: false,
noFallthroughCasesInSwitch: false,
noUncheckedIndexedAccess: false,
noImplicitOverride: false,
noPropertyAccessFromIndexSignature: false,
allowUnusedLabels: false,
allowUnreachableCode: false,
exactOptionalPropertyTypes: false,
noImplicitThis: false,
alwaysStrict: false,
noImplicitUseStrict: false,
forceConsistentCasingInFileNames: true,
},
});
}
/**
* Create a project for a single repository
* Handles tsconfig.json resolution and project references
*/
static createProjectForSingleRepo(
projectRoot: string,
tsConfigPath?: string,
tsConfigCache?: TsConfigCache
): Project {
let configPath = path.join(projectRoot, 'tsconfig.json');
if (tsConfigPath) {
let absoluteTsConfigPath = tsConfigPath;
if (!path.isAbsolute(absoluteTsConfigPath)) {
absoluteTsConfigPath = path.join(projectRoot, absoluteTsConfigPath);
}
configPath = absoluteTsConfigPath;
if (tsConfigCache) {
tsConfigCache.setGlobalConfigPath(absoluteTsConfigPath);
}
}
if (fs.existsSync(configPath)) {
const project = new Project({
tsConfigFilePath: configPath,
compilerOptions: {
allowJs: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
},
});
// Handle project references
ProjectFactory.processProjectReferences(project, configPath);
return project;
} else {
return ProjectFactory.createDefaultProject();
}
}
/**
* Create a project for a package (monorepo scenario)
* Simpler version that only checks for local tsconfig.json
*/
static createProjectForPackage(
packagePath: string,
packageName?: string
): Project {
const tsConfigPath = path.join(packagePath, 'tsconfig.json');
if (fs.existsSync(tsConfigPath)) {
console.log(
`Creating project for package ${packageName || path.basename(packagePath)} with tsconfig ${tsConfigPath}`
);
try {
return new Project({
tsConfigFilePath: tsConfigPath,
compilerOptions: {
allowJs: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
},
});
} catch (error) {
console.warn(
`Failed to create project with tsconfig for package ${packageName || path.basename(packagePath)}:`,
error
);
return ProjectFactory.createDefaultProject();
}
} else {
console.log(
`No tsconfig.json found for package ${packageName || path.basename(packagePath)}, using default config`
);
return ProjectFactory.createDefaultProject();
}
}
/**
* Process TypeScript project references recursively
* Handles composite projects and project references
*/
private static processProjectReferences(project: Project, configPath: string): void {
const tsConfigQueue: string[] = [configPath];
const processedTsConfigs = new Set<string>();
while (tsConfigQueue.length > 0) {
const currentTsConfig = path.resolve(tsConfigQueue.shift()!);
if (processedTsConfigs.has(currentTsConfig)) {
continue;
}
processedTsConfigs.add(currentTsConfig);
const tsConfig_ = ts.readConfigFile(currentTsConfig, ts.sys.readFile);
if (tsConfig_.error) {
console.warn('parse tsconfig error', tsConfig_.error);
continue;
}
const parsedConfig = ts.parseJsonConfigFileContent(
tsConfig_.config,
ts.sys,
path.dirname(currentTsConfig)
);
if (parsedConfig.errors.length > 0) {
parsedConfig.errors.forEach(err => {
console.warn('parse tsconfig warning:', err.messageText);
});
}
// Filter out non-existent files and ensure their directories exist
const existingFiles = parsedConfig.fileNames.filter(fileName => {
try {
// Check if file exists
if (!fs.existsSync(fileName)) {
return false;
}
// Check if parent directory exists
const parentDir = path.dirname(fileName);
return fs.existsSync(parentDir);
} catch (error) {
// If any error occurs during checking, exclude the file
return false;
}
});
if (existingFiles.length > 0) {
try {
project.addSourceFilesAtPaths(existingFiles);
} catch (error) {
console.warn('Failed to add source files:', error);
}
}
const references = parsedConfig.projectReferences;
if (!references) {
continue;
}
for (const ref of references) {
const resolvedRef = ts.resolveProjectReferencePath(ref);
if (resolvedRef.length > 0) {
const refPath = path.resolve(path.dirname(currentTsConfig), resolvedRef);
if (fs.existsSync(refPath)) {
tsConfigQueue.push(refPath);
}
}
}
}
}
}
/**
* Repository Factory - Centralized creation of Repository objects
*/
export class RepositoryFactory {
private static AST_VERSION = process.env.ABCODER_AST_VERSION || 'unknown';
private static TOOL_VERSION = process.env.ABCODER_TOOL_VERSION || 'unknown';
/**
* Create a repository for a single project/repository
*/
static createRepository(repoPath: string): Repository {
const absolutePath = path.resolve(repoPath);
return {
ASTVersion: RepositoryFactory.AST_VERSION,
ToolVersion: RepositoryFactory.TOOL_VERSION,
id: path.basename(absolutePath),
Path: absolutePath,
Modules: {},
Graph: {},
};
}
/**
* Create a repository for a monorepo package
*/
static createPackageRepository(pkg: MonorepoPackage, module: Module): Repository {
return {
ASTVersion: RepositoryFactory.AST_VERSION,
ToolVersion: RepositoryFactory.TOOL_VERSION,
Path: pkg.absolutePath,
id: pkg.name || path.basename(pkg.absolutePath),
Modules: { [module.Name]: module },
Graph: {},
};
}
/**
* Create an empty repository with custom id
*/
static createEmptyRepository(id: string): Repository {
return {
ASTVersion: RepositoryFactory.AST_VERSION,
ToolVersion: RepositoryFactory.TOOL_VERSION,
Path: '',
id,
Modules: {},
Graph: {},
};
}
}