-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-downloader.js
More file actions
267 lines (240 loc) · 7.78 KB
/
module-downloader.js
File metadata and controls
267 lines (240 loc) · 7.78 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
const fs = require("fs");
const path = require("path");
const https = require("https");
// Try F: drive first, fallback to local directory if not accessible
let DOWNLOAD_DIR = "F:/npm_packages";
const FALLBACK_DIR = path.join(__dirname, "npm_packages");
const BASE_URL = "https://registry.npmjs.org";
const MAX_VERSIONS = 200;
// Function to validate and set up download directory
function setupDownloadDirectory() {
// First try the F: drive
try {
if (!fs.existsSync(DOWNLOAD_DIR)) {
fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
}
// Test if we can write to the directory
const testFile = path.join(DOWNLOAD_DIR, ".test-write");
fs.writeFileSync(testFile, "test");
fs.unlinkSync(testFile);
console.log(`Using download directory: ${DOWNLOAD_DIR}`);
return DOWNLOAD_DIR;
} catch (error) {
console.warn(`F: drive not accessible: ${error.message}`);
console.log(`Falling back to local directory: ${FALLBACK_DIR}`);
// Fallback to local directory
try {
if (!fs.existsSync(FALLBACK_DIR)) {
fs.mkdirSync(FALLBACK_DIR, { recursive: true });
}
// Test if we can write to the fallback directory
const testFile = path.join(FALLBACK_DIR, ".test-write");
fs.writeFileSync(testFile, "test");
fs.unlinkSync(testFile);
DOWNLOAD_DIR = FALLBACK_DIR;
console.log(`Using fallback download directory: ${DOWNLOAD_DIR}`);
return DOWNLOAD_DIR;
} catch (fallbackError) {
console.error(
`Cannot create or write to fallback directory: ${fallbackError.message}`
);
process.exit(1);
}
}
}
// Function to safely create directory with retry logic
function safeMkdirSync(dirPath, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
return true;
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
console.warn(
`Attempt ${attempt} failed to create directory ${dirPath}: ${error.message}`
);
// Wait a bit before retrying
const waitTime = Math.min(1000 * attempt, 5000); // Exponential backoff, max 5 seconds
console.log(`Retrying in ${waitTime}ms...`);
require("util").promisify(setTimeout)(waitTime);
}
}
}
function readPackagesFromFile() {
try {
const content = fs.readFileSync(
path.join(__dirname, "output-no-dup.txt"),
"utf-8"
);
return content
.split("\n")
.map((line) => line.trim())
.filter(
(line) => line && !line.startsWith("#") && !line.startsWith("//")
);
} catch (error) {
console.error("Error reading output-no-dup.txt:", error.message);
process.exit(1);
}
}
async function processPackages() {
const packages = readPackagesFromFile();
console.log(`Found ${packages.length} packages to process`);
// Set up download directory
setupDownloadDirectory();
for (const packageName of packages) {
try {
console.log(`\nStarting processing for ${packageName}`);
await processPackage(packageName);
console.log(`Finished processing ${packageName}`);
} catch (error) {
console.error(`Error processing ${packageName}:`, error.message);
// Continue with next package instead of stopping
}
}
console.log("\nAll packages processed!");
}
async function processPackage(packageName) {
const packageDir = path.join(DOWNLOAD_DIR, ...packageName.split("/"));
const packageJsonPath = path.join(packageDir, "package.json");
// Skip if package is already fully processed
if (fs.existsSync(packageJsonPath)) {
console.log(`Skipping ${packageName} (already processed)`);
return;
}
// Create package directory if it doesn't exist with retry logic
try {
safeMkdirSync(packageDir);
} catch (error) {
throw new Error(
`Failed to create directory for ${packageName}: ${error.message}`
);
}
// Fetch package metadata from npm registry
const metadata = await fetchPackageMetadata(packageName);
if (!metadata || !metadata.versions) {
console.error(`No versions found for ${packageName}`);
return;
}
// Sort versions by publish date (newest first) and limit to MAX_VERSIONS
const sortedVersions = Object.keys(metadata.versions)
.map((version) => ({
version,
time: new Date(metadata.time[version] || metadata.time.created || 0),
}))
.sort((a, b) => b.time - a.time)
.map((item) => item.version)
.slice(0, MAX_VERSIONS);
// Prepare package.json structure
const packageJson = {
name: packageName,
versions: {},
time: {},
"dist-tags": metadata["dist-tags"] || {},
_id: metadata._id || packageName,
readme: metadata.readme || "",
readmeFilename: metadata.readmeFilename || "README.md",
_attachments: {},
totalVersions: Object.keys(metadata.versions).length,
downloadedVersions: sortedVersions.length,
};
console.log(`Processing ${sortedVersions.length} versions of ${packageName}`);
// Process each version
for (const version of sortedVersions) {
const versionData = metadata.versions[version];
// Add version metadata to package.json
packageJson.versions[version] = {
...versionData,
_id: versionData._id || `${packageName}@${version}`,
dist: versionData.dist || {},
};
// Add publish timestamp
if (metadata.time[version]) {
packageJson.time[version] = metadata.time[version];
}
// Download tarball if it doesn't exist
if (versionData.dist && versionData.dist.tarball) {
const fileName = `${packageName.replace(/\//g, "-")}-${version}.tgz`;
const filePath = path.join(packageDir, fileName);
if (!fs.existsSync(filePath)) {
try {
await downloadFile(versionData.dist.tarball, filePath);
packageJson._attachments[fileName] = {
shasum: versionData.dist.shasum,
version: version,
};
} catch (downloadError) {
console.warn(
`Failed to download ${fileName}: ${downloadError.message}`
);
// Continue processing other versions
}
}
}
}
// Write the complete package.json
try {
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Generated package.json for ${packageName}`);
} catch (writeError) {
throw new Error(
`Failed to write package.json for ${packageName}: ${writeError.message}`
);
}
}
function fetchPackageMetadata(packageName) {
return new Promise((resolve, reject) => {
const url = `${BASE_URL}/${packageName.replace(/\//g, "%2F")}`;
https
.get(url, (res) => {
if (res.statusCode === 404) {
resolve(null);
return;
}
if (res.statusCode !== 200) {
reject(
new Error(
`Failed to fetch metadata for ${packageName}: ${res.statusCode}`
)
);
return;
}
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
});
})
.on("error", reject);
});
}
function downloadFile(url, filePath) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
https
.get(url, (response) => {
if (response.statusCode !== 200) {
reject(
new Error(`Failed to download ${url}: ${response.statusCode}`)
);
return;
}
response.pipe(file);
file.on("finish", () => {
file.close(resolve);
});
})
.on("error", (err) => {
fs.unlink(filePath, () => reject(err));
});
});
}
processPackages().catch(console.error);