summaryrefslogtreecommitdiff
path: root/src/utils/config.ts
blob: 070631253ff86f3d481b5d086d37da291ddf820c (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
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(configFile);
    console.log(`You already have a config at: ${configFile}`);
  } catch {
    await fs.mkdir(configPath, { recursive: true });
    await fs.writeFile(`${configFile}`, JSON.stringify(sampleConfig, null, 2));
    console.log(`Config got generated: ${configFile}`);
  }
};