UNPKG

ddnsman

Version:
69 lines (68 loc) 2.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* tslint:disable no-console */ const cli_table_1 = __importDefault(require("cli-table")); const events_1 = require("events"); const ipc_1 = require("./lib/ipc"); class CommandExec extends events_1.EventEmitter { constructor() { super(); } async start(subdomain, domain, loginToken, name) { if (!loginToken) { throw new Error('Loin Token is required.'); } if (!domain || !subdomain) { throw new Error('Domain and sub domain are required.'); } this._runThenExit(async () => { name = typeof name === 'string' ? name : [subdomain, domain].join('.'); await ipc_1.sendMessage({ action: 'start', name, domain, subdomain, loginToken }); console.log(`DDNS worker for ${name} started.`); }); } async stop(name) { if (!name) { throw new Error('Name is required.'); } return this._runThenExit(async () => { await ipc_1.sendMessage({ action: 'stop', name }); console.log(`DDNS ${name} client stopped.`); }); } async list() { this._runThenExit(async () => { const domainList = await ipc_1.sendMessage({ action: 'list' }); const table = new cli_table_1.default({ head: ['Name', 'Status', 'Subdomain', 'Domain', 'IP'] }); domainList.forEach((item) => { table.push(item); }); console.log(table.toString()); }); } async _runThenExit(command) { command() .catch((err) => { console.error(err); process.exit(-1); }) .finally(() => { process.exit(0); }); } } exports.CommandExec = CommandExec;