-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (72 loc) · 2.83 KB
/
index.js
File metadata and controls
90 lines (72 loc) · 2.83 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
const { readFileSync } = require('fs');
const { exec } = require('child_process');
const { promisify } = require('util');
const execP = promisify(exec);
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const parseArguments = () => {
const args = process.argv.reduce((acc, arg) => {
const [key, value] = arg.split('=');
if (key && value && ['--sleep', '--start'].includes(key)) {
acc[key] = parseInt(value);
}
return acc;
}, {});
return {
sleep: (args['--sleep'] || 0) * 1000,
start: args['--start'] || 0
};
};
const installPackage = async (name, version, sleepMs) => {
await sleep(sleepMs);
try {
const packageSpec = `${name}@${version}`;
await execP(`npm install ${packageSpec} --no-save --no-package-lock`);
await execP(`npm remove ${name} --no-save --no-package-lock`);
return { success: true, package: packageSpec };
} catch (error) {
return { success: false, package: `${name}@${version}`, error: error.message };
}
};
const populatePackages = async () => {
const { sleep: sleepMs, start } = parseArguments();
console.log('Verdaccio Cache Populator');
console.log('─────────────────────────────');
console.log(`Starting at: ${start}`);
console.log(`Sleep: ${sleepMs}ms between installs\n`);
try {
// Read the package list
const data = readFileSync('./top-npm-packages.json', 'utf8');
const allPackages = JSON.parse(data);
// Process from start to end
const packages = allPackages.slice(start);
console.log(`Total package versions in file: ${allPackages.length}`);
console.log(`Processing ${packages.length} package versions (from index ${start})\n`);
let successCount = 0;
let errorCount = 0;
for (let i = 0; i < packages.length; i++) {
const { name, version } = packages[i];
const progress = `[${i + 1}/${packages.length}]`;
console.log(`${progress} Installing ${name}@${version}...`);
const result = await installPackage(name, version, sleepMs);
if (result.success) {
successCount++;
console.log(` SUCCESS: Cached ${result.package}`);
} else {
errorCount++;
console.error(` FAILED: Could not cache ${result.package}`);
}
}
console.log('\n─────────────────────────────');
console.log('Summary:');
console.log(` Success: ${successCount}`);
console.log(` Failed: ${errorCount}`);
console.log(` Total: ${packages.length}`);
console.log('─────────────────────────────');
} catch (error) {
console.error('ERROR:', error.message);
process.exit(1);
}
};
(async () => {
await populatePackages();
})();