Skip to content

Commit 85dbbd0

Browse files
committed
Update collect-npm-stats.ts
1 parent 1854aa9 commit 85dbbd0

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

scripts/collect-npm-stats.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,45 +91,40 @@ async function updateHistoricalData(
9191
}
9292

9393
async function processPackage(packageName: string): Promise<void> {
94-
try {
95-
// Fetch current stats
96-
const stats = await fetchPackageStats(packageName);
97-
98-
// Check if package has any download statistics
99-
if (!stats.downloads || Object.keys(stats.downloads).length === 0) {
100-
throw new Error(
101-
`Package ${packageName} has no download statistics - it may not exist or have no downloads`,
102-
);
103-
}
94+
// Fetch current stats
95+
const stats = await fetchPackageStats(packageName);
96+
97+
// Check if package has any download statistics
98+
if (!stats.downloads || Object.keys(stats.downloads).length === 0) {
99+
throw new Error(
100+
`Package ${packageName} has no download statistics - it may not exist or have no downloads`,
101+
);
102+
}
104103

105-
// Use all versions without aggregation
106-
const allVersionDownloads = stats.downloads;
104+
// Use all versions without aggregation
105+
const allVersionDownloads = stats.downloads;
107106

108-
// Determine file path
109-
const dataDir = join(process.cwd(), 'data', 'npm-versions');
110-
const filePath = join(dataDir, `${packageName}.json`);
107+
// Determine file path
108+
const dataDir = join(process.cwd(), 'data', 'npm-versions');
109+
const filePath = join(dataDir, `${packageName}.json`);
111110

112-
// Ensure directory exists
113-
await mkdir(dirname(filePath), { recursive: true });
111+
// Ensure directory exists
112+
await mkdir(dirname(filePath), { recursive: true });
114113

115-
// Read existing data
116-
const existingData = await readExistingData(filePath);
114+
// Read existing data
115+
const existingData = await readExistingData(filePath);
117116

118-
// Update historical data
119-
const updatedData = await updateHistoricalData(packageName, allVersionDownloads, existingData);
117+
// Update historical data
118+
const updatedData = await updateHistoricalData(packageName, allVersionDownloads, existingData);
120119

121-
// Write back to file
122-
await writeFile(filePath, JSON.stringify(updatedData, null, 2));
120+
// Write back to file
121+
await writeFile(filePath, JSON.stringify(updatedData, null, 2));
123122

124-
console.log(`✅ Updated stats for ${packageName}`);
125-
console.log(` Versions: ${Object.keys(allVersionDownloads).join(', ')}`);
126-
console.log(
127-
` Total downloads: ${Object.values(allVersionDownloads).reduce((a, b) => a + b, 0)}`,
128-
);
129-
} catch (error) {
130-
console.error(`❌ Failed to process ${packageName}:`, error);
131-
throw error;
132-
}
123+
console.log(`✅ Updated stats for ${packageName}`);
124+
console.log(` Versions: ${Object.keys(allVersionDownloads).join(', ')}`);
125+
console.log(
126+
` Total downloads: ${Object.values(allVersionDownloads).reduce((a, b) => a + b, 0)}`,
127+
);
133128
}
134129

135130
async function main() {

0 commit comments

Comments
 (0)