feat(project): init

This commit is contained in:
zhangheng
2026-04-25 11:26:33 +08:00
commit 9bf212f67c
63 changed files with 11141 additions and 0 deletions

51
scripts/load-env.mjs Normal file
View File

@@ -0,0 +1,51 @@
import { readFileSync } from "node:fs";
import path from "node:path";
export function loadEnvFiles({
cwd = process.cwd(),
files = [".env", ".env.local"],
override = false,
} = {}) {
const originalKeys = new Set(Object.keys(process.env));
for (const fileName of files) {
const filePath = path.join(cwd, fileName);
try {
const source = readFileSync(filePath, "utf8");
applyEnvSource(source, { override, originalKeys });
} catch {
continue;
}
}
}
function applyEnvSource(source, { override, originalKeys }) {
for (const line of source.split(/\r?\n/)) {
const trimmedLine = line.trim();
if (!trimmedLine || trimmedLine.startsWith("#")) {
continue;
}
const separatorIndex = trimmedLine.indexOf("=");
if (separatorIndex === -1) {
continue;
}
const key = trimmedLine.slice(0, separatorIndex).trim();
const rawValue = trimmedLine.slice(separatorIndex + 1).trim();
const value = rawValue.replace(/^['"]|['"]$/g, "");
if (!key) {
continue;
}
if (!override && originalKeys.has(key)) {
continue;
}
process.env[key] = value;
}
}