Some tweaks, directory viewing
This commit is contained in:
@@ -6,15 +6,21 @@ const handleNeutralinoError = err => {
|
||||
throw new Error(err.code + ': ' + err.message)
|
||||
}
|
||||
|
||||
const mapDirent = ({ entry, type }) => ({
|
||||
name: entry,
|
||||
type
|
||||
})
|
||||
|
||||
window.Platform = {
|
||||
//
|
||||
// Paths
|
||||
//
|
||||
|
||||
dirname(path) {
|
||||
let index = path.lastIndexOf('/')
|
||||
let normalPath = this.normalize(path)
|
||||
let index = normalPath.lastIndexOf('/')
|
||||
|
||||
return index === -1 ? '' : path.slice(0, index)
|
||||
return index === -1 ? '' : normalPath.slice(0, index)
|
||||
},
|
||||
|
||||
filename(path) {
|
||||
@@ -31,7 +37,13 @@ window.Platform = {
|
||||
},
|
||||
|
||||
join(...args) {
|
||||
return args.join('/')
|
||||
let parts = []
|
||||
|
||||
for(let arg of args) {
|
||||
parts = parts.concat(arg.split('/'))
|
||||
}
|
||||
|
||||
return this.normalizePathFromParts(parts)
|
||||
},
|
||||
|
||||
// TODO: support non-posix
|
||||
@@ -39,6 +51,38 @@ window.Platform = {
|
||||
return path.charAt(0) === '/'
|
||||
},
|
||||
|
||||
normalize(path) {
|
||||
return this.normalizePathFromParts(path.split('/'))
|
||||
},
|
||||
|
||||
normalizePathFromParts(parts) {
|
||||
// let newPath = path !== '/' && path.endsWith('/') ?
|
||||
// path.slice(0, -1) :
|
||||
// path
|
||||
|
||||
let out = []
|
||||
let skips = 0
|
||||
|
||||
for(let i = parts.length - 1; i >= 0; i--) {
|
||||
let part = parts[i]
|
||||
|
||||
if(part.length == 0 || part === '.') {
|
||||
continue
|
||||
} else if(part == '..') {
|
||||
skips++
|
||||
continue
|
||||
} else if(skips == 0) {
|
||||
out.unshift(part)
|
||||
} else {
|
||||
skips--
|
||||
}
|
||||
}
|
||||
|
||||
console.log(out)
|
||||
|
||||
return '/' + out.join('/')
|
||||
},
|
||||
|
||||
//
|
||||
// FS
|
||||
//
|
||||
@@ -66,6 +110,12 @@ window.Platform = {
|
||||
.catch(handleNeutralinoError)
|
||||
},
|
||||
|
||||
readdir(path) {
|
||||
return Neutralino.filesystem.readDirectory(path)
|
||||
.catch(handleNeutralinoError)
|
||||
.then(dirents => dirents.map(mapDirent))
|
||||
},
|
||||
|
||||
//
|
||||
// OS
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user