@bitblit/asyncglk
Version:
A Typescript Glk library
77 lines (73 loc) • 2.01 kB
JavaScript
/*
Cheap implementation of AsyncDialog
===================================
Copyright (c) 2024 Dannii Willis
MIT licenced
https://github.com/curiousdannii/asyncglk
*/
import fs from 'fs';
import fs_async from 'fs/promises';
import MuteStream from 'mute-stream';
import os from 'os';
import { path_native_to_posix, path_posix_to_native } from '../common/common.js';
import { get_stdio } from '../../glkote/cheap/stdio.js';
export class CheapAsyncDialog {
'async' = true;
rl;
stdout;
constructor() {
const cheap_stdio = get_stdio();
this.rl = cheap_stdio.rl;
this.stdout = cheap_stdio.stdout;
}
async init(_options) {
// Anything to do here?
}
async delete(path) {
try {
fs.unlinkSync(path_posix_to_native(path));
}
catch { }
}
async exists(path) {
try {
await fs_async.access(path_posix_to_native(path), fs.constants.F_OK);
return true;
}
catch {
return false;
}
}
get_dirs() {
const cwd = path_native_to_posix(process.cwd());
return {
storyfile: cwd,
system_cwd: cwd,
temp: path_native_to_posix(os.tmpdir()),
working: cwd,
};
}
prompt(extension, _save) {
this.stdout.write('\n');
return new Promise(resolve => {
this.rl.question('Please enter a file name (without an extension): ', path => {
resolve(path ? `${path}.${extension}` : null);
});
});
}
read(path) {
return fs_async.readFile(path_posix_to_native(path))
.catch(() => null);
}
set_storyfile_dir(path) {
return {
storyfile: path,
working: path,
};
}
async write(files) {
for (const [path, data] of Object.entries(files)) {
fs.writeFileSync(path_posix_to_native(path), data, {}); //flush: true})
}
}
}