From e58542aad5a908125ed406340320e58c71458a3c Mon Sep 17 00:00:00 2001 From: filip mertens Date: Mon, 27 Sep 2021 18:18:47 +0200 Subject: [PATCH] extend fn --- apps/remix-ide/src/app/files/dgitProvider.js | 16 +++---- apps/remix-ide/src/app/files/fileProvider.js | 50 +++++++++----------- apps/remix-ide/src/assets/js/init.js | 38 +++++++++++++-- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/apps/remix-ide/src/app/files/dgitProvider.js b/apps/remix-ide/src/app/files/dgitProvider.js index 40c9ccf96f..ec03632f3a 100644 --- a/apps/remix-ide/src/app/files/dgitProvider.js +++ b/apps/remix-ide/src/app/files/dgitProvider.js @@ -299,7 +299,7 @@ class DGitProvider extends Plugin { const files = await this.getDirectory('/') this.filesToSend = [] for (const file of files) { - const c = await window.remixFileSystem.readFile(addSlash(`${workspace.absolutePath}/${file}`)) + const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`) const ob = { path: file, content: c @@ -319,10 +319,10 @@ class DGitProvider extends Plugin { this.filesToSend = [] const data = new FormData() - files.forEach(async (file) => { - const c = window.remixFileSystem.readFileSync(addSlash(`${workspace.absolutePath}/${file}`)) + for (const file of files) { + const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`) data.append('file', new Blob([c]), `base/${file}`) - }) + } // get last commit data let ob try { @@ -431,7 +431,7 @@ class DGitProvider extends Plugin { this.createDirectories(`${workspace.absolutePath}/${dir}`) } catch (e) { throw new Error(e) } try { - await window.remixFileSystem.writeFile(addSlash(`${workspace.absolutePath}/${file.path}`, Buffer.concat(content) || new Uint8Array())) + await window.remixFileSystem.writeFile(`${workspace.absolutePath}/${file.path}`, Buffer.concat(content) || new Uint8Array()) } catch (e) { throw new Error(e) } } } catch (e) { @@ -495,7 +495,7 @@ class DGitProvider extends Plugin { const files = await this.getDirectory('/') this.filesToSend = [] for (const file of files) { - const c = await window.remixFileSystem.readFile(addSlash(`${workspace.absolutePath}/${file}`)) + const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`) zip.file(file, c) } await zip.generateAsync({ @@ -515,8 +515,8 @@ class DGitProvider extends Plugin { if (i > 0) previouspath = '/' + directories.slice(0, i).join('/') const finalPath = previouspath + '/' + directories[i] try { - if (!await window.remixFileSystem.exists(addSlash(finalPath))) { - await window.remixFileSystem.mkdir(addSlash(finalPath)) + if (!await window.remixFileSystem.exists(finalPath)) { + await window.remixFileSystem.mkdir(finalPath) } } catch (e) { console.log(e) diff --git a/apps/remix-ide/src/app/files/fileProvider.js b/apps/remix-ide/src/app/files/fileProvider.js index f5f30fbf15..c078b96e5d 100644 --- a/apps/remix-ide/src/app/files/fileProvider.js +++ b/apps/remix-ide/src/app/files/fileProvider.js @@ -80,7 +80,7 @@ class FileProvider { async _exists (path) { path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here var unprefixedpath = this.removePrefix(path) - return path === this.type ? true : await window.remixFileSystem.exists(this.addSlash(unprefixedpath)) + return path === this.type ? true : await window.remixFileSystem.exists(unprefixedpath) } init (cb) { @@ -91,8 +91,7 @@ class FileProvider { path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here var unprefixedpath = this.removePrefix(path) try { - console.log('getting', this.addSlash(unprefixedpath)) - const content = await window.remixFileSystem.readFile(this.addSlash(unprefixedpath), 'utf8') + const content = await window.remixFileSystem.readFile(unprefixedpath, 'utf8') if (cb) cb(null, content) return content } catch (err) { @@ -103,15 +102,15 @@ class FileProvider { async set (path, content, cb) { var unprefixedpath = this.removePrefix(path) - const exists = await window.remixFileSystem.exists(this.addSlash(unprefixedpath)) - if (exists && await window.remixFileSystem.readFile(this.addSlash(unprefixedpath), 'utf8') === content) { + const exists = await window.remixFileSystem.exists(unprefixedpath) + if (exists && await window.remixFileSystem.readFile(unprefixedpath, 'utf8') === content) { if (cb) cb() return null } await this.createDir(path.substr(0, path.lastIndexOf('/'))) try { - await window.remixFileSystem.writeFile(this.addSlash(unprefixedpath), content, 'utf8') + await window.remixFileSystem.writeFile(unprefixedpath, content, 'utf8') } catch (e) { if (cb) cb(e) return false @@ -156,13 +155,13 @@ class FileProvider { async isDirectory (path) { const unprefixedpath = this.removePrefix(path) - return path === this.type ? true : (await window.remixFileSystem.statExtended(this.addSlash(unprefixedpath))).isDirectory() + return path === this.type ? true : (await window.remixFileSystem.stat(unprefixedpath)).isDirectory() } async isFile (path) { path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here path = this.removePrefix(path) - return (await window.remixFileSystem.statExtended(this.addSlash(path))).isFile() + return (await window.remixFileSystem.stat(path)).isFile() } /** @@ -171,26 +170,26 @@ class FileProvider { */ async remove (path) { path = this.removePrefix(path) - if (await window.remixFileSystem.exists(this.addSlash(path))) { - const stat = await window.remixFileSystem.statExtended(this.addSlash(path)) + if (await window.remixFileSystem.exists(path)) { + const stat = await window.remixFileSystem.stat(path) try { if (!stat.isDirectory()) { return (this.removeFile(path)) } else { - const items = await window.remixFileSystem.readdir(this.addSlash(path)) + const items = await window.remixFileSystem.readdir(path) if (items.length !== 0) { for (const item of items) { const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` - if ((await window.remixFileSystem.statExtended(this.addSlash(curPath))).isDirectory()) { // delete folder + if ((await window.remixFileSystem.stat(curPath)).isDirectory()) { // delete folder await this.remove(curPath) } else { // delete file await this.removeFile(curPath) } } - await window.remixFileSystem.rmdir(this.addSlash(path)) + await window.remixFileSystem.rmdir(path) } else { // folder is empty - await window.remixFileSystem.rmdir(this.addSlash(path)) + await window.remixFileSystem.rmdir(path) } this.event.emit('fileRemoved', this._normalizePath(path)) } @@ -213,18 +212,18 @@ class FileProvider { const json = {} path = this.removePrefix(path) - if (await window.remixFileSystem.exists(this.addSlash(path))) { + if (await window.remixFileSystem.exists(path)) { try { - const items = await window.remixFileSystem.readdir(this.addSlash(path)) + const items = await window.remixFileSystem.readdir(path) visitFolder({ path }) if (items.length !== 0) { for (const item of items) { const file = {} const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` - if ((await window.remixFileSystem.statExtended(this.addSlash(curPath))).isDirectory()) { + if ((await window.remixFileSystem.stat(curPath)).isDirectory()) { file.children = await this._copyFolderToJsonInternal(curPath, visitFile, visitFolder) } else { - file.content = await window.remixFileSystem.readFile(this.addSlash(curPath), 'utf8') + file.content = await window.remixFileSystem.readFile(curPath, 'utf8') visitFile({ path: curPath, content: file.content }) } json[curPath] = file @@ -252,8 +251,8 @@ class FileProvider { async removeFile (path) { path = this.removePrefix(path) - if (await window.remixFileSystem.exists(this.addSlash(path)) && !(await window.remixFileSystem.statExtended(this.addSlash(path))).isDirectory()) { - await window.remixFileSystem.unlink(this.addSlash(path)) + if (await window.remixFileSystem.exists(path) && !(await window.remixFileSystem.stat(path)).isDirectory()) { + await window.remixFileSystem.unlink(path) this.event.emit('fileRemoved', this._normalizePath(path)) return true } else return false @@ -263,7 +262,7 @@ class FileProvider { var unprefixedoldPath = this.removePrefix(oldPath) var unprefixednewPath = this.removePrefix(newPath) if (await this._exists(unprefixedoldPath)) { - await window.remixFileSystem.rename(this.addSlash(unprefixedoldPath), this.addSlash(unprefixednewPath)) + await window.remixFileSystem.rename(unprefixedoldPath, unprefixednewPath) this.event.emit('fileRenamed', this._normalizePath(unprefixedoldPath), this._normalizePath(unprefixednewPath), @@ -278,7 +277,7 @@ class FileProvider { path = this.removePrefix(path) if (path.indexOf('/') !== 0) path = '/' + path try { - const files = await window.remixFileSystem.readdir(this.addSlash(path)) + const files = await window.remixFileSystem.readdir(path) const ret = {} if (files) { @@ -286,7 +285,7 @@ class FileProvider { path = path.replace(/^\/|\/$/g, '') // remove first and last slash element = element.replace(/^\/|\/$/g, '') // remove first and last slash const absPath = (path === '/' ? '' : path) + '/' + element - ret[absPath.indexOf('/') === 0 ? absPath.substr(1, absPath.length) : absPath] = { isDirectory: (await window.remixFileSystem.statExtended(this.addSlash(absPath))).isDirectory() } + ret[absPath.indexOf('/') === 0 ? absPath.substr(1, absPath.length) : absPath] = { isDirectory: (await window.remixFileSystem.stat(absPath)).isDirectory() } // ^ ret does not accept path starting with '/' } } @@ -298,11 +297,6 @@ class FileProvider { } } - addSlash (file) { - if (!file.startsWith('/'))file = '/' + file - return file - } - removePrefix (path) { path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path if (path === '') return '/' diff --git a/apps/remix-ide/src/assets/js/init.js b/apps/remix-ide/src/assets/js/init.js index 6c175c1517..d7bdb27cda 100644 --- a/apps/remix-ide/src/assets/js/init.js +++ b/apps/remix-ide/src/assets/js/init.js @@ -56,16 +56,44 @@ window.onload = () => { class RemixFileSystem extends LightningFS { constructor (...t) { super(...t) + this.addSlash = (file) => { + if (!file.startsWith('/'))file = '/' + file + return file + } + this.base = this.promises this.promises = { ...this.promises, + exists: async (path) => { return new Promise((resolve, reject) => { - this.promises.stat(path).then(() => resolve(true)).catch(() => resolve(false)) + this.base.stat(this.addSlash(path)).then(() => resolve(true)).catch(() => resolve(false)) }) }, - statExtended: async (path) => { - return new Promise((resolve, reject) => { - this.promises.stat(path).then((stat) => { + rmdir: async (path) => { + return this.base.rmdir(this.addSlash(path)) + }, + readdir: async (path) => { + return this.base.readdir(this.addSlash(path)) + }, + unlink: async (path) => { + return this.base.unlink(this.addSlash(path)) + }, + mkdir: async (path) => { + return this.base.mkdir(this.addSlash(path)) + }, + readFile: async (path, options) => { + return this.base.readFile(this.addSlash(path), options) + }, + rename: async (from, to) => { + return this.base.rename(this.addSlash(from), this.addSlash(to)) + }, + writeFile: async (path, content, options) => { + return this.base.writeFile(this.addSlash(path), content, options) + }, + stat: async (path) => { + return this.base.stat(this.addSlash(path)) + /* return new Promise((resolve, reject) => { + this.base.stat(this.addSlash(path)).then((stat) => { resolve({ isDirectory: () => { return stat.type === 'dir' @@ -75,7 +103,7 @@ window.onload = () => { } }) }).catch(() => reject(false)) - }) + }) */ } } }