parent
6ba69c9d46
commit
fa33283e33
File diff suppressed because one or more lines are too long
@ -1,119 +0,0 @@ |
||||
'use strict' |
||||
const EventManager = require('../../lib/events') |
||||
const toolTip = require('../ui/tooltip') |
||||
|
||||
class BasicFileProvider { |
||||
constructor (type) { |
||||
this.event = new EventManager() |
||||
this.files = {} |
||||
this.paths = {} |
||||
this.normalizedNames = {} // contains the raw url associated with the displayed path
|
||||
this.paths[type] = {} |
||||
this.type = type |
||||
this.readonly = true |
||||
} |
||||
|
||||
close (cb) { |
||||
this.files = {} |
||||
cb() |
||||
} |
||||
|
||||
init (cb) { |
||||
this.files = {} |
||||
} |
||||
|
||||
exists (path, cb) { |
||||
if (!this.files) return cb(null, false) |
||||
var unprefixedPath = this.removePrefix(path) |
||||
cb(null, this.files[unprefixedPath] !== undefined) |
||||
} |
||||
|
||||
get (path, cb) { |
||||
if (this.normalizedNames[path]) path = this.normalizedNames[path] // ensure we actually use the normalized path from here
|
||||
var unprefixedPath = this.removePrefix(path) |
||||
var content = this.files[unprefixedPath] |
||||
if (!content) { |
||||
content = this.files[this.type + '/' + this.normalizedNames[path]] |
||||
} |
||||
if (cb) { |
||||
cb(null, content) |
||||
} |
||||
return content |
||||
} |
||||
|
||||
set (path, content, cb) { |
||||
this.addReadOnly(path, content) |
||||
if (cb) cb() |
||||
return true |
||||
} |
||||
|
||||
addReadOnly (path, content, rawPath) { |
||||
path = this.removePrefix(path) |
||||
try { // lazy try to format JSON
|
||||
content = JSON.stringify(JSON.parse(content), null, '\t') |
||||
} catch (e) {} |
||||
if (!rawPath) rawPath = path |
||||
// splitting off the path in a tree structure, the json tree is used in `resolveDirectory`
|
||||
var split = path |
||||
var folder = false |
||||
while (split.lastIndexOf('/') !== -1) { |
||||
var subitem = split.substring(split.lastIndexOf('/')) |
||||
split = split.substring(0, split.lastIndexOf('/')) |
||||
if (!this.paths[this.type + '/' + split]) { |
||||
this.paths[this.type + '/' + split] = {} |
||||
} |
||||
this.paths[this.type + '/' + split][split + subitem] = { isDirectory: folder } |
||||
folder = true |
||||
} |
||||
this.paths[this.type][split] = { isDirectory: folder } |
||||
this.files[path] = content |
||||
this.normalizedNames[rawPath] = path |
||||
this.event.trigger('fileAdded', [this.type + '/' + path, true]) |
||||
return true |
||||
} |
||||
|
||||
remove (path) { |
||||
var unprefixedPath = this.removePrefix(path) |
||||
var folderPath = path.substring(0, path.lastIndexOf('/')) |
||||
if (this.paths[folderPath]) { |
||||
delete this.paths[folderPath][unprefixedPath] |
||||
delete this.files[path] |
||||
} |
||||
this.event.trigger('fileRemoved', [this.type + '/' + unprefixedPath]) |
||||
return true |
||||
} |
||||
|
||||
rename (oldPath, newPath, isFolder) { |
||||
if (isFolder) { return toolTip('folder renaming is not handled by this explorer') } |
||||
var unprefixedoldPath = this.removePrefix(oldPath) |
||||
var unprefixednewPath = this.removePrefix(newPath) |
||||
this.get(oldPath, (error, content) => { |
||||
if (error) return console.log(error) |
||||
this.remove(oldPath) |
||||
this.set(newPath, content) |
||||
this.event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) |
||||
}) |
||||
} |
||||
|
||||
isReadOnly (path) { |
||||
return false |
||||
} |
||||
|
||||
list () { |
||||
return this.files |
||||
} |
||||
|
||||
resolveDirectory (path, callback) { |
||||
var self = this |
||||
if (path[0] === '/') path = path.substring(1) |
||||
if (!path) return callback(null, { [self.type]: { } }) |
||||
// we just return the json tree populated by `addReadOnly`
|
||||
callback(null, this.paths[path]) |
||||
} |
||||
|
||||
removePrefix (path) { |
||||
return path.indexOf(this.type + '/') === 0 ? path.replace(this.type + '/', '') : path |
||||
} |
||||
} |
||||
|
||||
module.exports = BasicFileProvider |
@ -1,139 +0,0 @@ |
||||
'use strict' |
||||
|
||||
var EventManager = require('../../lib/events') |
||||
|
||||
class FileProvider { |
||||
constructor (name, storage) { |
||||
this.event = new EventManager() |
||||
this.storage = storage |
||||
this.type = name |
||||
this.structFile = '.' + name + '.tree' |
||||
this.tree = {} |
||||
} |
||||
|
||||
exists (path, cb) { |
||||
cb(null, this._exists(path)) |
||||
} |
||||
|
||||
updateRefs (path, type) { |
||||
var split = path.split('/') // this should be unprefixed path
|
||||
var crawlpath = this.tree |
||||
var intermediatePath = '' |
||||
split.forEach((pathPart, index) => { |
||||
intermediatePath += pathPart |
||||
if (!crawlpath[pathPart]) crawlpath[intermediatePath] = {} |
||||
if (index < split.length - 1) { |
||||
crawlpath = crawlpath[intermediatePath] |
||||
intermediatePath += '/' |
||||
} else if (type === 'add') { |
||||
crawlpath[intermediatePath] = path |
||||
} else if (type === 'remove' && crawlpath[intermediatePath]) { |
||||
delete crawlpath[intermediatePath] |
||||
} |
||||
}) |
||||
this.storage.set(this.structFile, JSON.stringify(this.tree)) |
||||
} |
||||
|
||||
_exists (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
return this.storage.exists(unprefixedpath) |
||||
} |
||||
|
||||
init (cb) { |
||||
var tree = this.storage.get(this.structFile) |
||||
this.tree = tree ? JSON.parse(tree) : {} |
||||
if (cb) cb() |
||||
} |
||||
|
||||
get (path, cb) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
var content = this.storage.get(unprefixedpath) |
||||
if (cb) { |
||||
cb(null, content) |
||||
} |
||||
return content |
||||
} |
||||
|
||||
set (path, content, cb) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
this.updateRefs(unprefixedpath, 'add') |
||||
var exists = this.storage.exists(unprefixedpath) |
||||
if (!this.storage.set(unprefixedpath, content)) { |
||||
if (cb) cb('error updating ' + path) |
||||
return false |
||||
} |
||||
if (!exists) { |
||||
this.event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) |
||||
} else { |
||||
this.event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) |
||||
} |
||||
if (cb) cb() |
||||
return true |
||||
} |
||||
|
||||
addReadOnly (path, content) { |
||||
return this.set(path, content) |
||||
} |
||||
|
||||
isReadOnly (path) { |
||||
return false |
||||
} |
||||
|
||||
remove (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
this.updateRefs(unprefixedpath, 'remove') |
||||
if (!this._exists(unprefixedpath)) { |
||||
return false |
||||
} |
||||
if (!this.storage.remove(unprefixedpath)) { |
||||
return false |
||||
} |
||||
this.event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) |
||||
return true |
||||
} |
||||
|
||||
rename (oldPath, newPath, isFolder) { |
||||
var unprefixedoldPath = this.removePrefix(oldPath) |
||||
var unprefixednewPath = this.removePrefix(newPath) |
||||
this.updateRefs(unprefixedoldPath, 'remove') |
||||
this.updateRefs(unprefixednewPath, 'add') |
||||
if (this.storage.exists(unprefixedoldPath)) { |
||||
if (!this.storage.rename(unprefixedoldPath, unprefixednewPath)) { |
||||
return false |
||||
} |
||||
this.event.trigger('fileRenamed', [ |
||||
this.type + '/' + unprefixedoldPath, |
||||
this.type + '/' + unprefixednewPath, |
||||
isFolder |
||||
]) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
resolveDirectory (path, callback) { |
||||
if (path[0] === '/') path = path.substring(1) |
||||
if (!path) return callback(null, { [this.type]: {} }) |
||||
var tree = {} |
||||
path = this.removePrefix(path) |
||||
|
||||
var split = path.split('/') // this should be unprefixed path
|
||||
var crawlpath = this.tree |
||||
split.forEach((pathPart, index) => { |
||||
if (crawlpath[pathPart]) crawlpath = crawlpath[pathPart] |
||||
}) |
||||
|
||||
for (var item in crawlpath) { |
||||
tree[item] = { isDirectory: typeof crawlpath[item] !== 'string' } |
||||
} |
||||
callback(null, tree) |
||||
} |
||||
|
||||
removePrefix (path) { |
||||
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path |
||||
if (path[0] === '/') return path.substring(1) |
||||
return path |
||||
} |
||||
} |
||||
|
||||
module.exports = FileProvider |
@ -0,0 +1,138 @@ |
||||
'use strict' |
||||
|
||||
var EventManager = require('../../lib/events') |
||||
|
||||
class FileProvider { |
||||
constructor (name) { |
||||
this.event = new EventManager() |
||||
this.type = name |
||||
} |
||||
|
||||
exists (path, cb) { |
||||
cb(null, this._exists(path)) |
||||
} |
||||
|
||||
_exists (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
return window.remixFileSystem.existsSync(unprefixedpath) |
||||
} |
||||
|
||||
init (cb) { |
||||
cb() |
||||
} |
||||
|
||||
get (path, cb) { |
||||
cb = cb || function () {} |
||||
var unprefixedpath = this.removePrefix(path) |
||||
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
||||
if (!exists) return cb(null, null) |
||||
window.remixFileSystem.readFile(unprefixedpath, 'utf8', (err, content) => { |
||||
cb(err, content) |
||||
}) |
||||
} |
||||
|
||||
set (path, content, cb) { |
||||
cb = cb || function () {} |
||||
var unprefixedpath = this.removePrefix(path) |
||||
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
||||
if (!exists && unprefixedpath.indexOf('/') !== -1) { |
||||
const paths = unprefixedpath.split('/') |
||||
paths.pop() // last element should the filename
|
||||
if (paths.length && paths[0] === '') paths.shift() |
||||
let currentCheck = '' |
||||
paths.forEach((value) => { |
||||
currentCheck = currentCheck + '/' + value |
||||
if (!window.remixFileSystem.existsSync(currentCheck)) { |
||||
window.remixFileSystem.mkdirSync(currentCheck) |
||||
this.event.trigger('folderAdded', [this._normalizePath(currentCheck)]) |
||||
} |
||||
}) |
||||
} |
||||
try { |
||||
window.remixFileSystem.writeFileSync(unprefixedpath, content) |
||||
} catch (e) { |
||||
cb(e) |
||||
return false |
||||
} |
||||
if (!exists) { |
||||
this.event.trigger('fileAdded', [this._normalizePath(unprefixedpath), false]) |
||||
} else { |
||||
this.event.trigger('fileChanged', [this._normalizePath(unprefixedpath)]) |
||||
} |
||||
cb() |
||||
return true |
||||
} |
||||
|
||||
addReadOnly (path, content) { |
||||
return this.set(path, content) |
||||
} |
||||
|
||||
isReadOnly (path) { |
||||
return false |
||||
} |
||||
|
||||
remove (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
if (!this._exists(unprefixedpath)) { |
||||
return false |
||||
} |
||||
const stat = window.remixFileSystem.statSync(unprefixedpath) |
||||
try { |
||||
if (stat.isDirectory()) { |
||||
window.remixFileSystem.rmdirSync(unprefixedpath, console.log) |
||||
} else { |
||||
window.remixFileSystem.unlinkSync(unprefixedpath, console.log) |
||||
} |
||||
this.event.trigger('fileRemoved', [this._normalizePath(unprefixedpath)]) |
||||
return true |
||||
} catch (e) { |
||||
console.log(e) |
||||
return false |
||||
} |
||||
} |
||||
|
||||
rename (oldPath, newPath, isFolder) { |
||||
var unprefixedoldPath = this.removePrefix(oldPath) |
||||
var unprefixednewPath = this.removePrefix(newPath) |
||||
if (this._exists(unprefixedoldPath)) { |
||||
window.remixFileSystem.renameSync(unprefixedoldPath, unprefixednewPath) |
||||
this.event.trigger('fileRenamed', [ |
||||
this._normalizePath(unprefixedoldPath), |
||||
this._normalizePath(unprefixednewPath), |
||||
isFolder |
||||
]) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
resolveDirectory (path, callback) { |
||||
if (!path) return callback(null, { [this.type]: {} }) |
||||
path = this.removePrefix(path) |
||||
if (path.indexOf('/') !== 0) path = '/' + path |
||||
|
||||
window.remixFileSystem.readdir(path, (error, files) => { |
||||
var ret = {} |
||||
if (files) { |
||||
files.forEach(element => { |
||||
const absPath = (path === '/' ? '' : path) + '/' + element |
||||
ret[absPath.indexOf('/') === 0 ? absPath.replace('/', '') : absPath] = { isDirectory: window.remixFileSystem.statSync(absPath).isDirectory() } |
||||
// ^ ret does not accept path starting with '/'
|
||||
}) |
||||
} |
||||
callback(error, ret) |
||||
}) |
||||
} |
||||
|
||||
removePrefix (path) { |
||||
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path |
||||
return path |
||||
} |
||||
|
||||
_normalizePath (path) { |
||||
if (path.indexOf('/') !== 0) path = '/' + path |
||||
return this.type + path |
||||
} |
||||
} |
||||
|
||||
module.exports = FileProvider |
@ -1,29 +0,0 @@ |
||||
'use strict' |
||||
|
||||
class FileProvider { |
||||
|
||||
exists (path, cb) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
init (cb) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
get (path, cb) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
set (path, content, cb) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
addReadOnly (path, content) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
isReadOnly (path) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
remove (path) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
rename (oldPath, newPath, isFolder) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
resolveDirectory (path, callback) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
removePrefix (path) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
updateRefs (path, type) { throw new Error(this.name + ' function is not implemented for ' + this.constructor.name + ' class') } |
||||
|
||||
} |
||||
|
||||
module.exports = FileProvider |
@ -1,148 +0,0 @@ |
||||
'use strict' |
||||
|
||||
var EventManager = require('../../lib/events') |
||||
|
||||
function LocalStorageProvider (storage) { |
||||
var event = new EventManager() |
||||
this.event = event |
||||
var readonly = {} |
||||
this.type = 'browser' |
||||
|
||||
this.exists = function (path, cb) { |
||||
cb(null, this._exists(path)) |
||||
} |
||||
|
||||
this._exists = function (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
// NOTE: ignore the config file
|
||||
if (path === '.remix.config') return false |
||||
|
||||
return this.isReadOnly(unprefixedpath) || storage.exists(unprefixedpath) |
||||
} |
||||
|
||||
this.init = function (cb) { |
||||
cb() |
||||
} |
||||
|
||||
this.get = function (path, cb) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
// NOTE: ignore the config file
|
||||
if (path === '.remix.config') { |
||||
return null |
||||
} |
||||
|
||||
var content = readonly[unprefixedpath] || storage.get(unprefixedpath) |
||||
if (cb) { |
||||
cb(null, content) |
||||
} |
||||
return content |
||||
} |
||||
|
||||
this.set = function (path, content, cb) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
// NOTE: ignore the config file
|
||||
if (path === '.remix.config') { |
||||
if (cb) cb('change not allowed') |
||||
return false |
||||
} |
||||
|
||||
if (!this.isReadOnly(unprefixedpath)) { |
||||
var exists = storage.exists(unprefixedpath) |
||||
if (!storage.set(unprefixedpath, content)) { |
||||
if (cb) cb('error updating ' + path) |
||||
return false |
||||
} |
||||
if (!exists) { |
||||
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) |
||||
} else { |
||||
event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) |
||||
} |
||||
if (cb) cb() |
||||
return true |
||||
} |
||||
if (cb) cb('is read only') |
||||
return false |
||||
} |
||||
|
||||
this.addReadOnly = function (path, content) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
if (!storage.exists(unprefixedpath)) { |
||||
readonly[unprefixedpath] = content |
||||
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, true]) |
||||
return true |
||||
} |
||||
|
||||
return false |
||||
} |
||||
|
||||
this.isReadOnly = function (path) { |
||||
path = this.removePrefix(path) |
||||
return readonly[path] !== undefined |
||||
} |
||||
|
||||
this.remove = function (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
if (!this._exists(unprefixedpath)) { |
||||
return false |
||||
} |
||||
|
||||
if (this.isReadOnly(unprefixedpath)) { |
||||
readonly[unprefixedpath] = undefined |
||||
} else { |
||||
if (!storage.remove(unprefixedpath)) { |
||||
return false |
||||
} |
||||
} |
||||
event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) |
||||
return true |
||||
} |
||||
|
||||
this.rename = function (oldPath, newPath, isFolder) { |
||||
var unprefixedoldPath = this.removePrefix(oldPath) |
||||
var unprefixednewPath = this.removePrefix(newPath) |
||||
if (!this.isReadOnly(unprefixedoldPath) && storage.exists(unprefixedoldPath)) { |
||||
if (!storage.rename(unprefixedoldPath, unprefixednewPath)) { |
||||
return false |
||||
} |
||||
event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
this.resolveDirectory = function (path, callback) { |
||||
var self = this |
||||
if (path[0] === '/') path = path.substring(1) |
||||
if (!path) return callback(null, { [self.type]: { } }) |
||||
path = self.removePrefix(path) |
||||
var filesList = {} |
||||
var tree = {} |
||||
// add r/w filesList to the list
|
||||
storage.keys().forEach((path) => { |
||||
// NOTE: as a temporary measure do not show the config file
|
||||
if (path !== '.remix.config') { |
||||
filesList[path] = false |
||||
} |
||||
}) |
||||
// add r/o files to the list
|
||||
Object.keys(readonly).forEach((path) => { |
||||
filesList[path] = true |
||||
}) |
||||
|
||||
Object.keys(filesList).forEach(function (path) { |
||||
tree[path] = { isDirectory: false } |
||||
}) |
||||
return callback(null, tree) |
||||
} |
||||
|
||||
this.removePrefix = function (path) { |
||||
return path.indexOf(this.type + '/') === 0 ? path.replace(this.type + '/', '') : path |
||||
} |
||||
|
||||
// rename .browser-solidity.json to .remix.config
|
||||
if (this._exists('.browser-solidity.json')) { |
||||
this.rename('.browser-solidity.json', '.remix.config') |
||||
} |
||||
} |
||||
|
||||
module.exports = LocalStorageProvider |
@ -1,18 +0,0 @@ |
||||
'use strict' |
||||
let BasicFileProvider = require('./basicFileProvider') |
||||
|
||||
class ReadonlyProvider extends BasicFileProvider { |
||||
|
||||
remove (path) { |
||||
return false |
||||
} |
||||
|
||||
rename (oldPath, newPath, isFolder) { |
||||
return false |
||||
} |
||||
|
||||
isReadOnly (path) { |
||||
return true |
||||
} |
||||
} |
||||
module.exports = ReadonlyProvider |
Loading…
Reference in new issue