Merge pull request #982 from ethereum/fixGist

Fix create gist
pull/984/head
yann300 4 years ago committed by GitHub
commit 9d62191fe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      apps/remix-ide/src/app/files/fileProvider.js
  2. 10
      apps/remix-ide/src/app/files/workspaceFileProvider.js
  3. 30
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -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} 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) => { return new Promise((resolve, reject) => {
const json = {} const json = {}
path = this.removePrefix(path) path = this.removePrefix(path)
@ -212,9 +213,10 @@ class FileProvider {
const file = {} const file = {}
const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}`
if (window.remixFileSystem.statSync(curPath).isDirectory()) { if (window.remixFileSystem.statSync(curPath).isDirectory()) {
file.children = await this.copyFolderToJson(curPath) file.children = await this._copyFolderToJsonInternal(curPath, visitFile)
} else { } else {
file.content = window.remixFileSystem.readFileSync(curPath, 'utf8') file.content = window.remixFileSystem.readFileSync(curPath, 'utf8')
visitFile({ path: curPath, content: file.content })
} }
json[curPath] = file 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) { removeFile (path) {
path = this.removePrefix(path) path = this.removePrefix(path)
if (window.remixFileSystem.existsSync(path) && !window.remixFileSystem.statSync(path).isDirectory()) { if (window.remixFileSystem.existsSync(path) && !window.remixFileSystem.statSync(path).isDirectory()) {

@ -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) { _normalizePath (path) {
if (!this.workspace) throw new Error('No workspace has been opened.') if (!this.workspace) throw new Error('No workspace has been opened.')
return path.replace(this.workspacesPath + '/' + this.workspace + '/', '') return path.replace(this.workspacesPath + '/' + this.workspace + '/', '')

@ -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 { TreeView, TreeViewItem } from '@remix-ui/tree-view' // eslint-disable-line
import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line
import { Toaster } from '@remix-ui/toaster' // eslint-disable-line import { Toaster } from '@remix-ui/toaster' // eslint-disable-line
import * as async from 'async'
import Gists from 'gists' import Gists from 'gists'
import { FileExplorerMenu } from './file-explorer-menu' // eslint-disable-line import { FileExplorerMenu } from './file-explorer-menu' // eslint-disable-line
import { FileExplorerContextMenu } from './file-explorer-context-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 = () => { 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', label: 'OK',
fn: toGist fn: toGist
}, { }, {
@ -587,7 +586,9 @@ export const FileExplorer = (props: FileExplorerProps) => {
fn: () => {} fn: () => {}
}) })
} else { } 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', label: 'Close',
fn: async () => {} fn: async () => {}
}, null) }, null)
@ -1057,29 +1058,20 @@ export const FileExplorer = (props: FileExplorerProps) => {
export default FileExplorer export default FileExplorer
function packageFiles (filesProvider, directory, callback) { async function packageFiles (filesProvider, directory, callback) {
const ret = {} const ret = {}
filesProvider.resolveDirectory(directory, (error, files) => { try {
if (error) callback(error) await filesProvider.copyFolderToJson(directory, ({ path, content }) => {
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) { if (/^\s+$/.test(content) || !content.length) {
content = '// this line is added to create a gist. Empty file is not allowed.' content = '// this line is added to create a gist. Empty file is not allowed.'
} }
path = path.replace(/\//g, '...')
ret[path] = { content } ret[path] = { content }
cb()
})
}
}, (error) => {
callback(error, ret)
}) })
callback(null, ret)
} catch (e) {
return callback(e)
} }
})
} }
function joinPath (...paths) { function joinPath (...paths) {

Loading…
Cancel
Save