2256
This commit is contained in:
1 parent
66117d18fb
commit
a5c7fa228e
2 files changed
+29
-8
No files matched your search
@@ -1,12 +1,30 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
|
||||
// 支持环境变量配置 BLOG_ROOT(Linux 部署)
|
||||
// Docker 部署时,BLOG_ROOT 通过 Docker 环境变量传入,不要放在 .env 文件中
|
||||
const envBlogRoot = process.env.BLOG_ROOT;
|
||||
export const BLOG_ROOT = envBlogRoot && envBlogRoot.trim()
|
||||
? path.resolve(envBlogRoot)
|
||||
: path.resolve(process.cwd(), "..");
|
||||
// 获取博客根目录
|
||||
// 优先级:BLOG_ROOT 环境变量 > Docker 挂载 /blog > 上级目录
|
||||
function getBlogRoot(): string {
|
||||
// 1. 环境变量
|
||||
const envRoot = process.env.BLOG_ROOT;
|
||||
if (envRoot && envRoot.trim() && envRoot !== "undefined") {
|
||||
const resolved = path.resolve(envRoot);
|
||||
if (fs.existsSync(path.join(resolved, "content"))) {
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Docker 挂载检测
|
||||
const dockerPath = "/blog";
|
||||
if (fs.existsSync(path.join(dockerPath, "content"))) {
|
||||
return dockerPath;
|
||||
}
|
||||
|
||||
// 3. 回退到上级目录
|
||||
return path.resolve(process.cwd(), "..");
|
||||
}
|
||||
|
||||
export const BLOG_ROOT = getBlogRoot();
|
||||
|
||||
export const CONTENT_DIR = path.join(BLOG_ROOT, "content", "posts");
|
||||
export const STATIC_DIR = path.join(BLOG_ROOT, "static");
|
||||
|
||||
Reference in new issue
Block a user