mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-15 10:30:37 +09:00
fix: isolate dev runner worktree env
This commit is contained in:
parent
03a2cf5c8a
commit
b6115424b1
4 changed files with 165 additions and 0 deletions
81
server/src/dev-runner-worktree.ts
Normal file
81
server/src/dev-runner-worktree.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
function parseEnvFile(contents: string): Record<string, string> {
|
||||
const entries: Record<string, string> = {};
|
||||
|
||||
for (const rawLine of contents.split(/\r?\n/)) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith("#")) continue;
|
||||
|
||||
const match = rawLine.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/);
|
||||
if (!match) continue;
|
||||
|
||||
const [, key, rawValue] = match;
|
||||
const value = rawValue.trim();
|
||||
if (!value) {
|
||||
entries[key] = "";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
(value.startsWith("\"") && value.endsWith("\"")) ||
|
||||
(value.startsWith("'") && value.endsWith("'"))
|
||||
) {
|
||||
entries[key] = value.slice(1, -1);
|
||||
continue;
|
||||
}
|
||||
|
||||
entries[key] = value.replace(/\s+#.*$/, "").trim();
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
export function isLinkedGitWorktreeCheckout(rootDir: string): boolean {
|
||||
const gitMetadataPath = path.join(rootDir, ".git");
|
||||
if (!existsSync(gitMetadataPath)) return false;
|
||||
|
||||
const stat = lstatSync(gitMetadataPath);
|
||||
if (!stat.isFile()) return false;
|
||||
|
||||
return readFileSync(gitMetadataPath, "utf8").trimStart().startsWith("gitdir:");
|
||||
}
|
||||
|
||||
export function resolveWorktreeEnvFilePath(rootDir: string): string {
|
||||
return path.resolve(rootDir, ".paperclip", ".env");
|
||||
}
|
||||
|
||||
export function bootstrapDevRunnerWorktreeEnv(
|
||||
rootDir: string,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): {
|
||||
envPath: string | null;
|
||||
missingEnv: boolean;
|
||||
} {
|
||||
if (!isLinkedGitWorktreeCheckout(rootDir)) {
|
||||
return {
|
||||
envPath: null,
|
||||
missingEnv: false,
|
||||
};
|
||||
}
|
||||
|
||||
const envPath = resolveWorktreeEnvFilePath(rootDir);
|
||||
if (!existsSync(envPath)) {
|
||||
return {
|
||||
envPath,
|
||||
missingEnv: true,
|
||||
};
|
||||
}
|
||||
|
||||
const entries = parseEnvFile(readFileSync(envPath, "utf8"));
|
||||
for (const [key, value] of Object.entries(entries)) {
|
||||
if (typeof env[key] === "string" && env[key]!.trim().length > 0) continue;
|
||||
env[key] = value;
|
||||
}
|
||||
|
||||
return {
|
||||
envPath,
|
||||
missingEnv: false,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue