Files
ecom-dmb/deploy/src/platform.ts
T
2026-09-16 22:43:21 +08:00

50 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { execSync } from 'child_process';
import { cpSync, existsSync, readdirSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
/** 本地临时目录(跨平台,替代 /tmp) */
export function getLocalTempPath(name: string): string {
return join(tmpdir(), name);
}
/** 解析 Maven 命令:Windows 用 mvnw.cmd,Unix 用 mvnw */
export function resolveMavenCommand(projectDir: string): string {
if (process.platform === 'win32') {
const mvnwCmd = join(projectDir, 'mvnw.cmd');
if (existsSync(mvnwCmd)) return `"${mvnwCmd}"`;
} else {
const mvnw = join(projectDir, 'mvnw');
if (existsSync(mvnw)) return `"${mvnw}"`;
}
return 'mvn';
}
/** 在 target 目录查找主 JAR(排除 *-original.jar) */
export function findMainJar(targetDir: string): string {
const jars = readdirSync(targetDir).filter(
(f) => f.endsWith('.jar') && !f.endsWith('-original.jar'),
);
if (jars.length === 0) throw new Error(`未找到 JAR 文件: ${targetDir}`);
return join(targetDir, jars[0]);
}
/** 将目录打包为 tar.gz(Windows 10+ / Linux / macOS 均内置 tar) */
export function createTarGz(sourceDir: string, destPath: string): void {
try {
execSync(`tar -czf "${destPath}" -C "${sourceDir}" .`, { stdio: 'pipe' });
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : String(e);
throw new Error(`打包失败 (${sourceDir}): ${msg}`);
}
}
/** 递归复制目录/文件到临时构建目录 */
export function copyBuildArtifacts(src: string, dest: string, opts?: { optional?: boolean }): void {
if (!existsSync(src)) {
if (opts?.optional) return;
throw new Error(`源路径不存在: ${src}`);
}
cpSync(src, dest, { recursive: true });
}