UNPKG

orgdo

Version:

Command-line tool to manage the Todo lists

113 lines (112 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ipc = require("node-ipc"); const Client_1 = require("./Client"); const notifier = require("node-notifier"); const fs = require("fs"); const cron_1 = require("cron"); const Task_1 = require("./Task"); const constants_1 = require("./constants"); ipc.config.appspace = constants_1.IPC_APP_SPACE; ipc.config.id = constants_1.IPC_SERVER_ID; ipc.config.silent = true; const DATA_FILE = process.argv[2]; const MINUTE_TO_MS = 60000; const clockSession = { timer: null, state: "idle", getRemain: null }; const cronSession = {}; try { fs.writeFileSync(constants_1.PID_FILE, process.pid); } catch (err) { } ipc.serve(() => { Client_1.default.init({ dataFile: DATA_FILE }).then(client => { client.listCrons().then(crons => { crons.forEach(model => addCronJob(model)); }); }); ipc.server.on("clock.state", (data, socket) => { ipc.server.emit(socket, "response", { type: clockSession.state, time: clockSession.getRemain ? clockSession.getRemain() : 0 }); }); ipc.server.on("clock.start", (data, socket) => { if (clockSession.timer) { clearTimeout(clockSession.timer); } clockSession.getRemain = remainGen(data.workTime); clockSession.state = "running"; clockSession.timer = setTimeout(() => { clockSession.getRemain = remainGen(data.breakTime); clockSession.state = data.next === "long" ? "longbreaking" : "shortbreaking"; notify(`You timer is up after worked ${data.workTime} minutes, take a ${data.next} break`); clockSession.timer = setTimeout(() => { clockSession.state = "idle"; notify(`Your break of ${data.breakTime} minutes is over.`); }, data.breakTime * MINUTE_TO_MS); Client_1.default.init({ dataFile: DATA_FILE }).then(client => { client.addClock({ taskId: data.taskId, time: data.workTime }); }); }, data.workTime * MINUTE_TO_MS); ipc.server.emit(socket, "response", {}); }); ipc.server.on("clock.stop", (data, socket) => { clockSession.state = "idle"; if (clockSession.timer) { clearTimeout(clockSession.timer); } ipc.server.emit(socket, "response", {}); }); ipc.server.on("cron.add", (data, socket) => { addCronJob(data); ipc.server.emit(socket, "response", {}); }); ipc.server.on("cron.update", (data, socket) => { removeCronJob(data.id); addCronJob(data); ipc.server.emit(socket, "response", {}); }); ipc.server.on("cron.update", (data, socket) => { removeCronJob(data.id); ipc.server.emit(socket, "response", {}); }); }); process.on("SIGINT", () => { fs.unlinkSync(constants_1.PID_FILE); process.exit(); }); ipc.server.start(); function notify(msg) { notifier.notify({ title: "Orgdo", message: msg }); } function remainGen(minutes) { const time = Date.now(); return () => { const now = Date.now(); return minutes * 60 - Math.ceil((now - time) / 1000); }; } function addCronJob(model) { const job = new cron_1.CronJob(model.cron, () => { Client_1.default.init({ dataFile: DATA_FILE }).then(client => { client.incId("taskId").then(id => { client.addTask(Task_1.default.create({ id, name: model.task, tags: ["cron"] })); }); }); }); cronSession[model.id] = job; job.start(); } function removeCronJob(id) { if (cronSession[id]) { cronSession[id].stop(); } }