All files config.ts

14% Statements 7/50
50% Branches 1/2
25% Functions 1/4
14% Lines 7/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x                                                                                                                     1x       1x                                                                         1x 62x 62x 62x                  
import { type ConsolaReporter, LogLevels } from "consola";
 
type InputConfig = {
  nodeEnv: "development" | "production" | "test";
  dbUrl: string;
  logger: {
    level?: number;
    reporters?: ConsolaReporter[];
  };
  port?: number;
  // TODO make sure naming is consistent
  paths?: {
    routes?: string;
    commands?: string;
    jobs?: string;
    schedules?: string;
    forms?: string;
    public?: string;
    schema?: string;
    out?: string;
    styles?: string;
    assets?: string;
    seed?: string;
    migrations?: string;
    database?: string;
    http?: string;
    queue?: string;
    mailer?: string;
  };
};
 
export type Config = {
  nodeEnv: "development" | "production" | "test";
  dbUrl: string;
  logger: {
    level: number;
    reporters: ConsolaReporter[];
  };
  port: number;
  paths: {
    routes: string;
    commands: string;
    jobs: string;
    schedules: string;
    forms: string;
    public: string;
    assets: string;
    schema: string;
    out: string;
    styles: string;
    seed: string;
    migrations: string;
    database: string;
    http: string;
    queue: string;
    mailer: string;
  };
};
 
export function defineConfig(config: InputConfig) {
  return config;
}
 
let config: Config | undefined;
 
async function loadConfig() {
  const c12LoadConfig = (await import("c12")).loadConfig;
  const resolvedConfig = await c12LoadConfig<InputConfig>({
    name: "plainstack",
    defaults: {
      nodeEnv: "production",
      dbUrl: "data.db",
      logger: {
        level: LogLevels.info,
        reporters: [],
      } satisfies Config["logger"],
      port: 3000,
      paths: {
        routes: "app/routes",
        commands: "app/commands",
        jobs: "app/jobs",
        schedules: "app/schedules",
        forms: "app/forms",
        public: ".out",
        schema: "app/config/schema.ts",
        out: ".out",
        styles: "assets/styles.css",
        assets: "assets",
        seed: "database/seed.ts",
        migrations: "database/migrations",
        database: "app/config/database.ts",
        http: "app/config/http.ts",
        queue: "app/config/queue.ts",
        mailer: "app/config/mailer.ts",
      } satisfies Config["paths"],
    },
  });
  config = resolvedConfig.config as Config;
}
 
export function getConfig() {
  if (!config) {
    throw new Error("config not loaded, make sure to call loadConfig() first");
  }
  return config;
}
 
export async function loadAndGetConfig() {
  if (config) return config;
  await loadConfig();
  return getConfig();
}