summaryrefslogtreecommitdiff
path: root/src/actions/checkDependencies.ts
blob: e728f3a20a9ae4e1c9b5b1fb392018db3983a625 (plain) (blame)
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
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";

export async function checkDependencies() {
  const config: Config = await getConfig();
  let projectAudits = [];
  let projects: Promise<Project>[] = [];

  const spinner = ora("Getting all Project Data").start();

  try {
    const dirs = await fs.readdir(config.path);

    for (let dir of dirs) {
      let dirFullPath = `${config.path}${dir}`;
      const projectDir = await fs.readdir(dirFullPath);
      if (projectDir.includes("package.json")) {
        projects.push(getAuditPromise(dirFullPath, dir, spinner));
      }
    }

    const results = await Promise.allSettled(projects);

    for (const result of results) {
      if (result.status === "fulfilled") {
        projectAudits.push(result.value);
      } else {
        console.log("audit failed:", result.reason);
      }
    }

    spinner.succeed("Got the Data successfully");
  } catch (error) {
    spinner.fail("Ups and Error :(");
    console.log(error);
  }

  return projectAudits;
}

function getAuditPromise(
  path: string,
  dirname: string,
  spinner: any,
): Promise<Project> {
  return new Promise(async (resolve, _) => {
    spinner.text = "pulling latest";
    await promiseExec(`cd "${path}" && git pull `, () => {});
    spinner.text = "getting audit";
    promiseExec(
      `cd "${path}" && npm audit --json`,
      (_: any, stdout: string) => {
        let output = JSON.parse(stdout);
        let project: Project = { projectName: dirname, ...output };
        resolve(project);
      },
    );
  });
}

function promiseExec(cmd: string, callback: any) {
  return new Promise((resolve, _) => {
    exec(cmd, (_, stdout, __) => {
      resolve(callback(_, stdout, __));
    });
  });
}