Skip to content

Commit

Permalink
Merge pull request #982 from ethereum/fixGist
Browse files Browse the repository at this point in the history
Fix create gist
  • Loading branch information
yann300 committed Mar 17, 2021
2 parents bc2b287 + 36f0b44 commit 9d62191
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
20 changes: 16 additions & 4 deletions apps/remix-ide/src/app/files/fileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
})
Expand All @@ -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()) {
Expand Down
10 changes: 10 additions & 0 deletions apps/remix-ide/src/app/files/workspaceFileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/', '')
Expand Down
42 changes: 17 additions & 25 deletions libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}, {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9d62191

Please sign in to comment.