This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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('\\', '/');
|
||||
}
|
||||
Reference in New Issue
Block a user