chore: sync local workspace changes
This commit is contained in:
@@ -2,6 +2,7 @@ import fs from 'node:fs';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { env } from '../config/env.js';
|
||||
|
||||
export type WorkServerBuildInfo = {
|
||||
version: string;
|
||||
@@ -16,13 +17,55 @@ export type WorkServerSourceChangeInfo = {
|
||||
|
||||
const MODULE_DIR_PATH = path.dirname(fileURLToPath(import.meta.url));
|
||||
const WORK_SERVER_ROOT_PATH = path.resolve(MODULE_DIR_PATH, '..', '..');
|
||||
const BUILD_INFO_FILE_PATH = path.join(WORK_SERVER_ROOT_PATH, 'dist', 'build-info.json');
|
||||
const SOURCE_TARGET_PATHS = [
|
||||
path.join(WORK_SERVER_ROOT_PATH, 'src'),
|
||||
path.join(WORK_SERVER_ROOT_PATH, 'scripts'),
|
||||
path.join(WORK_SERVER_ROOT_PATH, 'package.json'),
|
||||
path.join(WORK_SERVER_ROOT_PATH, 'tsconfig.json'),
|
||||
] as const;
|
||||
const SOURCE_TARGET_PATH_NAMES = ['src', 'scripts', 'package.json', 'tsconfig.json'] as const;
|
||||
|
||||
function normalizeRootPath(value: string | null | undefined) {
|
||||
const normalized = String(value ?? '').trim();
|
||||
return normalized ? path.resolve(normalized) : null;
|
||||
}
|
||||
|
||||
function resolveSourceTargetRoots() {
|
||||
const roots = [WORK_SERVER_ROOT_PATH];
|
||||
const mainProjectRoot = normalizeRootPath(env.SERVER_COMMAND_MAIN_PROJECT_ROOT || env.SERVER_COMMAND_PROJECT_ROOT);
|
||||
|
||||
if (mainProjectRoot) {
|
||||
const mirroredWorkServerRoot = path.join(mainProjectRoot, 'etc', 'servers', 'work-server');
|
||||
|
||||
if (!roots.includes(mirroredWorkServerRoot)) {
|
||||
roots.push(mirroredWorkServerRoot);
|
||||
}
|
||||
}
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
function resolveBuildInfoDirectoryPath(rootPath: string, configuredDistDir: string) {
|
||||
return path.resolve(rootPath, configuredDistDir);
|
||||
}
|
||||
|
||||
export function resolveWorkServerBuildInfoFilePaths(options?: {
|
||||
workServerRootPath?: string;
|
||||
mainProjectRoot?: string | null;
|
||||
configuredDistDir?: string | null;
|
||||
}) {
|
||||
const workServerRootPath = path.resolve(options?.workServerRootPath ?? WORK_SERVER_ROOT_PATH);
|
||||
const configuredDistDir = String(options?.configuredDistDir ?? env.WORK_SERVER_DIST_DIR ?? 'dist').trim() || 'dist';
|
||||
const mainProjectRoot = normalizeRootPath(
|
||||
options?.mainProjectRoot ?? env.SERVER_COMMAND_MAIN_PROJECT_ROOT ?? env.SERVER_COMMAND_PROJECT_ROOT,
|
||||
);
|
||||
const candidates = [
|
||||
path.join(resolveBuildInfoDirectoryPath(workServerRootPath, configuredDistDir), 'build-info.json'),
|
||||
path.join(workServerRootPath, 'dist', 'build-info.json'),
|
||||
];
|
||||
|
||||
if (mainProjectRoot) {
|
||||
const mirroredWorkServerRoot = path.join(mainProjectRoot, 'etc', 'servers', 'work-server');
|
||||
candidates.push(path.join(resolveBuildInfoDirectoryPath(mirroredWorkServerRoot, configuredDistDir), 'build-info.json'));
|
||||
candidates.push(path.join(mirroredWorkServerRoot, 'dist', 'build-info.json'));
|
||||
}
|
||||
|
||||
return candidates.filter((candidate, index, array) => array.indexOf(candidate) === index);
|
||||
}
|
||||
|
||||
function normalizeBuildInfo(value: unknown): WorkServerBuildInfo | null {
|
||||
if (!value || typeof value !== 'object') {
|
||||
@@ -65,27 +108,43 @@ function readBuildInfoFromDiskSync(filePath: string) {
|
||||
}
|
||||
|
||||
export async function readLatestWorkServerBuildInfo() {
|
||||
try {
|
||||
return normalizeBuildInfo(JSON.parse(await readFile(BUILD_INFO_FILE_PATH, 'utf8')));
|
||||
} catch {
|
||||
return null;
|
||||
let latestBuildInfo: WorkServerBuildInfo | null = null;
|
||||
|
||||
for (const candidatePath of resolveWorkServerBuildInfoFilePaths()) {
|
||||
try {
|
||||
const buildInfo = normalizeBuildInfo(JSON.parse(await readFile(candidatePath, 'utf8')));
|
||||
|
||||
if (!buildInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!latestBuildInfo || buildInfo.builtAt > latestBuildInfo.builtAt) {
|
||||
latestBuildInfo = buildInfo;
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return latestBuildInfo;
|
||||
}
|
||||
|
||||
const runtimeWorkServerBuildInfo = readBuildInfoFromDiskSync(BUILD_INFO_FILE_PATH);
|
||||
const runtimeWorkServerBuildInfo = readBuildInfoFromDiskSync(
|
||||
path.join(resolveBuildInfoDirectoryPath(WORK_SERVER_ROOT_PATH, env.WORK_SERVER_DIST_DIR), 'build-info.json'),
|
||||
);
|
||||
|
||||
export function getRuntimeWorkServerBuildInfo() {
|
||||
return runtimeWorkServerBuildInfo;
|
||||
}
|
||||
|
||||
async function findLatestSourceChangeInPath(targetPath: string): Promise<WorkServerSourceChangeInfo | null> {
|
||||
async function findLatestSourceChangeInPath(rootPath: string, targetPath: string): Promise<WorkServerSourceChangeInfo | null> {
|
||||
try {
|
||||
const stats = await fs.promises.stat(targetPath);
|
||||
|
||||
if (stats.isFile()) {
|
||||
return {
|
||||
changedAt: stats.mtime.toISOString(),
|
||||
path: path.relative(WORK_SERVER_ROOT_PATH, targetPath) || path.basename(targetPath),
|
||||
path: path.relative(rootPath, targetPath) || path.basename(targetPath),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -102,7 +161,7 @@ async function findLatestSourceChangeInPath(targetPath: string): Promise<WorkSer
|
||||
}
|
||||
|
||||
const childPath = path.join(targetPath, entry.name);
|
||||
const childLatest = await findLatestSourceChangeInPath(childPath);
|
||||
const childLatest = await findLatestSourceChangeInPath(rootPath, childPath);
|
||||
|
||||
if (!childLatest) {
|
||||
continue;
|
||||
@@ -122,15 +181,17 @@ async function findLatestSourceChangeInPath(targetPath: string): Promise<WorkSer
|
||||
export async function readLatestWorkServerSourceChange() {
|
||||
let latest: WorkServerSourceChangeInfo | null = null;
|
||||
|
||||
for (const targetPath of SOURCE_TARGET_PATHS) {
|
||||
const candidate = await findLatestSourceChangeInPath(targetPath);
|
||||
for (const rootPath of resolveSourceTargetRoots()) {
|
||||
for (const targetName of SOURCE_TARGET_PATH_NAMES) {
|
||||
const candidate = await findLatestSourceChangeInPath(rootPath, path.join(rootPath, targetName));
|
||||
|
||||
if (!candidate) {
|
||||
continue;
|
||||
}
|
||||
if (!candidate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!latest || candidate.changedAt > latest.changedAt) {
|
||||
latest = candidate;
|
||||
if (!latest || candidate.changedAt > latest.changedAt) {
|
||||
latest = candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user