diff options
| author | Leo Goetz <dev@leogtz.de> | 2026-05-08 15:48:37 +0200 |
|---|---|---|
| committer | Leo Goetz <dev@leogtz.de> | 2026-05-08 15:48:37 +0200 |
| commit | c3cb0ca317a52a740bf9625ca9df43f5c2306548 (patch) | |
| tree | aa8877dc2d651f16d23a629c4054043cae8be156 /src/actions | |
inital commit
Diffstat (limited to 'src/actions')
| -rw-r--r-- | src/actions/checkDependencies.ts | 62 | ||||
| -rw-r--r-- | src/actions/outputSummary.ts | 15 | ||||
| -rw-r--r-- | src/actions/sendEmail.ts | 27 |
3 files changed, 104 insertions, 0 deletions
diff --git a/src/actions/checkDependencies.ts b/src/actions/checkDependencies.ts new file mode 100644 index 0000000..4746bfd --- /dev/null +++ b/src/actions/checkDependencies.ts @@ -0,0 +1,62 @@ +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 projectsOutputs: Project[] = []; + 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(getProjectPromise(dirFullPath, dir, spinner)); + } + } + + projectsOutputs = await Promise.all(projects); + + spinner.succeed("Got the Data successfully"); + } catch (error) { + spinner.fail("Ups and Error :("); + console.log(error); + } + + return projectsOutputs; +} + +function getProjectPromise( + 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, __)); + }); + }); +} diff --git a/src/actions/outputSummary.ts b/src/actions/outputSummary.ts new file mode 100644 index 0000000..5a376cb --- /dev/null +++ b/src/actions/outputSummary.ts @@ -0,0 +1,15 @@ +import chalk from "chalk"; +import type { Project } from "../types.js"; + +export function outputSummary(projects: Project[]) { + const text = ` + This is what i found: + ${projects.map((project) => { + let projectVulnerabilities = project.metadata.vulnerabilities.total; + return ` + ${project.projectName} has ${projectVulnerabilities > 0 ? chalk.bold.red(projectVulnerabilities) : chalk.bold.green(projectVulnerabilities)} Security Issues`; + })} + `; + + console.log(text); +} diff --git a/src/actions/sendEmail.ts b/src/actions/sendEmail.ts new file mode 100644 index 0000000..50394c5 --- /dev/null +++ b/src/actions/sendEmail.ts @@ -0,0 +1,27 @@ +import nodemailer from "nodemailer"; +import { emailConfig } from "../utils/email.js"; +import type { Project } from "../types.js"; + +const transporter = nodemailer.createTransport(emailConfig); + +export const sendAuditEmail = async (projects: Project[]) => { + const text = emailContent(projects); + const email = await transporter.sendMail({ + from: `"${emailConfig.senderName}" <${emailConfig.senderEmail}>`, + to: `${emailConfig.reciever}`, + subject: emailConfig.subject ?? "Dependency Audit!", + text: text, + }); + + return email; +}; + +const emailContent = (projects: Project[]): string => { + return `Here is your Report: +${projects.map((project) => { + let projectVulnerabilities = project.metadata.vulnerabilities.total; + return ` +${project.projectName} has ${projectVulnerabilities} Security Issues`; +})} +`; +}; |
