From 0429892ca72163bdbf178fb1c0f372f3a0be77b8 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 24 Mar 2021 12:20:35 +0100 Subject: [PATCH] replicate folder --- apps/remix-ide/src/app/files/fileProvider.js | 13 +++++++++---- .../src/app/files/workspaceFileProvider.js | 5 ++++- .../src/app/ui/landing-page/landing-page.js | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileProvider.js b/apps/remix-ide/src/app/files/fileProvider.js index 20a7135c12..544a1b0bab 100644 --- a/apps/remix-ide/src/app/files/fileProvider.js +++ b/apps/remix-ide/src/app/files/fileProvider.js @@ -199,21 +199,24 @@ class FileProvider { * copy the folder recursively (internal use) * @param {string} path is the folder to be copied over * @param {Function} visitFile is a function called for each visited files + * @param {Function} visitFolder is a function called for each visited folders */ - _copyFolderToJsonInternal (path, visitFile) { + _copyFolderToJsonInternal (path, visitFile, visitFolder) { visitFile = visitFile || (() => {}) + visitFolder = visitFolder || (() => {}) return new Promise((resolve, reject) => { const json = {} path = this.removePrefix(path) if (window.remixFileSystem.existsSync(path)) { try { const items = window.remixFileSystem.readdirSync(path) + visitFolder({ path }) if (items.length !== 0) { items.forEach(async (item, index) => { const file = {} const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` if (window.remixFileSystem.statSync(curPath).isDirectory()) { - file.children = await this._copyFolderToJsonInternal(curPath, visitFile) + file.children = await this._copyFolderToJsonInternal(curPath, visitFile, visitFolder) } else { file.content = window.remixFileSystem.readFileSync(curPath, 'utf8') visitFile({ path: curPath, content: file.content }) @@ -234,10 +237,12 @@ class FileProvider { * copy the folder recursively * @param {string} path is the folder to be copied over * @param {Function} visitFile is a function called for each visited files + * @param {Function} visitFolder is a function called for each visited folders */ - copyFolderToJson (path, visitFile) { + copyFolderToJson (path, visitFile, visitFolder) { visitFile = visitFile || (() => {}) - return this._copyFolderToJsonInternal(path, visitFile) + visitFolder = visitFolder || (() => {}) + return this._copyFolderToJsonInternal(path, visitFile, visitFolder) } removeFile (path) { diff --git a/apps/remix-ide/src/app/files/workspaceFileProvider.js b/apps/remix-ide/src/app/files/workspaceFileProvider.js index 2b1a795d0b..0764f0726a 100644 --- a/apps/remix-ide/src/app/files/workspaceFileProvider.js +++ b/apps/remix-ide/src/app/files/workspaceFileProvider.js @@ -48,11 +48,14 @@ class WorkspaceFileProvider extends FileProvider { }) } - async copyFolderToJson (directory, visitFile) { + async copyFolderToJson (directory, visitFile, visitFolder) { visitFile = visitFile || (() => {}) + visitFolder = visitFolder || (() => {}) const regex = new RegExp(`.workspaces/${this.workspace}/`, 'g') let json = await super._copyFolderToJsonInternal(directory, ({ path, content }) => { visitFile({ path: path.replace(regex, ''), content }) + },({ path }) => { + visitFolder({ path: path.replace(regex, '') }) }) json = JSON.stringify(json).replace(regex, '') return JSON.parse(json) diff --git a/apps/remix-ide/src/app/ui/landing-page/landing-page.js b/apps/remix-ide/src/app/ui/landing-page/landing-page.js index f927580f27..be73a5c762 100644 --- a/apps/remix-ide/src/app/ui/landing-page/landing-page.js +++ b/apps/remix-ide/src/app/ui/landing-page/landing-page.js @@ -327,6 +327,8 @@ export class LandingPage extends ViewPlugin { const zip = new JSZip() await fileProviders.browser.copyFolderToJson('/', ({ path, content }) => { zip.file(path, content) + },({ path }) => { + zip.folder(path) }) zip.generateAsync({ type: 'blob' }).then(function (blob) { saveAs(blob, 'remixdbackup.zip')