UNPKG

@stryke/prisma-trpc-generator

Version:

A fork of the prisma-trpc-generator code to work in ESM with Prisma v6.

85 lines (83 loc) 4.31 kB
import { titleCase } from "../../string-format/src/title-case.mjs"; import { joinPaths } from "@stryke/path/join-paths"; import path from "node:path"; import os from "node:os"; //#region ../env/src/get-env-paths.ts const homedir = os.homedir(); const tmpdir = os.tmpdir(); const macos = (orgId) => { const library = joinPaths(homedir, "Library"); return { data: joinPaths(library, "Application Support", orgId), config: joinPaths(library, "Preferences", orgId), cache: joinPaths(library, "Caches", orgId), log: joinPaths(library, "Logs", orgId), temp: joinPaths(tmpdir, orgId) }; }; const windows = (orgId) => { const appData = process.env.APPDATA || joinPaths(homedir, "AppData", "Roaming"); const localAppData = process.env.LOCALAPPDATA || joinPaths(homedir, "AppData", "Local"); const windowsFormattedOrgId = titleCase(orgId).trim().replace(/\s+/g, ""); return { data: joinPaths(localAppData, windowsFormattedOrgId, "Data"), config: joinPaths(appData, windowsFormattedOrgId, "Config"), cache: joinPaths(localAppData, "Cache", orgId), log: joinPaths(localAppData, windowsFormattedOrgId, "Log"), temp: joinPaths(tmpdir, orgId) }; }; const linux = (orgId) => { const username = path.basename(homedir); return { data: joinPaths(process.env.XDG_DATA_HOME || joinPaths(homedir, ".local", "share"), orgId), config: joinPaths(process.env.XDG_CONFIG_HOME || joinPaths(homedir, ".config"), orgId), cache: joinPaths(process.env.XDG_CACHE_HOME || joinPaths(homedir, ".cache"), orgId), log: joinPaths(process.env.XDG_STATE_HOME || joinPaths(homedir, ".local", "state"), orgId), temp: process.env.DEVENV_RUNTIME || process.env.XDG_RUNTIME_DIR ? joinPaths(process.env.DEVENV_RUNTIME || process.env.XDG_RUNTIME_DIR, orgId) : joinPaths(tmpdir, username, orgId) }; }; /** * Get paths for storing things like data, config, logs, and cache in the current runtime environment. * * @remarks * On macOS, directories are generally created in `~/Library/Application Support/<name>`. * On Windows, directories are generally created in `%AppData%/<name>`. * On Linux, directories are generally created in `~/.config/<name>` - this is determined via the [XDG Base Directory spec](https://specifications.freedesktop.org/basedir-spec/latest/). * * If the `STORM_DATA_DIR`, `STORM_CONFIG_DIR`, `STORM_CACHE_DIR`, `STORM_LOG_DIR`, or `STORM_TEMP_DIR` environment variables are set, they will be used instead of the default paths. * * @param options - Parameters used to determine the specific paths for the current project/runtime environment * @returns An object containing the various paths for the runtime environment */ function getEnvPaths(options = {}) { let orgId = options.orgId || "storm-software"; if (!orgId) throw new Error("You need to provide an orgId to the `getEnvPaths` function"); if (options.suffix) orgId += `-${typeof options.suffix === "string" ? options.suffix : "nodejs"}`; let result = {}; if (process.platform === "darwin") result = macos(orgId); else if (process.platform === "win32") result = windows(orgId); else result = linux(orgId); if (process.env.STORM_DATA_DIR) result.data = process.env.STORM_DATA_DIR; else if (process.env.STORM_CONFIG_DIR) result.config = process.env.STORM_CONFIG_DIR; else if (process.env.STORM_CACHE_DIR) result.cache = process.env.STORM_CACHE_DIR; else if (process.env.STORM_LOG_DIR) result.log = process.env.STORM_LOG_DIR; else if (process.env.STORM_TEMP_DIR) result.temp = process.env.STORM_TEMP_DIR; if (options.workspaceRoot) { result.cache ??= joinPaths(options.workspaceRoot, "node_modules", ".cache", orgId); result.temp ??= joinPaths(options.workspaceRoot, "tmp", orgId); result.log ??= joinPaths(result.temp, "logs"); result.config ??= joinPaths(options.workspaceRoot, ".config", orgId); } return Object.keys(result).reduce((ret, key) => { if (result[key]) { const filePath = result[key]; ret[key] = options.appId && options.appId !== options.orgId && options.appId !== options.nestedDir ? joinPaths(filePath, options.appId) : filePath; if (options.nestedDir && options.nestedDir !== options.orgId && options.nestedDir !== options.appId) ret[key] = joinPaths(ret[key], options.nestedDir); } return ret; }, {}); } //#endregion export { getEnvPaths }; //# sourceMappingURL=get-env-paths.mjs.map