mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 03:19:18 +08:00
52 lines
2.1 KiB
TypeScript
52 lines
2.1 KiB
TypeScript
import { execSync } from 'child_process';
|
|
import { existsSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
import { Client } from 'ssh2';
|
|
import { syncFrontendArtifacts } from '../artifacts.js';
|
|
import { getArtifactDir, getFrontendDir, resolveFrontendDeployDir, resolveFrontendIndexDir } from '../config.js';
|
|
import { createTarGz, getLocalTempPath } from '../platform.js';
|
|
import { execRemote, uploadFile, ensureRemoteDir } from '../ssh.js';
|
|
import { printInfo, printSuccess } from '../ui/render.js';
|
|
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
|
|
|
|
export const serviceType: Service = 'frontend';
|
|
|
|
export async function build(): Promise<string> {
|
|
const frontendDir = getFrontendDir();
|
|
if (!existsSync(frontendDir)) throw new Error(`前端项目目录不存在: ${frontendDir}`);
|
|
|
|
printInfo('构建前端...');
|
|
execSync('pnpm install && pnpm build', { stdio: 'inherit', cwd: frontendDir });
|
|
printSuccess('前端构建完成');
|
|
|
|
const distDir = join(frontendDir, 'apps', 'web-antd', 'dist');
|
|
if (!existsSync(distDir)) throw new Error(`构建产物目录不存在: ${distDir}`);
|
|
|
|
const artifactDir = syncFrontendArtifacts(distDir);
|
|
printSuccess(`前端产物: ${artifactDir}`);
|
|
return artifactDir;
|
|
}
|
|
|
|
/**
|
|
* 上传静态产物到 {deployPath}/ecom-dmb-{env}/index/
|
|
* 例:/opt/ecom-dmb-test/index
|
|
*/
|
|
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, env: Environment): Promise<void> {
|
|
const buildDir = getArtifactDir('frontend');
|
|
const siteDir = resolveFrontendDeployDir(cfg, env);
|
|
const indexDir = resolveFrontendIndexDir(cfg, env);
|
|
await ensureRemoteDir(conn, siteDir);
|
|
|
|
const tmpTar = getLocalTempPath('ecom-frontend-build.tar.gz');
|
|
createTarGz(buildDir, tmpTar);
|
|
await uploadFile(conn, tmpTar, `${siteDir}/frontend.tar.gz`);
|
|
|
|
const { code, stderr } = await execRemote(
|
|
conn,
|
|
`cd "${siteDir}" && rm -rf index && mkdir index && tar -xzf frontend.tar.gz -C index && rm frontend.tar.gz`,
|
|
);
|
|
if (code !== 0) throw new Error(`解压失败: ${stderr}`);
|
|
printSuccess(`前端文件已部署到 ${indexDir}`);
|
|
}
|