UNPKG

@agentkai/node

Version:

AgentKai的Node.js环境特定实现

142 lines (141 loc) 3.28 kB
import { PlatformType, } from '@agentkai/core'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; /** * Node.js环境的文件系统实现 */ export class NodeFileSystem { async readFile(filePath) { return fs.promises.readFile(filePath, { encoding: 'utf-8' }); } async writeFile(filePath, data) { await fs.promises.writeFile(filePath, data, { encoding: 'utf-8' }); } async exists(filePath) { try { await fs.promises.access(filePath); return true; } catch { return false; } } async mkdir(filePath, options) { await fs.promises.mkdir(filePath, options); } async readdir(filePath) { return fs.promises.readdir(filePath); } async unlink(filePath) { await fs.promises.unlink(filePath); } async stat(filePath) { return fs.promises.stat(filePath); } } /** * Node.js环境的环境变量提供者实现 */ export class NodeEnvProvider { get(key, defaultValue) { return process.env[key] || defaultValue; } set(key, value) { process.env[key] = value; } getAll() { return { ...process.env }; } } /** * Node.js环境的路径工具实现 */ export class NodePathUtils { home() { return os.homedir(); } join(...paths) { return path.join(...paths); } resolve(...paths) { return path.resolve(...paths); } dirname(pathStr) { return path.dirname(pathStr); } basename(pathStr) { return path.basename(pathStr); } extname(pathStr) { return path.extname(pathStr); } } /** * Node.js环境的平台信息实现 */ export class NodePlatformInfo { homeDir() { return os.homedir(); } platform() { return process.platform; } isNode() { return true; } isBrowser() { return false; } tmpdir() { return os.tmpdir(); } cwd() { return process.cwd(); } } /** * Node.js环境的平台服务实现 */ export class NodePlatformServices { constructor() { Object.defineProperty(this, "type", { enumerable: true, configurable: true, writable: true, value: PlatformType.NODE }); Object.defineProperty(this, "fs", { enumerable: true, configurable: true, writable: true, value: new NodeFileSystem() }); Object.defineProperty(this, "env", { enumerable: true, configurable: true, writable: true, value: new NodeEnvProvider() }); Object.defineProperty(this, "path", { enumerable: true, configurable: true, writable: true, value: new NodePathUtils() }); Object.defineProperty(this, "platformInfo", { enumerable: true, configurable: true, writable: true, value: new NodePlatformInfo() }); } } /** * Node.js环境的平台服务工厂 */ export class NodePlatformServiceFactory { create() { return new NodePlatformServices(); } }