UNPKG

yao-node-client

Version:

A node client for yao application development

416 lines 13.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FS = void 0; const request_1 = require("./request"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const mime_1 = __importDefault(require("mime")); /** * 使用 FS 对象实现文件操作。 Yao 提供 System, DSL, Script 三个空间, * System 用于应用数据操作, * DSL 用于DSL文件操作, * Script 用于脚本文件操作; * DSL 和 Script 只能用于 stuido 脚本。 * * let fs = new FS("system"); * * let data = fs.ReadFile("/f1.txt"); // /data/app/data/f1.txt */ class FS { // [key: string]: any; space; isLocal; basePath; /** * data /data/app/data 应用数据 * app /data/app 应用目录 * system /data/app/data 应用数据 * dsl /data/app 除 scripts 外的所有目录(仅 Studio 脚本可用) * script /data/app/scirpts 脚本目录(仅 Studio 脚本可用) * app 应用目录 * @param space */ constructor(space) { if (!space) { throw Error(`文件操作需要指定一个参数:"data" | "app"`); } this.space = space; let yao_app_root = process.env.YAO_APP_ROOT; if (!yao_app_root) { yao_app_root = "./"; } this.isLocal = false; switch (this.space) { case "data": case "system": yao_app_root = path_1.default.resolve(path_1.default.join(yao_app_root, "data", path_1.default.sep)); if (fs_1.default.existsSync(yao_app_root)) { this.basePath = yao_app_root; this.isLocal = true; } break; case "script": yao_app_root = path_1.default.resolve(path_1.default.join(yao_app_root, "scripts", path_1.default.sep)); if (fs_1.default.existsSync(yao_app_root)) { this.basePath = yao_app_root; this.isLocal = true; } this.basePath = path_1.default.join(yao_app_root, "scripts", path_1.default.sep); break; case "app": case "dsl": yao_app_root = path_1.default.resolve(yao_app_root); if (fs_1.default.existsSync(yao_app_root)) { this.basePath = path_1.default.join(yao_app_root, path_1.default.sep); this.isLocal = true; } break; default: break; } } ReadFile(src) { if (this.isLocal) { let fpath = path_1.default.join(this.basePath, src); return fs_1.default.readFileSync(fpath, "utf8"); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "ReadFile", args: [src], }); } ReadFileBuffer(src) { if (this.isLocal) { return fs_1.default.readFileSync(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "ReadFileBuffer", args: [src], }); } WriteFileBase64(src, str) { return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "WriteFileBase64", args: [src, str], }); } WriteFile(src, str, mode) { if (this.isLocal) { const fname = path_1.default.join(this.basePath, src); makeParentFolder(fname); return fs_1.default.writeFileSync(fname, str, { mode }); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "WriteFile", args: [src, str, mode], }); } WriteFileBuffer(src, buffer, mode) { if (this.isLocal) { const fname = path_1.default.join(this.basePath, src); makeParentFolder(fname); return fs_1.default.writeFileSync(fname, buffer, { mode }); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "WriteFileBuffer", args: [src, buffer, mode], }); } ReadDir(src, recursive) { if (this.isLocal) { if (recursive) { return readDirAll(this.basePath, path_1.default.join(this.basePath, src)); } else { return readDir(this.basePath, path_1.default.join(this.basePath, src)); } } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "ReadDir", args: [src, recursive], }); } Mkdir(src, mode) { if (this.isLocal) { return fs_1.default.mkdirSync(path_1.default.join(this.basePath, src), { mode }); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Mkdir", args: [src, mode], }); } /** * 根据目录,创建必须的目录 * @param src 目录 * @param mode 目录权限 * @returns */ MkdirAll(src, mode) { if (this.isLocal) { return fs_1.default.mkdirSync(path_1.default.join(this.basePath, src), { recursive: true, mode, }); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "MkdirAll", args: [src, mode], }); } /** * 创建一个临时目录,该目录具有唯一的、随机生成的名称,并且只能由当前用户访问 * @param src 目录 * @param pattern 指定临时目录的前缀 * @returns 创建的临时目录的路径 */ MkdirTemp(src, pattern) { if (this.isLocal) { fs_1.default.mkdirSync(path_1.default.join(this.basePath, src), { recursive: true }); let newpath = path_1.default.join(path_1.default.join(this.basePath, src), pattern); return fs_1.default.mkdtempSync(newpath); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "MkdirTemp", args: [src, pattern], }); } Exists(src) { if (this.isLocal) { return fs_1.default.existsSync(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Exists", args: [src], }); } IsDir(src) { if (this.isLocal) { const stat = fs_1.default.statSync(path_1.default.join(this.basePath, src)); return stat.isDirectory(); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "IsDir", args: [src], }); } IsFile(src) { if (this.isLocal) { const stat = fs_1.default.statSync(path_1.default.join(this.basePath, src)); return stat.isFile(); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "IsFile", args: [src], }); } Remove(src) { if (this.isLocal) { rmoveLocal(path_1.default.join(this.basePath, src)); return; } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Remove", args: [src], }); } RemoveAll(src) { if (this.isLocal) { rmoveLocal(path_1.default.join(this.basePath, src)); return; } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "RemoveAll", args: [src], }); } Chmod(src, mode) { if (this.isLocal) { return fs_1.default.chmodSync(path_1.default.join(this.basePath, src), mode); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Chmod", args: [src, mode], }); } BaseName(src) { if (this.isLocal) { return path_1.default.basename(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "BaseName", args: [src], }); } DirName(src) { if (this.isLocal) { return path_1.default.dirname(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "DirName", args: [src], }); } ExtName(src) { if (this.isLocal) { return path_1.default.extname(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "ExtName", args: [src], }); } MimeType(src) { if (this.isLocal) { return mime_1.default.getType(path_1.default.join(this.basePath, src)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "MimeType", args: [src], }); } Mode(src) { if (this.isLocal) { const stat = fs_1.default.statSync(path_1.default.join(this.basePath, src)); return stat.mode; } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Mode", args: [src], }); } Size(src) { if (this.isLocal) { const stat = fs_1.default.statSync(path_1.default.join(this.basePath, src)); return stat.size; } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Size", args: [src], }); } ModTime(src) { if (this.isLocal) { const stat = fs_1.default.statSync(path_1.default.join(this.basePath, src)); return stat.mtime; } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "ModTime", args: [src], }); } Copy(src, target) { if (this.isLocal) { return localCopy(path_1.default.join(this.basePath, src), path_1.default.join(this.basePath, target)); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Copy", args: [src, target], }); } Merge(fileList, str) { return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Merge", args: [fileList, str], }); } Move(src, target) { if (this.isLocal) { const old = path_1.default.join(this.basePath, src); const newPath = path_1.default.join(this.basePath, target); return fs_1.default.renameSync(old, newPath); } return (0, request_1.RemoteRequest)({ type: "FileSystem", method: "Move", args: [src, target], }); } } exports.FS = FS; function readDir(base, dir) { let files = []; fs_1.default.readdirSync(dir).forEach((file) => { const src = `${dir}${path_1.default.sep}${file}`; files.push(`/${src.replace(base, "")}`); }); return files; } /** * 读取目录下所有的文件列表,包含子目录 * @param dir 目录 * @returns 目录所有的文件列表,包含子目录 */ function readDirAll(base, dir) { const files = []; for (const fileName of fs_1.default.readdirSync(dir)) { const filePath = path_1.default.join(dir, fileName); files.push(`/${filePath.replace(base, "")}`); if (fs_1.default.statSync(filePath).isDirectory()) { files.push(...readDirAll(base, filePath)); } } return files; } function rmoveLocal(srcPath) { const stat = fs_1.default.statSync(srcPath); if (stat.isDirectory()) { const files = fs_1.default.readdirSync(srcPath); files.forEach((file) => { fs_1.default.unlinkSync(`${path_1.default}${path_1.default.sep}${file}`); }); } else { fs_1.default.unlinkSync(srcPath); } } function localCopy(src, target) { const stat = fs_1.default.statSync(src); if (!stat.isDirectory()) { makeParentFolder(target); fs_1.default.copyFileSync(src, target); } else { let files = fs_1.default.readdirSync(src); files.forEach((file) => { // Source file const srcFile = `${src}${path_1.default.sep}${file}`; // Destination file const destFile = `${target}${path_1.default.sep}${file}`; localCopy(srcFile, destFile); }); } } function makeParentFolder(target) { const parentFolder = path_1.default.dirname(target); if (!fs_1.default.existsSync(parentFolder)) { fs_1.default.mkdirSync(parentFolder, { recursive: true }); } } //# sourceMappingURL=filesystem.js.map