Files
xiaowang ac84f8f206
环境分支晋级检查 / environment-promotion-source (pull_request_target) Successful in 1s
重整三环境发布与回退工作流
2026-10-15 11:26:28 +08:00

63 lines
2.1 KiB
JavaScript

import { mkdir, readFile, readdir, rename, writeFile } from 'node:fs/promises';
import { dirname, join, resolve } from 'node:path';
export const storeRoot = process.env.RELEASE_STORE_ROOT || '/salon-release';
export function envName(value = process.env.NACOS_ENVIRONMENT) {
if (!['dev', 'sit', 'prod'].includes(value)) throw new Error(`不支持的环境:${value}`);
return value;
}
export function environmentRoot(environment = envName()) {
return join(storeRoot, environment);
}
export function snapshotRoot(environment = envName(), snapshotId) {
return join(storeRoot, 'nacos-snapshots', environment, snapshotId);
}
export function recordsRoot(environment = envName()) {
return join(storeRoot, 'records', environment);
}
export async function atomicWrite(path, content) {
await mkdir(dirname(path), { recursive: true });
const temp = `${path}.tmp-${process.pid}-${Date.now()}`;
await writeFile(temp, content, 'utf8');
await rename(temp, path);
}
export async function readJson(path) {
return JSON.parse(await readFile(path, 'utf8'));
}
export async function writeJson(path, value) {
await atomicWrite(path, `${JSON.stringify(value, null, 2)}\n`);
}
export async function latestSuccess(environment = envName(), kind = 'nacos') {
const path = join(recordsRoot(environment), `latest-success-${kind}.json`);
return readJson(path);
}
export async function releaseRecord(environment, kind, releaseId) {
return readJson(join(recordsRoot(environment), kind, `${releaseId}.json`));
}
export async function listYamlFiles(root) {
const result = [];
for (const entry of await readdir(root, { withFileTypes: true })) {
const path = join(root, entry.name);
if (entry.isDirectory()) result.push(...await listYamlFiles(path));
else if (entry.isFile() && entry.name.endsWith('.yaml')) result.push(path);
}
return result.sort();
}
export function safeRelative(root, path) {
const value = resolve(path);
const base = resolve(root);
if (!value.startsWith(`${base}/`) && !value.startsWith(`${base}\\`)) throw new Error(`路径越界:${path}`);
return value.slice(base.length + 1).replaceAll('\\', '/');
}