mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-25 09:24:54 +10:00
fix: auto-load .env, resolves #2987
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, expect, test } from "vitest";
|
||||
import { findWorkspaceRoot, getLocalDataDirectory } from "./monorepo.node";
|
||||
|
||||
let workspaceRoot: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
workspaceRoot = await mkdtemp(join(tmpdir(), "reactive-resume-utils-"));
|
||||
await mkdir(join(workspaceRoot, "apps", "web", ".output", "server"), { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(workspaceRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("finds the pnpm workspace root from a nested directory", async () => {
|
||||
await writeFile(join(workspaceRoot, "pnpm-workspace.yaml"), "packages:\n - apps/*\n - packages/*\n");
|
||||
|
||||
const root = findWorkspaceRoot(join(workspaceRoot, "apps", "web", ".output", "server"));
|
||||
|
||||
expect(root).toBe(realpathSync(workspaceRoot));
|
||||
});
|
||||
|
||||
test("rejects a pnpm workspace manifest with a .yml extension", async () => {
|
||||
await writeFile(join(workspaceRoot, "pnpm-workspace.yml"), "packages:\n - apps/*\n - packages/*\n");
|
||||
|
||||
expect(() => findWorkspaceRoot(join(workspaceRoot, "apps", "web", ".output", "server"))).toThrow(
|
||||
/The workspace manifest file should be named "pnpm-workspace\.yaml"\. File found: .*pnpm-workspace\.yml/,
|
||||
);
|
||||
});
|
||||
|
||||
test("returns the workspace data directory from a nested directory", async () => {
|
||||
await writeFile(join(workspaceRoot, "pnpm-workspace.yaml"), "packages:\n - apps/*\n - packages/*\n");
|
||||
|
||||
const dataDirectory = getLocalDataDirectory(join(workspaceRoot, "apps", "web", ".output", "server"));
|
||||
|
||||
expect(dataDirectory).toBe(join(realpathSync(workspaceRoot), "data"));
|
||||
});
|
||||
|
||||
test("falls back to the cwd data directory outside a workspace", () => {
|
||||
const dataDirectory = getLocalDataDirectory(workspaceRoot);
|
||||
|
||||
expect(dataDirectory).toBe(join(realpathSync(workspaceRoot), "data"));
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { findUpSync } from "find-up";
|
||||
|
||||
export const findWorkspaceRoot = (cwd = process.cwd()) => {
|
||||
const workspaceManifestPath = findUpSync("pnpm-workspace.yaml", { cwd: realpathSync(cwd) });
|
||||
|
||||
return workspaceManifestPath ? dirname(workspaceManifestPath) : null;
|
||||
};
|
||||
|
||||
export const getLocalDataDirectory = (cwd = process.cwd()) => {
|
||||
const workspaceRoot = findWorkspaceRoot(cwd);
|
||||
|
||||
return join(workspaceRoot ?? realpathSync(cwd), "data");
|
||||
};
|
||||
Reference in New Issue
Block a user