-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfetch.js
83 lines (71 loc) · 2.28 KB
/
fetch.js
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
// Import Node.js Dependencies
import path from "node:path";
// Import Third-party Dependencies
import kleur from "kleur";
// Import Internal Dependencies
import { buildStatsFromNsecurePayloads } from "./extractScannerData.js";
import * as scanner from "./scanner.js";
import * as localStorage from "../localStorage.js";
import * as utils from "../utils/index.js";
import * as CONSTANTS from "../constants.js";
export async function fetchPackagesAndRepositoriesData() {
const config = localStorage.getConfig().report;
const fetchNpm = config.npm?.packages.length > 0;
const fetchGit = config.git?.repositories.length > 0;
if (!fetchGit && !fetchNpm) {
throw new Error(
"No git repositories and no npm packages to fetch in the local configuration!"
);
}
const pkgStats = fetchNpm ?
await fetchPackagesStats(
utils.formatNpmPackages(
config.npm.organizationPrefix,
config.npm.packages
)
) :
null;
const { repositories, organizationUrl } = config.git;
const repoStats = fetchGit ?
await fetchRepositoriesStats(
repositories,
organizationUrl
) :
null;
return { pkgStats, repoStats };
}
async function fetchPackagesStats(packages) {
const jsonFiles = await utils.runInSpinner(
{
title: `[Fetcher: ${kleur.yellow().bold("NPM")}]`,
start: "Fetching NPM packages metadata on the NPM Registry"
},
async() => Promise.all(packages.map(scanner.from))
);
return buildStatsFromNsecurePayloads(
jsonFiles.filter((value) => value !== null)
);
}
async function fetchRepositoriesStats(repositories, organizationUrl) {
const jsonFiles = await utils.runInSpinner(
{
title: `[Fetcher: ${kleur.yellow().bold("GIT")}]`,
start: "Cloning GIT repositories"
},
async(spinner) => {
const repos = await Promise.all(
repositories.map((repositoryName) => {
return utils.cloneGITRepository(
path.join(CONSTANTS.DIRS.CLONES, repositoryName),
`${organizationUrl}/${repositoryName}.git`
);
})
);
spinner.text = "Fetching repositories metadata on the NPM Registry";
return Promise.all(repos.map(scanner.cwd));
}
);
return buildStatsFromNsecurePayloads(
jsonFiles.filter((value) => value !== null)
);
}