diff --git a/apps/remix-ide/src/app/files/fileProvider.js b/apps/remix-ide/src/app/files/fileProvider.js index adc5840dfdd..20a7135c12c 100644 --- a/apps/remix-ide/src/app/files/fileProvider.js +++ b/apps/remix-ide/src/app/files/fileProvider.js @@ -196,11 +196,12 @@ class FileProvider { } /** - * copy the folder recursively + * copy the folder recursively (internal use) * @param {string} path is the folder to be copied over - * @param {string} destination is the destination folder + * @param {Function} visitFile is a function called for each visited files */ - copyFolderToJson (path) { + _copyFolderToJsonInternal (path, visitFile) { + visitFile = visitFile || (() => {}) return new Promise((resolve, reject) => { const json = {} path = this.removePrefix(path) @@ -212,9 +213,10 @@ class FileProvider { const file = {} const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` if (window.remixFileSystem.statSync(curPath).isDirectory()) { - file.children = await this.copyFolderToJson(curPath) + file.children = await this._copyFolderToJsonInternal(curPath, visitFile) } else { file.content = window.remixFileSystem.readFileSync(curPath, 'utf8') + visitFile({ path: curPath, content: file.content }) } json[curPath] = file }) @@ -228,6 +230,16 @@ 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 + */ + copyFolderToJson (path, visitFile) { + visitFile = visitFile || (() => {}) + return this._copyFolderToJsonInternal(path, visitFile) + } + removeFile (path) { path = this.removePrefix(path) if (window.remixFileSystem.existsSync(path) && !window.remixFileSystem.statSync(path).isDirectory()) { diff --git a/apps/remix-ide/src/app/files/workspaceFileProvider.js b/apps/remix-ide/src/app/files/workspaceFileProvider.js index 8522f5ff4cf..2b1a795d0b0 100644 --- a/apps/remix-ide/src/app/files/workspaceFileProvider.js +++ b/apps/remix-ide/src/app/files/workspaceFileProvider.js @@ -48,6 +48,16 @@ class WorkspaceFileProvider extends FileProvider { }) } + async copyFolderToJson (directory, visitFile) { + visitFile = visitFile || (() => {}) + const regex = new RegExp(`.workspaces/${this.workspace}/`, 'g') + let json = await super._copyFolderToJsonInternal(directory, ({ path, content }) => { + visitFile({ path: path.replace(regex, ''), content }) + }) + json = JSON.stringify(json).replace(regex, '') + return JSON.parse(json) + } + _normalizePath (path) { if (!this.workspace) throw new Error('No workspace has been opened.') return path.replace(this.workspacesPath + '/' + this.workspace + '/', '') diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index 5d6bdada62f..60176dcd2d2 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -3,7 +3,6 @@ import React, { useEffect, useState, useRef } from 'react' // eslint-disable-lin import { TreeView, TreeViewItem } from '@remix-ui/tree-view' // eslint-disable-line import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line import { Toaster } from '@remix-ui/toaster' // eslint-disable-line -import * as async from 'async' import Gists from 'gists' import { FileExplorerMenu } from './file-explorer-menu' // eslint-disable-line import { FileExplorerContextMenu } from './file-explorer-context-menu' // eslint-disable-line @@ -559,7 +558,7 @@ export const FileExplorer = (props: FileExplorerProps) => { } const publishToGist = () => { - modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com? Note: this will not include directories.`, { + modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com?`, { label: 'OK', fn: toGist }, { @@ -587,7 +586,9 @@ export const FileExplorer = (props: FileExplorerProps) => { fn: () => {} }) } else { - modal('Publish to gist Failed', data.message + ' ' + data.documentation_url + ' ' + JSON.stringify(data.errors, null, '\t'), { + const error = JSON.stringify(data.errors, null, '\t') || '' + const message = data.message === 'Not Found' ? data.message + '. Please make sure the API token has right to create a gist.' : data.message + modal('Publish to gist Failed', message + ' ' + data.documentation_url + ' ' + error, { label: 'Close', fn: async () => {} }, null) @@ -1057,29 +1058,20 @@ export const FileExplorer = (props: FileExplorerProps) => { export default FileExplorer -function packageFiles (filesProvider, directory, callback) { +async function packageFiles (filesProvider, directory, callback) { const ret = {} - filesProvider.resolveDirectory(directory, (error, files) => { - if (error) callback(error) - else { - async.eachSeries(Object.keys(files), (path, cb) => { - if (filesProvider.isDirectory(path)) { - cb() - } else { - filesProvider.get(path, (error, content) => { - if (error) return cb(error) - if (/^\s+$/.test(content) || !content.length) { - content = '// this line is added to create a gist. Empty file is not allowed.' - } - ret[path] = { content } - cb() - }) - } - }, (error) => { - callback(error, ret) - }) - } - }) + try { + await filesProvider.copyFolderToJson(directory, ({ path, content }) => { + if (/^\s+$/.test(content) || !content.length) { + content = '// this line is added to create a gist. Empty file is not allowed.' + } + path = path.replace(/\//g, '...') + ret[path] = { content } + }) + callback(null, ret) + } catch (e) { + return callback(e) + } } function joinPath (...paths) {