qwe/lib/platforms/neutralino.js

125 lines
2.2 KiB
JavaScript

{
let userHome
const handleNeutralinoError = err => {
throw new Error(err.code + ': ' + err.message)
}
const mapDirent = ({ entry, type }) => ({
name: entry,
type
})
window.Platform = {
//
// Paths
//
dirname(path) {
let normalPath = this.normalize(path)
let index = normalPath.lastIndexOf('/')
return index === -1 ? '' : normalPath.slice(0, index)
},
filename(path) {
let index = path.lastIndexOf('/')
return index === -1 ? path : path.slice(index + 1)
},
ext(path) {
let filename = this.filename(path)
let index = filename.lastIndexOf('.')
return index === -1 ? '' : filename.slice(index + 1)
},
join(...args) {
let parts = []
for(let arg of args) {
parts = parts.concat(arg.split('/'))
}
return this.normalizePathFromParts(parts)
},
// TODO: support non-posix
absolute(path) {
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--
}
}
return '/' + out.join('/')
},
//
// FS
//
async access(path) {
try {
await Neutralino.filesystem.getStats(path)
return
} catch(err) {
if(err.name = 'NE_FS_NOPATHE') {
return false
} else {
throw err
}
}
},
writeText(path, content) {
return Neutralino.filesystem.writeFile(path, content)
.catch(handleNeutralinoError)
},
readText(path) {
return Neutralino.filesystem.readFile(path)
.catch(handleNeutralinoError)
},
readdir(path) {
return Neutralino.filesystem.readDirectory(path)
.catch(handleNeutralinoError)
.then(dirents => dirents.map(mapDirent))
},
//
// OS
//
async getHome() {
return userHome ?? (userHome = await Neutralino.os.getEnv('HOME'))
},
}
}