UNPKG

orgdo

Version:

Command-line tool to manage the Todo lists

215 lines (214 loc) 6.92 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const low = require("lowdb"); const FileAsync = require("lowdb/adapters/FileAsync"); const _ = require("lodash"); const os = require("os"); const mkdirp = require("mkdirp"); const constants_1 = require("./constants"); mkdirp.sync(constants_1.DATA_DIR); const Task_1 = require("./Task"); const IPC_1 = require("./IPC"); const Clock_1 = require("./Clock"); class Client { static init(options = { dataFile: constants_1.DATA_FILE }) { return __awaiter(this, void 0, void 0, function* () { const client = new Client(options); try { client.db = yield low(new FileAsync(options.dataFile)); yield client.db.defaults(_.cloneDeep(constants_1.DEFAULT_DB)).write(); } catch (err) { print(err.message); } return client; }); } constructor(options) { this.ipc = new IPC_1.default(options.dataFile); } incId(name) { return __awaiter(this, void 0, void 0, function* () { const id = this.db.get(name).value() + 1; yield this.db.set(name, id).write(); return id; }); } listTasks(filter) { return __awaiter(this, void 0, void 0, function* () { const tasks = yield this.db.get("tasks").value(); return tasks .map(task => Task_1.default.fromJSON(task)) .filter(task => filter(task)) .sort(Task_1.default.sort); }); } addTask(task) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("tasks") .push(task) .write(); }); } updateTask(task) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("tasks") .find({ id: task.id }) .assign(task) .write(); }); } getTask(id) { return __awaiter(this, void 0, void 0, function* () { const data = this.db .get("tasks") .find(task => task.id === id) .value(); if (!data) { throw new Error(`Task ${id} not found`); } return Task_1.default.fromJSON(data); }); } removeTask(id) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("tasks") .remove({ id }) .write(); }); } startClock(args) { return __awaiter(this, void 0, void 0, function* () { return this.ipc.exec("clock.start", args); }); } stopClock() { return __awaiter(this, void 0, void 0, function* () { if (!this.ipc.isRunning()) { return; } const state = yield this.getClockState(); if (state.type === "idle") { return; } return this.ipc.exec("clock.stop"); }); } getClockSettings() { return __awaiter(this, void 0, void 0, function* () { return this.db.get("clockSettings").value(); }); } updateClockSettings(settings) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("clockSettings") .assign(settings) .write(); }); } getClockState() { return __awaiter(this, void 0, void 0, function* () { const data = yield this.ipc.exec("clock.state"); return data; }); } listClocks(filter) { return __awaiter(this, void 0, void 0, function* () { const clocks = yield this.db.get("clocks").value(); return (clocks.map(clock => Clock_1.newClockModel(clock)).filter(clock => filter(clock))); }); } addClock(data) { return __awaiter(this, void 0, void 0, function* () { const id = yield this.incId("clockId"); const clock = Object.assign({ id, createdAt: new Date() }, data); yield this.db .get("clocks") .push(clock) .write(); return clock; }); } addCron(data) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("crons") .push(data) .write(); yield this.ipc.exec("cron.add", data); }); } getCron(id) { return __awaiter(this, void 0, void 0, function* () { const data = this.db .get("crons") .find(cron => cron.id === id) .value(); if (!data) { throw new Error(`Cron ${id} not found`); } return data; }); } updateCron(cron) { return __awaiter(this, void 0, void 0, function* () { yield this.db .get("crons") .find({ id: cron.id }) .assign(cron) .write(); yield this.ipc.exec("cron.update", cron); }); } removeCron(id) { return __awaiter(this, void 0, void 0, function* () { try { yield this.getCron(id); } catch (err) { return; } yield this.db .get("crons") .remove({ id }) .write(); yield this.ipc.exec("cron.remove", { id }); }); } listCrons() { return __awaiter(this, void 0, void 0, function* () { const crons = yield this.db.get("crons").value(); return crons; }); } } exports.default = Client; function print(str, eol = true) { process.stdout.write(str); if (eol) { process.stdout.write(os.EOL); } } exports.print = print; function printErrorAndExit(err, exitCode = 1) { if (err instanceof Error) { print(err.message); } else { print(err.toString()); } process.exit(1); } exports.printErrorAndExit = printErrorAndExit;