From c512803685e20de523671fb1f4858e5b6d25d9a7 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 17 Mar 2021 11:20:34 +0100 Subject: [PATCH] copy all the files / folders to gist --- apps/remix-ide/src/app/files/fileProvider.js | 15 ++++--- .../src/app/files/workspaceFileProvider.js | 7 ++- .../file-explorer/src/lib/file-explorer.tsx | 43 ++++++------------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileProvider.js b/apps/remix-ide/src/app/files/fileProvider.js index 70dbdc7aca3..3a2e85abd8c 100644 --- a/apps/remix-ide/src/app/files/fileProvider.js +++ b/apps/remix-ide/src/app/files/fileProvider.js @@ -198,9 +198,10 @@ class FileProvider { /** * 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 */ - _copyFolderToJsonInternal (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._copyFolderToJsonInternal(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 }) @@ -231,10 +233,11 @@ class FileProvider { /** * copy the folder recursively * @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) { - return this._copyFolderToJsonInternal(path) + copyFolderToJson (path, visitFile) { + visitFile = visitFile || (() => {}) + return this._copyFolderToJsonInternal(path, visitFile) } removeFile (path) { diff --git a/apps/remix-ide/src/app/files/workspaceFileProvider.js b/apps/remix-ide/src/app/files/workspaceFileProvider.js index 91f3227fbd1..cc7368e0e2d 100644 --- a/apps/remix-ide/src/app/files/workspaceFileProvider.js +++ b/apps/remix-ide/src/app/files/workspaceFileProvider.js @@ -48,9 +48,12 @@ class WorkspaceFileProvider extends FileProvider { }) } - async copyFolderToJson (directory) { - let json = await super._copyFolderToJsonInternal(directory) + 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) } 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 bb2f513d102..729da26097c 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -559,7 +559,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? This also add an entry named 'project.json' which include all the folders and files.`, { + 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 }, { @@ -1059,35 +1059,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() - }) - } - }, async (error) => { - try { - const json = await filesProvider.copyFolderToJson(directory) - ret['project.json'] = { content: JSON.stringify(json, null, '\t') } - } catch (e) { - console.log(e) - } - 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) {