openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.95 kB
JavaScript
import{a as extractErrorCode}from"./error-coercion-DlKnt6w-.mjs";import{t as asNonArrayRecord}from"./record-coerce-BTbqdJ3-.mjs";import{m as readRegularFile,n as appendRegularFile}from"./fs-safe-Bxzu1j4h.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{r as decodeUtf8Input}from"./utf8-input-CiFSnxL0.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./security-runtime-Y28JTB1K.mjs";import"./runtime-doctor-migrations-DjW1C-6Q.mjs";import"./error-runtime-DW-sj0iq.mjs";import{d as markMigrationItemSkipped,t as MIGRATION_REASON_MISSING_SOURCE_OR_TARGET,u as markMigrationItemError}from"./migration-B8ObiJfa.mjs";import path from"node:path";import fs from"node:fs/promises";import os from"node:os";import{parse as parse$1}from"dotenv";import{parse as parse$2}from"yaml";
//!extensions/migrate-hermes/helpers.ts
const HOME_SHORTHAND_RE=/^~(?=$|[\\/])/u;const UNSAFE_NAME_CHARS_RE=/[^a-z0-9._-]+/g;const EDGE_DASHES_RE=/^-+|-+$/g;function resolveHomePath(e){const t=e.trim();return t?path.resolve(t.replace(HOME_SHORTHAND_RE,()=>os.homedir())):t}async function isDirectory(e){return(await fs.stat(e).catch(e=>{const t=extractErrorCode(e);if(t===`ENOENT`||t===`ENOTDIR`)return;throw e}))?.isDirectory()===true}function sanitizeName(e){return e.trim().toLowerCase().replaceAll(UNSAFE_NAME_CHARS_RE,`-`).replaceAll(EDGE_DASHES_RE,``)}async function readText(e){return e?await fs.realpath(e).then(e=>readRegularFile({filePath:e})).then(({buffer:e})=>e.toString(`utf8`)).catch(()=>void 0):void 0}async function readUtf8Text(e){if(!e)return;try{const{buffer:t}=await readRegularFile({filePath:await fs.realpath(e)});return decodeUtf8Input(t,`Source file`)}catch(t){throw new Error(`Failed reading text file ${e}: ${formatErrorMessage(t)}. Use an ordinary readable UTF-8 file and retry migration.`,{cause:t})}}async function readJsonObject(e){const t=await readUtf8Text(e);try{return t?asNonArrayRecord(JSON.parse(t)):{}}catch{return{}}}function parseEnv(e){return e?parse$1(e):{}}function parseHermesConfig(e){return e?asNonArrayRecord(parse$2(e)):{}}async function appendItem(e){if(!e.source||!e.target)return markMigrationItemError(e,MIGRATION_REASON_MISSING_SOURCE_OR_TARGET);try{const t=await readUtf8Text(e.source);const n=`\n\n<!-- Imported from Hermes: ${path.basename(e.source)} -->\n\n`;const r=t?.trimEnd();if(!r)return markMigrationItemSkipped(e,`source file is empty`);const i=`${n}${r}\n`;if((await readText(e.target)??``).includes(i))return markMigrationItemSkipped(e,`already imported from Hermes`);await fs.mkdir(path.dirname(e.target),{recursive:true});await appendRegularFile({filePath:e.target,content:i,rejectSymlinkParents:true});return{...e,status:`migrated`}}catch(t){return markMigrationItemError(e,t instanceof Error?t.message:String(t))}}export{readJsonObject as a,resolveHomePath as c,parseHermesConfig as i,sanitizeName as l,isDirectory as n,readText as o,parseEnv as r,readUtf8Text as s,appendItem as t};