copy all the files / folders to gist

pull/982/head
yann300 4 years ago
parent 1decb8d37c
commit c512803685
  1. 15
      apps/remix-ide/src/app/files/fileProvider.js
  2. 7
      apps/remix-ide/src/app/files/workspaceFileProvider.js
  3. 29
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

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

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

@ -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)
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 }
cb()
})
}
}, async (error) => {
try {
const json = await filesProvider.copyFolderToJson(directory)
ret['project.json'] = { content: JSON.stringify(json, null, '\t') }
callback(null, ret)
} catch (e) {
console.log(e)
return callback(e)
}
callback(error, ret)
})
}
})
}
function joinPath (...paths) {

Loading…
Cancel
Save