summaryrefslogtreecommitdiff
path: root/src/utils/config.ts
blob: 62080f7358c491be7f8a8fa6d72bcc5ea7a40199 (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
import os from "os";
import fs from "fs/promises";
import type { Config } from "../types.js";

const configPath: string = new URL(
  `${os.homedir()}/.config/dephelp/`,
  import.meta.url,
).pathname;

const configFile: string = `${configPath}/config.json`;

const sampleConfig: Config = {
  path: "/your/projects/ (absolute path!)",
  email: {
    host: "mail.example.com",
    port: 587,
    secure: true,
    auth: {
      user: "example",
      pass: "example-password",
    },
    senderEmail: "audit@example.com",
    senderName: "Dependency Reporter",
    subject: "Your Project(s) are insecure!",
    reciever: "your@beautiful.email",
  },
};

export const getConfig = async (): Promise<Config> => {
  const content = await fs.readFile(configFile, { encoding: "utf8" });
  const config: Config = JSON.parse(content);
  return config;
};

export const initConfig = async () => {
  try {
    await fs.access(configPath);
  } catch {
    await fs.mkdir(configPath);
  }

  try {
    await fs.writeFile(`${configFile}`, JSON.stringify(sampleConfig, null, 2));
    console.log(`Config got generated in /.config/dephelp/config.json`);
  } catch (error) {
    console.error(error);
  }
};