UNPKG

ucbuilder

Version:

:Shree Ganeshay Namah: new way app design

197 lines 8.89 kB
import { IpcRendererHelper } from './ipc/IpcRendererHelper.js'; import { ucUtil } from '../global/ucUtil.js'; export class nodeFn { static renderer = IpcRendererHelper.Group('ucbuilder/src/main/nodeFn'); static fullfill = undefined; static onReady(callback) { this.renderer.loaded(callback); } static crypto = { toString: (size, encoding) => { return this.renderer.sendSync('crypto.toString', [size, encoding]); }, randomBytes: (size) => { return this.renderer.sendSync('crypto.randomBytes', [size]); }, convert: (data, from = "utf-8", to = "hex") => { return this.renderer.sendSync('crypto.convert', [data, from, to]); }, encrypt: (data) => { return this.crypto.convert(data, 'utf8', 'hex'); }, decrypt: (data) => { return this.crypto.convert(data, 'hex', 'utf8'); } }; static url = { fileURLToPath: (path) => { if (!path.startsWith('file:')) return path; return nodeFn.fullfill.url.fileURLToPath(path); //return renderer.sendSync('url.fileURLToPath', [path]) as string; }, pathToFileURL: (path) => { if (path.startsWith('file:')) return path; return nodeFn.fullfill.url.pathToFileURL(path); //return renderer.sendSync('url.pathToFileURL', [path]) as string; }, }; /* static resolver = { resolve: (importPath: string, importer?: string): string => { return this.renderer.sendSync('resolver.resolve', [importPath, importer]); }, resolveOut: (importPath: string, importer?: string): string => { return this.renderer.sendSync('resolver.resolveOut', [importPath, importer]); } }*/ static path = { startsWith: (thisPath, startWithThisPath) => { const base = nodeFn.path.resolve(startWithThisPath).toLowerCase(); const target = nodeFn.path.resolve(thisPath).toLowerCase(); const relative = nodeFn.path.relative(base, target); return relative && !relative.startsWith("..") && !nodeFn.path.isAbsolute(relative); }, dirname: (path) => { return this.fullfill.path.dirname(path); //return renderer.sendSync('path.dirname', [path]); }, isAbsolute: (path) => { return this.fullfill.path.isAbsolute(path); //return renderer.sendSync('path.dirname', [path]); }, basename: (path, suffix) => { return nodeFn.fullfill.path.basename(path, suffix); //return renderer.sendSync('path.basename', [{ path: path, suffix: suffix } as I_PathBaseName]); }, relative: (from, to) => { return this.fullfill.path.relative(from, to); //return renderer.sendSync('path.relative', [{ from: from, to: to } as I_PathRelative]); }, resolve: (...paths) => { return nodeFn.fullfill.path.resolve(...paths); //return renderer.sendSync('path.resolve', [paths]); }, resolveFilePath: (fromFilePath, toFilePath) => { let ius = this.fullfill.path.dirname(fromFilePath.startsWith('file:') ? this.fullfill.url.fileURLToPath(fromFilePath) : fromFilePath); let fspath = ucUtil.toFilePath(this.fullfill.path.resolve(ius, toFilePath)); return fspath; //return renderer.sendSync('path.resolveFilePath', [basePath, path]); }, relativeFilePath: (fromFilePath, path) => { path = ucUtil.devEsc(path); let ius = this.fullfill.path.dirname(fromFilePath.startsWith('file:') ? this.fullfill.url.fileURLToPath(fromFilePath) : fromFilePath); let fspath = ucUtil.toFilePath(this.fullfill.path.relative(ius, path)); return fspath; //return renderer.sendSync('path.relativeFilePath', [fromFilePath, path]); }, getPathOnly: (pth) => { return pth.startsWith('file:') ? this.fullfill.url.fileURLToPath(pth) : pth; }, getUrlOnly: (pth) => { return pth.startsWith('file:') ? pth : this.fullfill.url.pathToFileURL(pth); }, subtractPath: (basePath, targetPath) => { const absBase = this.fullfill.path.resolve(basePath); const absTarget = this.fullfill.path.resolve(targetPath); // Get relative path from base to target const relative = this.fullfill.path.relative(absBase, absTarget); return relative; //return renderer.sendSync('path.subtractPath', [basePath, targetPath]); }, isSamePath: (path1, path2) => { const absA = nodeFn.path.resolve(path1); const absB = nodeFn.path.resolve(path2); return (nodeFn.path.normalize(absA) === nodeFn.path.normalize(absB)); //return renderer.sendSync('path.isSamePath', [path1, path2]); }, join: (...paths) => { return nodeFn.fullfill.path.join(...paths); //return renderer.sendSync('path.join', [paths]); }, normalize: (path) => { return nodeFn.fullfill.path.normalize(path); //return renderer.sendSync('path.normalize', [path]); }, // isAbsolute: (path: string): boolean => { // return this.renderer.sendSync('path.isAbsolute', [path]); // }, intersectPath: (path1, path2) => { return this.renderer.sendSync('path.intersectPath', [path1, path2]); }, intersectAndReplacePath: (basePath, targetPath) => { return this.renderer.sendSync('path.intersectAndReplacePath', [basePath, targetPath]); }, // ProjectResolve: (path: string, importMetaUrl: string) => { // return ProjectManage.resolve(path as any, importMetaUrl); // } }; static readFileSyncStorage = new Map(); static readFileSyncStorageCounter = 0; static fs = { openSync: (path, flags, mode) => { return this.renderer.sendSync('fs.openSync', [path, flags, mode]); }, existsSync: (path) => { return this.renderer.sendSync('fs.existsSync', [path]); }, rename: (from, to) => { return this.renderer.sendSync('fs.rename', [from, to]); }, rmSync: (path, options) => { return this.renderer.sendSync('fs.rmSync', [path, options]); }, isDirectory: (path, options) => { return this.renderer.sendSync('fs.statSync.isDirectory', [path, options]); }, mkdirSync: (path, options) => { return this.renderer.sendSync('fs.mkdirSync', [path, options]); }, copyFileSync: (fromPath, toPath, option) => { return this.renderer.sendSync('fs.copyFileSync', [fromPath, toPath, option]); }, readFile: (path, encode = 'utf-8') => { return this.renderer.Invoke('fs.readFile', [{ path: path, encode: encode, doCache: false, }]); }, readFileSync: (path, encode = 'utf-8', doCache = false) => { let _finalpath = nodeFn.path.normalize(path); if (doCache) { let rtrn = this.readFileSyncStorage.get(_finalpath); if (rtrn != undefined) return rtrn; else { //console.log('cache..'+(this.readFileSyncStorageCounter++)); rtrn = this.renderer.sendSync('fs.readFileSync', [{ path: _finalpath, encode: encode, doCache: doCache, }]); if (rtrn != undefined) this.readFileSyncStorage.set(_finalpath, rtrn); return rtrn; } } else { //console.log('no cache..'+(this.readFileSyncStorageCounter++)); return this.renderer.sendSync('fs.readFileSync', [{ path: _finalpath, encode: encode, doCache: doCache, }]); } }, readdirSync: (path, encode = 'binary') => { return this.renderer.sendSync('fs.readdirSync', [path, encode]); }, readdirSyncDirent: (path, recursive) => { return this.renderer.sendSync('fs.readdirSyncDirent', [path, recursive]); }, writeFileSync: (path, data, encode = 'utf-8') => { return this.renderer.sendSync('fs.writeFileSync', [{ path: path, data: data, encode: encode }]); } }; } //# sourceMappingURL=nodeFn.js.map