replicate folder

pull/1021/head
yann300 4 years ago
parent e3bc26cf68
commit 0429892ca7
  1. 13
      apps/remix-ide/src/app/files/fileProvider.js
  2. 5
      apps/remix-ide/src/app/files/workspaceFileProvider.js
  3. 2
      apps/remix-ide/src/app/ui/landing-page/landing-page.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) {

@ -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)

@ -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')

Loading…
Cancel
Save