2026-06-15 15:04:43 -07:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Standalone Plaza dev: Portal API + Next.js + optional :80 reverse proxy.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Usage:
|
|
|
|
|
|
* pnpm dev:plaza
|
|
|
|
|
|
* sudo pnpm dev:plaza-proxy # separate terminal, maps plaza.tkmind.cn:80 -> :3001
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { spawn, execSync } from 'node:child_process';
|
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
|
|
|
|
|
|
|
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
2026-06-28 12:18:26 +08:00
|
|
|
|
const defaultPlazaDir = path.join(root, '../memind_plaza');
|
2026-06-26 15:19:03 +08:00
|
|
|
|
const plazaDir = process.env.PLAZA_APP_DIR ?? defaultPlazaDir;
|
2026-06-15 15:04:43 -07:00
|
|
|
|
const portalPort = Number(process.env.H5_PORT ?? 8081);
|
|
|
|
|
|
const plazaPort = Number(process.env.PLAZA_PORT ?? 3001);
|
|
|
|
|
|
const publicPort = Number(process.env.PLAZA_PUBLIC_PORT ?? 443);
|
|
|
|
|
|
const plazaHost = process.env.PLAZA_LOCAL_HOST ?? 'plaza.tkmind.cn';
|
|
|
|
|
|
const plazaPublicScheme = String(process.env.PLAZA_PUBLIC_BASE ?? 'https://plaza.tkmind.cn').startsWith('http://')
|
|
|
|
|
|
? 'http'
|
|
|
|
|
|
: 'https';
|
|
|
|
|
|
const plazaPublicBase = (
|
|
|
|
|
|
process.env.PLAZA_PUBLIC_BASE ??
|
|
|
|
|
|
(publicPort === 80 || publicPort === 443
|
|
|
|
|
|
? `${plazaPublicScheme}://${plazaHost}`
|
|
|
|
|
|
: `${plazaPublicScheme}://${plazaHost}:${publicPort}`)
|
|
|
|
|
|
).replace(/\/$/, '');
|
|
|
|
|
|
const portalUrl = `http://127.0.0.1:${portalPort}`;
|
|
|
|
|
|
|
|
|
|
|
|
function loadEnvFile(filePath) {
|
|
|
|
|
|
if (!fs.existsSync(filePath)) return;
|
|
|
|
|
|
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
|
|
|
|
|
|
const trimmed = line.trim();
|
|
|
|
|
|
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
|
|
|
|
const eq = trimmed.indexOf('=');
|
|
|
|
|
|
if (eq < 0) continue;
|
|
|
|
|
|
const key = trimmed.slice(0, eq).trim();
|
|
|
|
|
|
const value = trimmed.slice(eq + 1).trim();
|
|
|
|
|
|
if (!process.env[key]) process.env[key] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
loadEnvFile(path.join(root, '../../.env.local'));
|
|
|
|
|
|
loadEnvFile(path.join(root, '.env'));
|
|
|
|
|
|
|
|
|
|
|
|
function plazaHostConfigured(host) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const hosts = fs.readFileSync('/etc/hosts', 'utf8');
|
|
|
|
|
|
return new RegExp(`^\\s*127\\.0\\.0\\.1\\s+${host.replace('.', '\\.')}(\\s|$)`, 'm').test(hosts);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function freePort(port) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
execSync(`lsof -ti TCP:${port} -sTCP:LISTEN | xargs kill -9`, { stdio: 'ignore' });
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// already free
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function portOpen(port) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch(`http://127.0.0.1:${port}/auth/status`);
|
|
|
|
|
|
return res.ok;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function waitFor(check, label, retries = 80) {
|
|
|
|
|
|
for (let i = 0; i < retries; i += 1) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (await check()) return;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// still starting
|
|
|
|
|
|
}
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Error(`${label} 启动超时`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let portal;
|
|
|
|
|
|
let plaza;
|
2026-06-15 22:09:38 -07:00
|
|
|
|
let dnsServer;
|
2026-06-15 15:04:43 -07:00
|
|
|
|
let stopping = false;
|
|
|
|
|
|
|
|
|
|
|
|
function spawnChild(command, args, label, cwd = root, extraEnv = {}) {
|
|
|
|
|
|
const child = spawn(command, args, {
|
|
|
|
|
|
cwd,
|
|
|
|
|
|
env: { ...process.env, ...extraEnv },
|
|
|
|
|
|
stdio: 'inherit',
|
|
|
|
|
|
});
|
|
|
|
|
|
child.on('exit', (code, signal) => {
|
|
|
|
|
|
if (signal || stopping) return;
|
|
|
|
|
|
if (code && code !== 0) {
|
|
|
|
|
|
console.error(`[${label}] exited with code ${code}`);
|
|
|
|
|
|
shutdown(code ?? 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return child;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function shutdown(code = 0) {
|
|
|
|
|
|
if (stopping) return;
|
|
|
|
|
|
stopping = true;
|
|
|
|
|
|
portal?.kill('SIGTERM');
|
|
|
|
|
|
plaza?.kill('SIGTERM');
|
2026-06-15 22:09:38 -07:00
|
|
|
|
dnsServer?.kill('SIGTERM');
|
2026-06-15 15:04:43 -07:00
|
|
|
|
setTimeout(() => process.exit(code), 300);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.on('SIGINT', () => shutdown(0));
|
|
|
|
|
|
process.on('SIGTERM', () => shutdown(0));
|
|
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(path.join(plazaDir, 'package.json'))) {
|
2026-06-28 12:18:26 +08:00
|
|
|
|
console.error(`未找到 Plaza Next.js:${plazaDir}。请设置 PLAZA_APP_DIR 指向本地 Plaza 源码,例如 ../memind_plaza`);
|
2026-06-15 15:04:43 -07:00
|
|
|
|
process.exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-15 22:09:38 -07:00
|
|
|
|
function plazaResolverConfigured(host) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const body = fs.readFileSync(`/etc/resolver/${host}`, 'utf8');
|
|
|
|
|
|
return body.includes('tkmind-plaza-local');
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!plazaHostConfigured(plazaHost) || !plazaResolverConfigured(plazaHost)) {
|
2026-06-15 15:04:43 -07:00
|
|
|
|
console.warn('');
|
2026-06-15 22:09:38 -07:00
|
|
|
|
console.warn(`⚠ ${plazaHost} 本地 DNS 未完整配置(Tailscale/Clash 会劫持解析)`);
|
2026-06-15 15:04:43 -07:00
|
|
|
|
console.warn(' 请执行:sudo pnpm setup:plaza-dns');
|
|
|
|
|
|
console.warn('');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const mindSpacePublicBase = (
|
|
|
|
|
|
process.env.H5_PUBLIC_BASE_URL ?? `http://localhost:${portalPort}`
|
|
|
|
|
|
).replace(/\/$/, '');
|
|
|
|
|
|
|
|
|
|
|
|
const plazaEnv = {
|
|
|
|
|
|
PLAZA_API_PROXY: portalUrl,
|
|
|
|
|
|
PLAZA_API_BASE: portalUrl,
|
|
|
|
|
|
PLAZA_PUBLIC_BASE: plazaPublicBase,
|
|
|
|
|
|
NEXT_PUBLIC_API_BASE: plazaPublicBase,
|
|
|
|
|
|
NEXT_PUBLIC_SITE_BASE: plazaPublicBase,
|
|
|
|
|
|
NEXT_PUBLIC_MINDSPACE_BASE: mindSpacePublicBase,
|
|
|
|
|
|
NEXT_PUBLIC_PLAZA_BASE: '/plaza',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`==> Plaza 独立开发模式`);
|
|
|
|
|
|
console.log(` 公开地址 ${plazaPublicBase}/plaza`);
|
|
|
|
|
|
console.log(` 内部端口 ${plazaPort}`);
|
|
|
|
|
|
|
|
|
|
|
|
freePort(plazaPort);
|
2026-06-15 22:09:38 -07:00
|
|
|
|
freePort(Number(process.env.PLAZA_LOCAL_DNS_PORT ?? 5533));
|
2026-06-15 15:04:43 -07:00
|
|
|
|
|
|
|
|
|
|
try {
|
2026-06-15 22:09:38 -07:00
|
|
|
|
console.log('==> 启动 Plaza 本地 DNS(覆盖 Tailscale fake-ip)');
|
|
|
|
|
|
dnsServer = spawnChild('node', ['scripts/plaza-local-dns-server.mjs'], 'plaza-dns');
|
|
|
|
|
|
|
2026-06-15 15:04:43 -07:00
|
|
|
|
if (!(await portOpen(portalPort))) {
|
|
|
|
|
|
console.log(`==> 启动 Portal @ ${portalUrl}`);
|
|
|
|
|
|
portal = spawnChild('node', ['server.mjs'], 'portal');
|
|
|
|
|
|
await waitFor(() => portOpen(portalPort), 'Portal');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log(`==> Portal 已在运行: ${portalUrl}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`==> 启动 Plaza Next.js (内部 :${plazaPort})`);
|
|
|
|
|
|
plaza = spawnChild(
|
|
|
|
|
|
'npm',
|
|
|
|
|
|
['run', 'dev', '--', '-H', '0.0.0.0', '-p', String(plazaPort)],
|
|
|
|
|
|
'plaza',
|
|
|
|
|
|
plazaDir,
|
|
|
|
|
|
plazaEnv,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
|
|
const res = await fetch(`http://127.0.0.1:${plazaPort}/plaza`, {
|
|
|
|
|
|
headers: { Host: `${plazaHost}:${plazaPort}` },
|
|
|
|
|
|
});
|
|
|
|
|
|
return res.ok || res.status === 307 || res.status === 308;
|
|
|
|
|
|
}, 'Plaza Next.js');
|
|
|
|
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
console.log('Plaza 已就绪(内部):');
|
|
|
|
|
|
console.log(` http://127.0.0.1:${plazaPort}/plaza`);
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
console.log('公开访问(需 80 端口代理):');
|
|
|
|
|
|
console.log(` ${plazaPublicBase}/plaza`);
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
console.log('若浏览器还打不开 plaza.tkmind.cn,另开终端执行:');
|
|
|
|
|
|
console.log(' sudo pnpm dev:plaza-proxy');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err instanceof Error ? err.message : err);
|
|
|
|
|
|
shutdown(1);
|
|
|
|
|
|
}
|