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. 43
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -198,9 +198,10 @@ class FileProvider {
/** /**
* copy the folder recursively (internal use) * 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
*/ */
_copyFolderToJsonInternal (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._copyFolderToJsonInternal(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
}) })
@ -231,10 +233,11 @@ class FileProvider {
/** /**
* copy the folder recursively * copy the folder recursively
* @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) { copyFolderToJson (path, visitFile) {
return this._copyFolderToJsonInternal(path) visitFile = visitFile || (() => {})
return this._copyFolderToJsonInternal(path, visitFile)
} }
removeFile (path) { removeFile (path) {

@ -48,9 +48,12 @@ class WorkspaceFileProvider extends FileProvider {
}) })
} }
async copyFolderToJson (directory) { async copyFolderToJson (directory, visitFile) {
let json = await super._copyFolderToJsonInternal(directory) visitFile = visitFile || (() => {})
const regex = new RegExp(`.workspaces/${this.workspace}/`, 'g'); 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, '') json = JSON.stringify(json).replace(regex, '')
return JSON.parse(json) return JSON.parse(json)
} }

@ -559,7 +559,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? 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', label: 'OK',
fn: toGist fn: toGist
}, { }, {
@ -1059,35 +1059,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 { if (/^\s+$/.test(content) || !content.length) {
async.eachSeries(Object.keys(files), (path, cb) => { content = '// this line is added to create a gist. Empty file is not allowed.'
if (filesProvider.isDirectory(path)) { }
cb() path = path.replace(/\//g, '...')
} else { ret[path] = { content }
filesProvider.get(path, (error, content) => { })
if (error) return cb(error) callback(null, ret)
if (/^\s+$/.test(content) || !content.length) { } catch (e) {
content = '// this line is added to create a gist. Empty file is not allowed.' return callback(e)
} }
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)
})
}
})
} }
function joinPath (...paths) { function joinPath (...paths) {

Loading…
Cancel
Save