@hp4k1h5/terminordle
Version:
> multiplayer [wordle](https://www.powerlanguage.co.uk/wordle/) clone in your terminal
52 lines (51 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Log = exports.fp = void 0;
//@ts-strict
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const process = require("process");
const dirname = process.cwd();
function fp(relPath) {
if (path.isAbsolute(relPath)) {
return relPath;
}
return path.resolve(path.join(dirname, relPath));
}
exports.fp = fp;
class Log {
constructor(relPath, overwrite = true) {
this.filepath = fp(relPath);
this.logger = (function (_this) {
if (!fs.existsSync(_this.filepath) || overwrite) {
const dirname = path.dirname(_this.filepath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
fs.openSync(_this.filepath, 'w');
fs.writeFileSync(_this.filepath, '');
}
return fs.createWriteStream(_this.filepath, {
flags: overwrite ? 'r+' : 'a+',
});
})(this);
this.reader = readline.createInterface({
input: fs.createReadStream(this.filepath, {}),
});
}
log(line) {
line.timestamp = new Date();
this.logger.write(JSON.stringify(line) + '\n');
}
read() {
async function* read(_this) {
for await (let line of _this.reader) {
line = JSON.parse(line);
yield line;
}
}
return read(this);
}
}
exports.Log = Log;