qwe/device/nw.js
2024-12-11 19:21:57 -07:00

69 lines
1.4 KiB
JavaScript

const FS = require('fs/promises')
const Path = require('path')
const themePath = process.env.HOME ? Path.join(process.env.HOME, '.config/base16.css') : 'base16.css'
function handle(sPath) {
return {
name: Path.basename(sPath),
path: absolute(sPath)
}
}
function absolute(sPath) {
return Path.isAbsolute(sPath) ? sPath : Path.join(window.cwd, sPath)
}
export async function Init() {
window.cwd = nw.App.startPath
await loadTheme()
watchTheme()
}
async function watchTheme() {
let watcher = await FS.watch(themePath)
for await (const event of watcher) {
await loadTheme()
}
}
async function loadTheme() {
let f = await Open(handle(themePath))
loadStylesheet(f)
}
function loadStylesheet(fStylesheet) {
let l = document.createElement('link')
l.rel = "stylesheet"
l.href = URL.createObjectURL(fStylesheet)
document.head.appendChild(l)
}
export async function Entries() {
let pool = new Map()
for(let d of await FS.readdir(window.cwd, { withFileTypes: true })) {
if(d.isFile()) {
let h = handle(d.name)
pool.set(d.name, h)
}
}
return pool
}
export async function Create(sPath) {
return handle(sPath)
}
export async function Open(hFile) {
let b = await FS.readFile(hFile.path)
return new File([ b ], hFile.name)
}
export async function Write(hFile, sContent) {
return await FS.writeFile(hFile.path, sContent, { encoding: 'utf-8' })
}