pull/1622/head
filip mertens 3 years ago
parent 238d1df2cc
commit c20024eb2d
  1. 16
      apps/remix-ide/src/app/files/dgitProvider.js
  2. 50
      apps/remix-ide/src/app/files/fileProvider.js
  3. 38
      apps/remix-ide/src/assets/js/init.js

@ -299,7 +299,7 @@ class DGitProvider extends Plugin {
const files = await this.getDirectory('/') const files = await this.getDirectory('/')
this.filesToSend = [] this.filesToSend = []
for (const file of files) { for (const file of files) {
const c = await window.remixFileSystem.readFile(addSlash(`${workspace.absolutePath}/${file}`)) const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`)
const ob = { const ob = {
path: file, path: file,
content: c content: c
@ -319,10 +319,10 @@ class DGitProvider extends Plugin {
this.filesToSend = [] this.filesToSend = []
const data = new FormData() const data = new FormData()
files.forEach(async (file) => { for (const file of files) {
const c = window.remixFileSystem.readFileSync(addSlash(`${workspace.absolutePath}/${file}`)) const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`)
data.append('file', new Blob([c]), `base/${file}`) data.append('file', new Blob([c]), `base/${file}`)
}) }
// get last commit data // get last commit data
let ob let ob
try { try {
@ -431,7 +431,7 @@ class DGitProvider extends Plugin {
this.createDirectories(`${workspace.absolutePath}/${dir}`) this.createDirectories(`${workspace.absolutePath}/${dir}`)
} catch (e) { throw new Error(e) } } catch (e) { throw new Error(e) }
try { try {
await window.remixFileSystem.writeFile(addSlash(`${workspace.absolutePath}/${file.path}`, Buffer.concat(content) || new Uint8Array())) await window.remixFileSystem.writeFile(`${workspace.absolutePath}/${file.path}`, Buffer.concat(content) || new Uint8Array())
} catch (e) { throw new Error(e) } } catch (e) { throw new Error(e) }
} }
} catch (e) { } catch (e) {
@ -495,7 +495,7 @@ class DGitProvider extends Plugin {
const files = await this.getDirectory('/') const files = await this.getDirectory('/')
this.filesToSend = [] this.filesToSend = []
for (const file of files) { for (const file of files) {
const c = await window.remixFileSystem.readFile(addSlash(`${workspace.absolutePath}/${file}`)) const c = await window.remixFileSystem.readFile(`${workspace.absolutePath}/${file}`)
zip.file(file, c) zip.file(file, c)
} }
await zip.generateAsync({ await zip.generateAsync({
@ -515,8 +515,8 @@ class DGitProvider extends Plugin {
if (i > 0) previouspath = '/' + directories.slice(0, i).join('/') if (i > 0) previouspath = '/' + directories.slice(0, i).join('/')
const finalPath = previouspath + '/' + directories[i] const finalPath = previouspath + '/' + directories[i]
try { try {
if (!await window.remixFileSystem.exists(addSlash(finalPath))) { if (!await window.remixFileSystem.exists(finalPath)) {
await window.remixFileSystem.mkdir(addSlash(finalPath)) await window.remixFileSystem.mkdir(finalPath)
} }
} catch (e) { } catch (e) {
console.log(e) console.log(e)

@ -80,7 +80,7 @@ class FileProvider {
async _exists (path) { async _exists (path) {
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
return path === this.type ? true : await window.remixFileSystem.exists(this.addSlash(unprefixedpath)) return path === this.type ? true : await window.remixFileSystem.exists(unprefixedpath)
} }
init (cb) { init (cb) {
@ -91,8 +91,7 @@ class FileProvider {
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
try { try {
console.log('getting', this.addSlash(unprefixedpath)) const content = await window.remixFileSystem.readFile(unprefixedpath, 'utf8')
const content = await window.remixFileSystem.readFile(this.addSlash(unprefixedpath), 'utf8')
if (cb) cb(null, content) if (cb) cb(null, content)
return content return content
} catch (err) { } catch (err) {
@ -103,15 +102,15 @@ class FileProvider {
async set (path, content, cb) { async set (path, content, cb) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
const exists = await window.remixFileSystem.exists(this.addSlash(unprefixedpath)) const exists = await window.remixFileSystem.exists(unprefixedpath)
if (exists && await window.remixFileSystem.readFile(this.addSlash(unprefixedpath), 'utf8') === content) { if (exists && await window.remixFileSystem.readFile(unprefixedpath, 'utf8') === content) {
if (cb) cb() if (cb) cb()
return null return null
} }
await this.createDir(path.substr(0, path.lastIndexOf('/'))) await this.createDir(path.substr(0, path.lastIndexOf('/')))
try { try {
await window.remixFileSystem.writeFile(this.addSlash(unprefixedpath), content, 'utf8') await window.remixFileSystem.writeFile(unprefixedpath, content, 'utf8')
} catch (e) { } catch (e) {
if (cb) cb(e) if (cb) cb(e)
return false return false
@ -156,13 +155,13 @@ class FileProvider {
async isDirectory (path) { async isDirectory (path) {
const unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
return path === this.type ? true : (await window.remixFileSystem.statExtended(this.addSlash(unprefixedpath))).isDirectory() return path === this.type ? true : (await window.remixFileSystem.stat(unprefixedpath)).isDirectory()
} }
async isFile (path) { async isFile (path) {
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
path = this.removePrefix(path) path = this.removePrefix(path)
return (await window.remixFileSystem.statExtended(this.addSlash(path))).isFile() return (await window.remixFileSystem.stat(path)).isFile()
} }
/** /**
@ -171,26 +170,26 @@ class FileProvider {
*/ */
async remove (path) { async remove (path) {
path = this.removePrefix(path) path = this.removePrefix(path)
if (await window.remixFileSystem.exists(this.addSlash(path))) { if (await window.remixFileSystem.exists(path)) {
const stat = await window.remixFileSystem.statExtended(this.addSlash(path)) const stat = await window.remixFileSystem.stat(path)
try { try {
if (!stat.isDirectory()) { if (!stat.isDirectory()) {
return (this.removeFile(path)) return (this.removeFile(path))
} else { } else {
const items = await window.remixFileSystem.readdir(this.addSlash(path)) const items = await window.remixFileSystem.readdir(path)
if (items.length !== 0) { if (items.length !== 0) {
for (const item of items) { for (const item of items) {
const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}`
if ((await window.remixFileSystem.statExtended(this.addSlash(curPath))).isDirectory()) { // delete folder if ((await window.remixFileSystem.stat(curPath)).isDirectory()) { // delete folder
await this.remove(curPath) await this.remove(curPath)
} else { // delete file } else { // delete file
await this.removeFile(curPath) await this.removeFile(curPath)
} }
} }
await window.remixFileSystem.rmdir(this.addSlash(path)) await window.remixFileSystem.rmdir(path)
} else { } else {
// folder is empty // folder is empty
await window.remixFileSystem.rmdir(this.addSlash(path)) await window.remixFileSystem.rmdir(path)
} }
this.event.emit('fileRemoved', this._normalizePath(path)) this.event.emit('fileRemoved', this._normalizePath(path))
} }
@ -213,18 +212,18 @@ class FileProvider {
const json = {} const json = {}
path = this.removePrefix(path) path = this.removePrefix(path)
if (await window.remixFileSystem.exists(this.addSlash(path))) { if (await window.remixFileSystem.exists(path)) {
try { try {
const items = await window.remixFileSystem.readdir(this.addSlash(path)) const items = await window.remixFileSystem.readdir(path)
visitFolder({ path }) visitFolder({ path })
if (items.length !== 0) { if (items.length !== 0) {
for (const item of items) { for (const item of items) {
const file = {} const file = {}
const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}`
if ((await window.remixFileSystem.statExtended(this.addSlash(curPath))).isDirectory()) { if ((await window.remixFileSystem.stat(curPath)).isDirectory()) {
file.children = await this._copyFolderToJsonInternal(curPath, visitFile, visitFolder) file.children = await this._copyFolderToJsonInternal(curPath, visitFile, visitFolder)
} else { } else {
file.content = await window.remixFileSystem.readFile(this.addSlash(curPath), 'utf8') file.content = await window.remixFileSystem.readFile(curPath, 'utf8')
visitFile({ path: curPath, content: file.content }) visitFile({ path: curPath, content: file.content })
} }
json[curPath] = file json[curPath] = file
@ -252,8 +251,8 @@ class FileProvider {
async removeFile (path) { async removeFile (path) {
path = this.removePrefix(path) path = this.removePrefix(path)
if (await window.remixFileSystem.exists(this.addSlash(path)) && !(await window.remixFileSystem.statExtended(this.addSlash(path))).isDirectory()) { if (await window.remixFileSystem.exists(path) && !(await window.remixFileSystem.stat(path)).isDirectory()) {
await window.remixFileSystem.unlink(this.addSlash(path)) await window.remixFileSystem.unlink(path)
this.event.emit('fileRemoved', this._normalizePath(path)) this.event.emit('fileRemoved', this._normalizePath(path))
return true return true
} else return false } else return false
@ -263,7 +262,7 @@ class FileProvider {
var unprefixedoldPath = this.removePrefix(oldPath) var unprefixedoldPath = this.removePrefix(oldPath)
var unprefixednewPath = this.removePrefix(newPath) var unprefixednewPath = this.removePrefix(newPath)
if (await this._exists(unprefixedoldPath)) { if (await this._exists(unprefixedoldPath)) {
await window.remixFileSystem.rename(this.addSlash(unprefixedoldPath), this.addSlash(unprefixednewPath)) await window.remixFileSystem.rename(unprefixedoldPath, unprefixednewPath)
this.event.emit('fileRenamed', this.event.emit('fileRenamed',
this._normalizePath(unprefixedoldPath), this._normalizePath(unprefixedoldPath),
this._normalizePath(unprefixednewPath), this._normalizePath(unprefixednewPath),
@ -278,7 +277,7 @@ class FileProvider {
path = this.removePrefix(path) path = this.removePrefix(path)
if (path.indexOf('/') !== 0) path = '/' + path if (path.indexOf('/') !== 0) path = '/' + path
try { try {
const files = await window.remixFileSystem.readdir(this.addSlash(path)) const files = await window.remixFileSystem.readdir(path)
const ret = {} const ret = {}
if (files) { if (files) {
@ -286,7 +285,7 @@ class FileProvider {
path = path.replace(/^\/|\/$/g, '') // remove first and last slash path = path.replace(/^\/|\/$/g, '') // remove first and last slash
element = element.replace(/^\/|\/$/g, '') // remove first and last slash element = element.replace(/^\/|\/$/g, '') // remove first and last slash
const absPath = (path === '/' ? '' : path) + '/' + element const absPath = (path === '/' ? '' : path) + '/' + element
ret[absPath.indexOf('/') === 0 ? absPath.substr(1, absPath.length) : absPath] = { isDirectory: (await window.remixFileSystem.statExtended(this.addSlash(absPath))).isDirectory() } ret[absPath.indexOf('/') === 0 ? absPath.substr(1, absPath.length) : absPath] = { isDirectory: (await window.remixFileSystem.stat(absPath)).isDirectory() }
// ^ ret does not accept path starting with '/' // ^ ret does not accept path starting with '/'
} }
} }
@ -298,11 +297,6 @@ class FileProvider {
} }
} }
addSlash (file) {
if (!file.startsWith('/'))file = '/' + file
return file
}
removePrefix (path) { removePrefix (path) {
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path
if (path === '') return '/' if (path === '') return '/'

@ -56,16 +56,44 @@ window.onload = () => {
class RemixFileSystem extends LightningFS { class RemixFileSystem extends LightningFS {
constructor (...t) { constructor (...t) {
super(...t) super(...t)
this.addSlash = (file) => {
if (!file.startsWith('/'))file = '/' + file
return file
}
this.base = this.promises
this.promises = { this.promises = {
...this.promises, ...this.promises,
exists: async (path) => { exists: async (path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.promises.stat(path).then(() => resolve(true)).catch(() => resolve(false)) this.base.stat(this.addSlash(path)).then(() => resolve(true)).catch(() => resolve(false))
}) })
}, },
statExtended: async (path) => { rmdir: async (path) => {
return new Promise((resolve, reject) => { return this.base.rmdir(this.addSlash(path))
this.promises.stat(path).then((stat) => { },
readdir: async (path) => {
return this.base.readdir(this.addSlash(path))
},
unlink: async (path) => {
return this.base.unlink(this.addSlash(path))
},
mkdir: async (path) => {
return this.base.mkdir(this.addSlash(path))
},
readFile: async (path, options) => {
return this.base.readFile(this.addSlash(path), options)
},
rename: async (from, to) => {
return this.base.rename(this.addSlash(from), this.addSlash(to))
},
writeFile: async (path, content, options) => {
return this.base.writeFile(this.addSlash(path), content, options)
},
stat: async (path) => {
return this.base.stat(this.addSlash(path))
/* return new Promise((resolve, reject) => {
this.base.stat(this.addSlash(path)).then((stat) => {
resolve({ resolve({
isDirectory: () => { isDirectory: () => {
return stat.type === 'dir' return stat.type === 'dir'
@ -75,7 +103,7 @@ window.onload = () => {
} }
}) })
}).catch(() => reject(false)) }).catch(() => reject(false))
}) }) */
} }
} }
} }

Loading…
Cancel
Save