Some tweaks, directory viewing

This commit is contained in:
Dakedres
2023-12-25 21:21:42 -05:00
parent 030f0e6438
commit 4648240842
6 changed files with 124 additions and 24 deletions

View File

@@ -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
//