orgdo
Version:
Command-line tool to manage the Todo lists
112 lines (111 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Client_1 = require("../../Client");
const Cronjob_1 = require("../../Cronjob");
const utits_1 = require("../../utits");
exports.command = "cron";
exports.describe = "Manage cronjobs";
function builder(cmd) {
return cmd
.command({
command: "add <cron>",
describe: "Add cron job",
builder: (subcmd) => {
return subcmd
.option("task", {
describe: "Name of task",
type: "string"
})
.positional("cron", {
describe: "Cron pattern",
type: "string"
});
},
handler: (options) => {
Client_1.default.init().then(client => {
new Cronjob_1.default(client)
.add(options)
.then(cron => {
Client_1.print(`Add cron ${cron.id}.`);
})
.catch(err => Client_1.printErrorAndExit(err));
});
}
})
.command({
command: "update <id>",
describe: "Update cron job",
builder: (subcmd) => {
return subcmd
.option("task", {
describe: "Name of task",
type: "string"
})
.option("cron", {
describe: "Cron pattern",
type: "string"
})
.positional("id", {
describe: "Id of cronjob",
type: "number"
});
},
handler: (options) => {
Client_1.default.init().then(client => {
new Cronjob_1.default(client)
.update(options)
.then(cron => {
Client_1.print(`Update cron ${cron.id}.`);
})
.catch(err => Client_1.printErrorAndExit(err));
});
}
})
.command({
command: ["rm <id>", "remove", "del", "delete"],
describe: "Remove cron job",
builder: (subcmd) => {
return subcmd.positional("id", {
describe: "Id of cronjob",
type: "number"
});
},
handler: (options) => {
Client_1.default.init().then(client => {
new Cronjob_1.default(client)
.remove(options.id)
.then(() => {
Client_1.print(`Remove cron ${options.id}.`);
})
.catch(err => Client_1.printErrorAndExit(err));
});
}
})
.command({
command: ["list", "ls"],
describe: "List cron jobs",
builder: {},
handler: (options) => {
Client_1.default.init().then(client => {
new Cronjob_1.default(client)
.list()
.then(models => {
if (models.length === 0) {
Client_1.print("No crons.");
return;
}
const cronColumnWidth = Math.max(...models.map(model => model.cron.length));
models.forEach(model => Client_1.print(renderCron(model, cronColumnWidth)));
})
.catch(err => Client_1.printErrorAndExit(err));
});
}
})
.demandCommand(1, `Run 'orgdo cron <subcommand> --help' for more information on a command.`);
}
exports.builder = builder;
function handler(argv) { }
exports.handler = handler;
function renderCron(model, cronColumnWidth) {
return `${utits_1.paddingRight(String(model.id), 3)}\t${utits_1.paddingRight(model.cron, cronColumnWidth)}\t${model.task}`;
}