refactor: share build copy script helpers

This commit is contained in:
Peter Steinberger
2026-03-26 23:05:23 +00:00
parent 0106b0488a
commit abf95c5f99
3 changed files with 50 additions and 41 deletions

View File

@@ -5,27 +5,21 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { ensureDirectory, logVerboseCopy, resolveBuildCopyContext } from "./lib/copy-assets.ts";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, "..");
const verbose = process.env.OPENCLAW_BUILD_VERBOSE === "1";
const context = resolveBuildCopyContext(import.meta.url);
const srcDir = path.join(projectRoot, "src", "auto-reply", "reply", "export-html");
const distDir = path.join(projectRoot, "dist", "export-html");
const srcDir = path.join(context.projectRoot, "src", "auto-reply", "reply", "export-html");
const distDir = path.join(context.projectRoot, "dist", "export-html");
function copyExportHtmlTemplates() {
if (!fs.existsSync(srcDir)) {
console.warn("[copy-export-html-templates] Source directory not found:", srcDir);
console.warn(`${context.prefix} Source directory not found:`, srcDir);
return;
}
// Create dist directory
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir, { recursive: true });
}
ensureDirectory(distDir);
// Copy main template files
const templateFiles = ["template.html", "template.css", "template.js"];
let copiedCount = 0;
for (const file of templateFiles) {
@@ -34,19 +28,14 @@ function copyExportHtmlTemplates() {
if (fs.existsSync(srcFile)) {
fs.copyFileSync(srcFile, distFile);
copiedCount += 1;
if (verbose) {
console.log(`[copy-export-html-templates] Copied ${file}`);
}
logVerboseCopy(context, `Copied ${file}`);
}
}
// Copy vendor files
const srcVendor = path.join(srcDir, "vendor");
const distVendor = path.join(distDir, "vendor");
if (fs.existsSync(srcVendor)) {
if (!fs.existsSync(distVendor)) {
fs.mkdirSync(distVendor, { recursive: true });
}
ensureDirectory(distVendor);
const vendorFiles = fs.readdirSync(srcVendor);
for (const file of vendorFiles) {
const srcFile = path.join(srcVendor, file);
@@ -54,14 +43,12 @@ function copyExportHtmlTemplates() {
if (fs.statSync(srcFile).isFile()) {
fs.copyFileSync(srcFile, distFile);
copiedCount += 1;
if (verbose) {
console.log(`[copy-export-html-templates] Copied vendor/${file}`);
}
logVerboseCopy(context, `Copied vendor/${file}`);
}
}
}
console.log(`[copy-export-html-templates] Copied ${copiedCount} export-html assets.`);
console.log(`${context.prefix} Copied ${copiedCount} export-html assets.`);
}
copyExportHtmlTemplates();

View File

@@ -5,24 +5,20 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { ensureDirectory, logVerboseCopy, resolveBuildCopyContext } from "./lib/copy-assets.ts";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, "..");
const verbose = process.env.OPENCLAW_BUILD_VERBOSE === "1";
const context = resolveBuildCopyContext(import.meta.url);
const srcBundled = path.join(projectRoot, "src", "hooks", "bundled");
const distBundled = path.join(projectRoot, "dist", "bundled");
const srcBundled = path.join(context.projectRoot, "src", "hooks", "bundled");
const distBundled = path.join(context.projectRoot, "dist", "bundled");
function copyHookMetadata() {
if (!fs.existsSync(srcBundled)) {
console.warn("[copy-hook-metadata] Source directory not found:", srcBundled);
console.warn(`${context.prefix} Source directory not found:`, srcBundled);
return;
}
if (!fs.existsSync(distBundled)) {
fs.mkdirSync(distBundled, { recursive: true });
}
ensureDirectory(distBundled);
const entries = fs.readdirSync(srcBundled, { withFileTypes: true });
let copiedCount = 0;
@@ -39,22 +35,18 @@ function copyHookMetadata() {
const distHookMd = path.join(distHookDir, "HOOK.md");
if (!fs.existsSync(srcHookMd)) {
console.warn(`[copy-hook-metadata] No HOOK.md found for ${hookName}`);
console.warn(`${context.prefix} No HOOK.md found for ${hookName}`);
continue;
}
if (!fs.existsSync(distHookDir)) {
fs.mkdirSync(distHookDir, { recursive: true });
}
ensureDirectory(distHookDir);
fs.copyFileSync(srcHookMd, distHookMd);
copiedCount += 1;
if (verbose) {
console.log(`[copy-hook-metadata] Copied ${hookName}/HOOK.md`);
}
logVerboseCopy(context, `Copied ${hookName}/HOOK.md`);
}
console.log(`[copy-hook-metadata] Copied ${copiedCount} hook metadata files.`);
console.log(`${context.prefix} Copied ${copiedCount} hook metadata files.`);
}
copyHookMetadata();

View File

@@ -0,0 +1,30 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
export type BuildCopyContext = {
prefix: string;
projectRoot: string;
verbose: boolean;
};
export function resolveBuildCopyContext(importMetaUrl: string): BuildCopyContext {
const filePath = fileURLToPath(importMetaUrl);
return {
prefix: `[${path.basename(filePath, path.extname(filePath))}]`,
projectRoot: path.resolve(path.dirname(filePath), ".."),
verbose: process.env.OPENCLAW_BUILD_VERBOSE === "1",
};
}
export function ensureDirectory(dirPath: string): void {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
export function logVerboseCopy(context: BuildCopyContext, message: string): void {
if (context.verbose) {
console.log(`${context.prefix} ${message}`);
}
}