@codingtools/cdt
Version:
CLI for Developers
115 lines • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const cron_validate_1 = tslib_1.__importDefault(require("cron-validate"));
const cronstrue_1 = tslib_1.__importDefault(require("cronstrue"));
const node_cron_1 = tslib_1.__importDefault(require("node-cron"));
const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
const logger_1 = tslib_1.__importDefault(require("../utilities/logger"));
const utilities_1 = tslib_1.__importDefault(require("../utilities/utilities"));
// https://github.com/Airfooox/cron-validate
/*
* this Commands add support for cron jobs
* */
class Cron extends command_1.Command {
// only 2 parameters required HASH_TYPE and INPUT_STRING
async run() {
const { args, flags } = this.parse(Cron);
args.string = utilities_1.default.getInputStringFromCmd(this, flags, args); // from either -s or args
args.action = this.getAction(flags, args); //by default let it be sha1
//check params after evaluating all
this.checkParameters(flags, args);
this.evalCron(flags, args);
}
// to check required parameters passed or not
// tslint:disable-next-line:no-unused
checkParameters(flags, args) {
if (args.string === undefined || args.string === '')
logger_1.default.error(this, 'Input string is empty or undefined');
// for more present settings check - https://github.com/Airfooox/cron-validate
let preset = Cron.PRESET_DEFAULT;
// if we have 6 inputs enable seconds as an option
let charCount = (args.string.match(/\s+/g) || []).length + 1;
if (charCount === 6) {
preset = Cron.PRESET_DEFAULT_WITH_SECONDS;
}
else if (charCount === 7) {
preset = Cron.PRESET_DEFAULT_WITH_SECONDS_AND_YEARS;
}
const cronResult = cron_validate_1.default(args.string, preset);
if (!cronResult.isValid()) {
const errorValue = cronResult.getError();
// The error value contains an array of strings, which represent the cron validation errors.
logger_1.default.error(this, `Invalid Cron expression : ${chalk_1.default.yellow(errorValue)}`);
}
if (args.action === undefined || args.string === '')
logger_1.default.error(this, 'Action empty or undefined');
}
// tslint:disable-next-line:no-unused
evalCron(flags, args) {
// Logger.success(this, `Action: ${chalk.green(args.action)}`)
let cronSchedule = cronstrue_1.default.toString(args.string);
if (args.action === Cron.DESCRIBE) {
logger_1.default.success(this, cronSchedule);
}
else if (args.action === Cron.RUN) {
logger_1.default.success(this, `running ${chalk_1.default.yellow(flags.run)}, ${chalk_1.default.green(cronSchedule)}, use ${chalk_1.default.yellow('Ctrl+C')} to exit.`);
node_cron_1.default.schedule(args.string, function () {
shelljs_1.default.exec(flags.run);
});
}
}
// tslint:disable-next-line:no-unused
getAction(flags, args) {
if (flags.describe) // find human readable descriptions for cron
return Cron.DESCRIBE;
else if (flags.run) // if run is given
return Cron.RUN;
logger_1.default.error(this, 'Invalid Or Unsupported action');
}
}
exports.default = Cron;
Cron.description = 'Cron Expressions helper and scheduler';
Cron.RUN = 'run';
Cron.DESCRIBE = 'desc';
Cron.PRESET_DEFAULT = {
preset: 'default',
override: {
useAliases: true,
useBlankDay: true,
}
};
Cron.PRESET_DEFAULT_WITH_SECONDS = {
preset: 'default',
override: {
useSeconds: true,
useAliases: true,
useBlankDay: true,
}
};
Cron.PRESET_DEFAULT_WITH_SECONDS_AND_YEARS = {
preset: 'default',
override: {
useSeconds: true,
useYears: true,
useAliases: true,
useBlankDay: true,
}
};
// adding more options to have bit loosen validation rules
Cron.VALIDATOR_OPTIONS = {
seconds: true,
allowBlankDay: true,
alias: true,
allowSevenAsSunday: true
};
Cron.flags = {
help: command_1.flags.help({ char: 'h' }),
string: command_1.flags.string({ char: 's', description: 'Cron expression' }),
describe: command_1.flags.boolean({ char: 'd', description: 'Describe cron expressions into human readable descriptions' }),
run: command_1.flags.string({ char: 'r', description: 'run command using cron expression' }),
};
Cron.args = [{ name: 'string' }];
//# sourceMappingURL=cron.js.map