111
This commit is contained in:
135
scripts/init-content-repo.js
Normal file
135
scripts/init-content-repo.js
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Mizuki 内容仓库初始化脚本
|
||||
* 帮助用户快速设置代码内容分离
|
||||
*/
|
||||
|
||||
import { execSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import readline from "readline";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const rootDir = path.resolve(__dirname, "..");
|
||||
|
||||
// 加载 .env 文件的辅助函数
|
||||
function loadEnvFile(envPath) {
|
||||
if (!fs.existsSync(envPath)) return;
|
||||
|
||||
const envContent = fs.readFileSync(envPath, "utf-8");
|
||||
envContent.split("\n").forEach((line) => {
|
||||
line = line.trim();
|
||||
if (!line || line.startsWith("#")) return;
|
||||
|
||||
const match = line.match(/^([^=]+)=(.*)$/);
|
||||
if (match) {
|
||||
const key = match[1].trim();
|
||||
let value = match[2].trim();
|
||||
value = value.replace(/^["']|["']$/g, "");
|
||||
process.env[key] = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 加载现有的 .env 文件
|
||||
loadEnvFile(path.join(rootDir, ".env"));
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
|
||||
function question(query) {
|
||||
return new Promise((resolve) => rl.question(query, resolve));
|
||||
}
|
||||
|
||||
function exec(command, options = {}) {
|
||||
try {
|
||||
return execSync(command, { stdio: "inherit", ...options });
|
||||
} catch (error) {
|
||||
console.error(`Command execution failed: ${command}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("Mizuki Content Repository Initialization\n");
|
||||
|
||||
console.log("Using independent repository mode to manage content\n");
|
||||
|
||||
// 询问内容仓库 URL
|
||||
const repoUrl = await question("Content repository URL: ");
|
||||
|
||||
if (!repoUrl.trim()) {
|
||||
console.error("Error: Content repository URL cannot be empty!");
|
||||
rl.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// 确认信息
|
||||
console.log("\nConfiguration:");
|
||||
console.log(" Mode: Independent Repository");
|
||||
console.log(` Repository: ${repoUrl.trim()}`);
|
||||
|
||||
const confirm = await question("\nConfirm initialization? (y/n): ");
|
||||
|
||||
if (confirm.toLowerCase() !== "y") {
|
||||
console.log("Initialization cancelled");
|
||||
rl.close();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("\nStarting initialization...\n");
|
||||
|
||||
// 创建 .env 文件
|
||||
const envPath = path.join(rootDir, ".env");
|
||||
const envContent = `# Mizuki Content Repository Configuration
|
||||
# Auto-generated by initialization script
|
||||
|
||||
CONTENT_REPO_URL=${repoUrl.trim()}
|
||||
CONTENT_DIR=./content
|
||||
|
||||
# Umami configuration (optional)
|
||||
# UMAMI_API_KEY=your_api_key_here
|
||||
|
||||
# bcrypt configuration
|
||||
BCRYPT_SALT_ROUNDS=12
|
||||
`;
|
||||
|
||||
fs.writeFileSync(envPath, envContent);
|
||||
console.log("Created .env file");
|
||||
|
||||
// 同步内容
|
||||
console.log("Synchronizing content repository...");
|
||||
try {
|
||||
exec("pnpm run sync-content", {
|
||||
cwd: rootDir,
|
||||
env: {
|
||||
...process.env,
|
||||
CONTENT_REPO_URL: repoUrl.trim(),
|
||||
},
|
||||
});
|
||||
console.log("Content synchronized successfully");
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Content synchronization failed. Run manually: pnpm run sync-content",
|
||||
);
|
||||
}
|
||||
|
||||
// 提示后续步骤
|
||||
console.log("\nInitialization completed\n");
|
||||
console.log("\nDocumentation:");
|
||||
console.log("- Content repository: docs/CONTENT_REPOSITORY.md");
|
||||
console.log("- Migration guide: docs/MIGRATION_GUIDE.md");
|
||||
|
||||
rl.close();
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("Initialization failed:", error);
|
||||
rl.close();
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user