Skip to content

Commit 614deed

Browse files
chore: remove any types and improve error handling in scan.ts
1 parent 4ef3c05 commit 614deed

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/scan.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { XMLParser } from 'fast-xml-parser';
44
import { techMap } from './techMap';
55
import simpleIconsHex from './simple-icons-hex.json';
66
import { generateMarkdown, copyAssets } from './output';
7+
import { StackItem } from './types';
78

89
const BASE_DIR = path.join(process.cwd(), 'public', 'stackscan');
910

@@ -27,8 +28,8 @@ function getPackageJson(projectPath: string) {
2728
try {
2829
console.log(`Renaming ${pkgPath} to ${pkgPathUnderscore}`);
2930
fs.renameSync(pkgPath, pkgPathUnderscore);
30-
} catch (e: any) {
31-
console.warn(`Failed to rename package.json to _package.json: ${e.message}`);
31+
} catch (e: unknown) {
32+
if (e instanceof Error) console.warn(`Failed to rename package.json to _package.json: ${e.message}`);
3233
}
3334
}
3435

@@ -37,8 +38,8 @@ function getPackageJson(projectPath: string) {
3738
try {
3839
const content = fs.readFileSync(pkgPathUnderscore, 'utf-8');
3940
return JSON.parse(content);
40-
} catch (e: any) {
41-
console.warn(`Failed to read _package.json: ${e.message}`);
41+
} catch (e: unknown) {
42+
if (e instanceof Error) console.warn(`Failed to read _package.json: ${e.message}`);
4243
}
4344
}
4445

@@ -47,8 +48,8 @@ function getPackageJson(projectPath: string) {
4748
try {
4849
const content = fs.readFileSync(pkgPath, 'utf-8');
4950
return JSON.parse(content);
50-
} catch (e: any) {
51-
console.warn(`Failed to read package.json: ${e.message}`);
51+
} catch (e: unknown) {
52+
if (e instanceof Error) console.warn(`Failed to read package.json: ${e.message}`);
5253
}
5354
}
5455

@@ -68,8 +69,8 @@ function getPomXml(projectPath: string) {
6869
try {
6970
console.log(`Renaming ${pomPath} to ${pomPathUnderscore}`);
7071
fs.renameSync(pomPath, pomPathUnderscore);
71-
} catch (e: any) {
72-
console.warn(`Failed to rename pom.xml to _pom.xml: ${e.message}`);
72+
} catch (e: unknown) {
73+
if (e instanceof Error) console.warn(`Failed to rename pom.xml to _pom.xml: ${e.message}`);
7374
}
7475
}
7576

@@ -78,23 +79,23 @@ function getPomXml(projectPath: string) {
7879
if (fs.existsSync(pomPathUnderscore)) {
7980
try {
8081
xmlContent = fs.readFileSync(pomPathUnderscore, 'utf-8');
81-
} catch (e: any) {
82-
console.warn(`Failed to read _pom.xml: ${e.message}`);
82+
} catch (e: unknown) {
83+
if (e instanceof Error) console.warn(`Failed to read _pom.xml: ${e.message}`);
8384
}
8485
} else if (fs.existsSync(pomPath)) {
8586
try {
8687
xmlContent = fs.readFileSync(pomPath, 'utf-8');
87-
} catch (e: any) {
88-
console.warn(`Failed to read pom.xml: ${e.message}`);
88+
} catch (e: unknown) {
89+
if (e instanceof Error) console.warn(`Failed to read pom.xml: ${e.message}`);
8990
}
9091
}
9192

9293
if (xmlContent) {
9394
try {
9495
const parser = new XMLParser();
9596
return parser.parse(xmlContent);
96-
} catch (e: any) {
97-
console.warn(`Failed to parse XML: ${e.message}`);
97+
} catch (e: unknown) {
98+
if (e instanceof Error) console.warn(`Failed to parse XML: ${e.message}`);
9899
}
99100
}
100101

@@ -158,7 +159,7 @@ interface SyncOptions {
158159
out?: string;
159160
}
160161

161-
async function analyzeProject(projectPath: string, options: SyncOptions): Promise<any[]> {
162+
async function analyzeProject(projectPath: string, options: SyncOptions): Promise<StackItem[]> {
162163
const pkg = getPackageJson(projectPath);
163164
const pom = getPomXml(projectPath);
164165

@@ -167,7 +168,7 @@ async function analyzeProject(projectPath: string, options: SyncOptions): Promis
167168
}
168169

169170
// 1. Detect Tech
170-
const allDeps: Record<string, any> = {};
171+
const allDeps: Record<string, string> = {};
171172

172173
// Process package.json
173174
if (pkg) {
@@ -181,14 +182,14 @@ async function analyzeProject(projectPath: string, options: SyncOptions): Promis
181182
deps = [deps];
182183
}
183184

184-
deps.forEach((d: any) => {
185+
deps.forEach((d: { groupId?: string; artifactId?: string }) => {
185186
// Map groupId:artifactId and just artifactId
186187
if (d.artifactId) allDeps[d.artifactId] = "latest";
187188
if (d.groupId && d.artifactId) allDeps[`${d.groupId}:${d.artifactId}`] = "latest";
188189
});
189190
}
190191

191-
const detectedTechs: any[] = [];
192+
const detectedTechs: StackItem[] = [];
192193

193194
Object.keys(allDeps).forEach(dep => {
194195
if (SKIPPED_TECHS.includes(dep)) return;
@@ -207,9 +208,10 @@ async function analyzeProject(projectPath: string, options: SyncOptions): Promis
207208
const nameSlug = tech.name.toLowerCase();
208209
const nameSlugNoSpaces = tech.name.toLowerCase().replace(/\s+/g, '');
209210

210-
const hex = (simpleIconsHex as any)[depSlug] ||
211-
(simpleIconsHex as any)[nameSlug] ||
212-
(simpleIconsHex as any)[nameSlugNoSpaces];
211+
const icons = simpleIconsHex as Record<string, string>;
212+
const hex = icons[depSlug] ||
213+
icons[nameSlug] ||
214+
icons[nameSlugNoSpaces];
213215

214216
if (hex) color = `#${hex}`;
215217
}
@@ -302,8 +304,8 @@ async function scan(targetPath?: string | object, optionsOrUndefined?: SyncOptio
302304
console.log(`✅ Generated stack.json at: ${outPath}`);
303305
}
304306

305-
} catch (err: any) {
306-
console.error(`❌ Error scanning project:`, err.message);
307+
} catch (err: unknown) {
308+
if (err instanceof Error) console.error(`❌ Error scanning project:`, err.message);
307309
process.exit(1);
308310
}
309311
return;
@@ -327,7 +329,7 @@ async function scan(targetPath?: string | object, optionsOrUndefined?: SyncOptio
327329

328330
console.log(`Found ${projectDirs.length} projects to process.\n`);
329331

330-
const allProjects: { name: string; techs: any[] }[] = [];
332+
const allProjects: { name: string; techs: StackItem[] }[] = [];
331333

332334
for (const dir of projectDirs) {
333335
const projectPath = path.join(BASE_DIR, dir.name);
@@ -358,8 +360,8 @@ async function scan(targetPath?: string | object, optionsOrUndefined?: SyncOptio
358360

359361
console.log(`✅ ${dir.name.padEnd(20)} -> stack.json (${techsWithUrls.length} techs)`);
360362

361-
} catch (err: any) {
362-
console.error(`❌ Error processing ${dir.name}:`, err.message);
363+
} catch (err: unknown) {
364+
if (err instanceof Error) console.error(`❌ Error processing ${dir.name}:`, err.message);
363365
}
364366
} else {
365367
console.warn(`⚠️ Skipping "${dir.name}": No package.json or pom.xml found.`);
@@ -376,7 +378,7 @@ async function scan(targetPath?: string | object, optionsOrUndefined?: SyncOptio
376378
console.log('\n✨ Sync complete.');
377379
}
378380

379-
function updateRootReadme(projects: { name: string; techs: any[] }[]) {
381+
function updateRootReadme(projects: { name: string; techs: StackItem[] }[]) {
380382
const readmePath = path.join(process.cwd(), 'README.md');
381383
if (!fs.existsSync(readmePath)) {
382384
console.log('⚠️ No root README.md found to update.');

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ export interface DetectorResult {
22
name: string;
33
logo: string;
44
type: string;
5-
color?: string;
5+
color?: string | null;
6+
}
7+
8+
export interface StackItem extends DetectorResult {
9+
slug: string;
10+
relativePath?: string;
611
}
712

813
export interface TechDefinition {

0 commit comments

Comments
 (0)