-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathingestion.ts
66 lines (57 loc) · 2.35 KB
/
ingestion.ts
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
import * as fs from 'fs';
import * as childProcess from 'child_process';
import axios from 'axios';
export async function pkg_download(score: number, pkg_name: string, registry_url: string){
//check if package score is elgible for ingest to upload
if(score>=0.5){
//install the public package in currenty directory
const command = `npm install -g ${pkg_name}`;
//execut the install command
childProcess.exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
} else {
console.log(`Package downloaded successfully!`);
console.log(`stdout: ${stdout}`);
//configure private registry for upload
fs.writeFileSync('.npmrc', `registry=${registry_url}`);
//change to directory of package
process.chdir(pkg_name);
//publish the qualified package to registry
childProcess.exec('npm publish', (error, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}else{
console.log(`Package published successfully!`);
}
});
}
});
}else{
console.error('Package metrics do not meet the required criteria.');
}
}
export async function reset_state(registryUrl: string){
clearRegistry(registryUrl);
//clear the link with registry
fs.unlinkSync('.npmrc');
//reset to default public registry
const defaultRegistryUrl = 'https://registry.npmjs.org/';
fs.writeFileSync('.npmrc', `registry=${defaultRegistryUrl}`);
}
async function clearRegistry(registryUrl: string): Promise<void> {
try {
// Fetch a list of all packages in the registry
const response = await axios.get(`${registryUrl}/-/all`);
const packages = response.data;
// Iterate through the packages and remove each one
for (const packageName of Object.keys(packages)) {
await axios.delete(`${registryUrl}/${packageName}`);
console.log(`Package ${packageName} deleted.`);
}
console.log('Registry cleared successfully.');
} catch (error) {
console.error(`Error clearing registry: ${error.message}`);
}
}