mymusics

retro MySpace-style music player
Log | Files | Refs | README

paths.ts (1477B)


      1 import fs from "node:fs";
      2 import path from "node:path";
      3 
      4 export function getProjectRoot(): string {
      5   return process.cwd();
      6 }
      7 
      8 export function resolvePath(p: string, root = getProjectRoot()): string {
      9   return path.isAbsolute(p) ? p : path.resolve(root, p);
     10 }
     11 
     12 export function bundledMetadataTsv(root = getProjectRoot()): string {
     13   return path.join(root, "data", "metadata.tsv");
     14 }
     15 
     16 export function bundledTracksDb(root = getProjectRoot()): string {
     17   return path.join(root, "data", "tracks.db");
     18 }
     19 
     20 export function resolveEffectiveMetadataTsv(
     21   env: NodeJS.ProcessEnv,
     22   root = getProjectRoot(),
     23 ): { path: string; envRequested: string | null; usedFallback: boolean } {
     24   const bundled = bundledMetadataTsv(root);
     25   const raw = env.METADATA_TSV?.trim();
     26   if (!raw) {
     27     return { path: bundled, envRequested: null, usedFallback: false };
     28   }
     29   const resolved = resolvePath(raw, root);
     30   if (fs.existsSync(resolved)) {
     31     return { path: resolved, envRequested: resolved, usedFallback: false };
     32   }
     33   if (fs.existsSync(bundled)) {
     34     console.warn(`MyMusics: METADATA_TSV not found at ${resolved}; using bundled ${bundled}`);
     35     return { path: bundled, envRequested: resolved, usedFallback: true };
     36   }
     37   return { path: resolved, envRequested: resolved, usedFallback: false };
     38 }
     39 
     40 export function resolveTracksDb(env: NodeJS.ProcessEnv, root = getProjectRoot()): string {
     41   const raw = env.TRACKS_DB?.trim();
     42   return raw ? resolvePath(raw, root) : bundledTracksDb(root);
     43 }