Skip to content

Commit

Permalink
copy all the files / folders to gist
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Mar 17, 2021
1 parent 1decb8d commit c512803
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
15 changes: 9 additions & 6 deletions apps/remix-ide/src/app/files/fileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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._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
})
Expand All @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions apps/remix-ide/src/app/files/workspaceFileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 14 additions & 29 deletions libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c512803

Please sign in to comment.