Skip to content

Commit d3aab18

Browse files
authored
feat: add default pattern for js / ts project (#166)
1 parent 7a8dede commit d3aab18

1 file changed

Lines changed: 59 additions & 74 deletions

File tree

ts-parser/src/utils/typescript-structure.ts

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -103,43 +103,51 @@ export class TypeScriptStructureAnalyzer {
103103

104104

105105
private analyzePackages(module: ModuleInfo, options: { noDist?: boolean, srcPatterns?: string[] } = {}): PackageInfo[] {
106-
const packages: PackageInfo[] = [];
107-
const sourceDirs = this.findSourceDirectories(module, options);
106+
const sourceFiles = this.findSourceFiles(module, options);
107+
return sourceFiles.map(file => this.createPackageFromFile(module, file));
108+
}
108109

109-
for (const sourceDir of sourceDirs) {
110-
const packageInfos = this.createPackagesFromDirectory(module, sourceDir, options);
111-
packages.push(...packageInfos);
110+
private findSourceFiles(module: ModuleInfo, options: { noDist?: boolean, srcPatterns?: string[] } = {}): string[] {
111+
// Handle default srcPatterns if not provided
112+
if (!options.srcPatterns || options.srcPatterns.length === 0) {
113+
options.srcPatterns = ['**/*.ts', '**/*.js'];
112114
}
113115

114-
return packages;
115-
}
116+
const allFiles = new Set<string>();
116117

117-
private findSourceDirectories(module: ModuleInfo, options: { noDist?: boolean, srcPatterns?: string[] } = {}): string[] {
118-
// Handle srcPatterns if provided
118+
// 1. Handle srcPatterns if provided
119119
if (options.srcPatterns && options.srcPatterns.length > 0) {
120-
return this.findDirectoriesByPatterns(module.path, options.srcPatterns, options);
120+
// For now, if patterns are provided, we search the entire module for matching files
121+
// In a more complex implementation, we might want to support actual glob matching
122+
// Here we reuse findTypeScriptFiles which already finds .ts/.js files
123+
const files = this.findTypeScriptFiles(module.path, options);
124+
files.forEach(f => allFiles.add(f));
125+
return Array.from(allFiles);
121126
}
122127

123-
// Original behavior when no srcPatterns provided
124-
const dirs: string[] = [];
125-
128+
// 2. Original behavior fallback
126129
// Get tsconfig.json configuration
127130
const config = this.tsConfigCache.getTsConfig(module.path);
128131

129-
// Default: all directories in tsconfig.json
132+
// Default: all files in tsconfig.json
130133
if(config.fileNames && config.fileNames.length > 0) {
131-
const dirSet = new Set<string>();
132-
config.fileNames.forEach(file => { dirSet.add(path.dirname(file)); });
133-
dirs.push(...Array.from(dirSet));
134-
return dirs.filter(dir => fs.existsSync(dir));
134+
config.fileNames.forEach(file => {
135+
if (fs.existsSync(file)) {
136+
allFiles.add(file);
137+
}
138+
});
139+
if (allFiles.size > 0) {
140+
return Array.from(allFiles);
141+
}
135142
}
136143

137144
// Fallback to rootDir and outDir
145+
const searchDirs: string[] = [];
138146
if (config.rootDir) {
139-
dirs.push(path.join(module.path, config.rootDir));
147+
searchDirs.push(path.join(module.path, config.rootDir));
140148
}
141149
if (config.outDir && !(options.noDist && config.outDir === 'dist')) {
142-
dirs.push(path.join(module.path, config.outDir));
150+
searchDirs.push(path.join(module.path, config.outDir));
143151
}
144152

145153
// Default source directories
@@ -151,67 +159,44 @@ export class TypeScriptStructureAnalyzer {
151159
for (const dir of defaultDirs) {
152160
const dirPath = path.join(module.path, dir);
153161
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
154-
dirs.push(dirPath);
162+
searchDirs.push(dirPath);
155163
}
156164
}
157165

158-
// Remove duplicates and ensure paths exist
159-
return [...new Set(dirs)].filter(dir => fs.existsSync(dir));
160-
}
161-
162-
private findDirectoriesByPatterns(modulePath: string, patterns: string[], options: { noDist?: boolean }): string[] {
163-
const matchedDirs = new Set<string>();
164-
165-
for (const pattern of patterns) {
166-
// Assuming patterns are relative to modulePath
167-
const fullPath = path.join(modulePath, pattern);
168-
169-
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
170-
// Check if the directory matches the noDist option
171-
if (options.noDist && path.basename(fullPath) === 'dist') {
172-
continue;
173-
}
174-
175-
matchedDirs.add(fullPath);
166+
// Find all files in the collected directories
167+
for (const dir of [...new Set(searchDirs)]) {
168+
if (fs.existsSync(dir)) {
169+
const files = this.findTypeScriptFiles(dir, options);
170+
files.forEach(f => allFiles.add(f));
176171
}
177172
}
178-
179-
return Array.from(matchedDirs);
180-
}
181173

182-
private createPackagesFromDirectory(module: ModuleInfo, sourceDir: string, options: { noDist?: boolean } = {}): PackageInfo[] {
183-
const packages: PackageInfo[] = [];
184-
185-
// Find all TypeScript files
186-
const tsFiles = this.findTypeScriptFiles(sourceDir, options);
187-
188-
// Each file becomes a separate package
189-
for (const file of tsFiles) {
190-
// Calculate file path relative to the package root (module path)
191-
const relativeFilePath = path.relative(module.path, file);
192-
const pkgPath = relativeFilePath.replace(/\\/g, '/');
193-
194-
// Check if this is a main file (index or main)
195-
const baseName = path.basename(file, path.extname(file));
196-
const isMain = baseName === 'index' || baseName === 'main';
197-
198-
// Check if this is a test file
199-
const isTest = relativeFilePath.includes('test') ||
200-
relativeFilePath.includes('__tests__') ||
201-
file.includes('.test.') ||
202-
file.includes('.spec.');
203-
204-
packages.push({
205-
pkgPath,
206-
moduleName: module.name,
207-
isMain,
208-
isTest,
209-
files: [file], // Each package contains only one file
210-
imports: [] // Will be populated during parsing
211-
});
212-
}
174+
return Array.from(allFiles);
175+
}
213176

214-
return packages;
177+
private createPackageFromFile(module: ModuleInfo, file: string): PackageInfo {
178+
// Calculate file path relative to the package root (module path)
179+
const relativeFilePath = path.relative(module.path, file);
180+
const pkgPath = relativeFilePath.replace(/\\/g, '/');
181+
182+
// Check if this is a main file (index or main)
183+
const baseName = path.basename(file, path.extname(file));
184+
const isMain = baseName === 'index' || baseName === 'main';
185+
186+
// Check if this is a test file
187+
const isTest = relativeFilePath.includes('test') ||
188+
relativeFilePath.includes('__tests__') ||
189+
file.includes('.test.') ||
190+
file.includes('.spec.');
191+
192+
return {
193+
pkgPath,
194+
moduleName: module.name,
195+
isMain,
196+
isTest,
197+
files: [file], // Each package contains only one file
198+
imports: [] // Will be populated during parsing
199+
};
215200
}
216201

217202
private findTypeScriptFiles(dir: string, options: { noDist?: boolean, srcPatterns?: string[] } = {}): string[] {

0 commit comments

Comments
 (0)