import fs from "fs/promises"; import ora from "ora"; import type { Config, Project } from "../types.js"; import { getConfig } from "../utils/config.js"; import { exec } from "child_process"; const spinner = ora("Getting all Project Data").start(); export async function checkDependencies() { const config: Config = await getConfig(); let projectAudits: PromiseSettledResult[]; let projects: Promise[] = []; try { const entries = await fs.readdir(config.path, { withFileTypes: true }); for (let entry of entries) { if (!entry.isDirectory()) { continue; } let dirFullPath = `${config.path}${entry.name}`; const projectDir = await fs.readdir(dirFullPath); if (projectDir.includes("package.json")) { let auditPromise = getAuditPromise(dirFullPath, entry.name); projects.push(auditPromise); } } projectAudits = await Promise.allSettled(projects); spinner.succeed("Got the Data successfully"); return projectAudits; } catch (error) { spinner.fail("Ups and Error :("); throw error; } } async function getAuditPromise( path: string, dirname: string, ): Promise { return new Promise(async (resolve, reject) => { await pullLatest(path); spinner.text = "getting audit"; promiseExec( `cd "${path}" && npm audit --json`, (_: any, stdout: string) => { let output = JSON.parse(stdout); let project: Project = { projectName: dirname, ...output }; if (project.error) { reject( `${dirname} could not be audited, maybe package lock is corrupted`, ); } resolve(project); }, ); }); } async function pullLatest(path: string) { spinner.text = "pulling latest"; await promiseExec(`cd "${path}" && git pull `, () => {}); } function promiseExec(cmd: string, callback: any): Promise { return new Promise((resolve, _) => { exec(cmd, (err, stdout, stderr) => { resolve(callback(err, stdout, stderr)); }); }); }