|
| 1 | +import {Command, flags} from '@oclif/command' |
| 2 | +import chalk from 'chalk' |
| 3 | +import {isValidCron} from 'cron-validator' |
| 4 | +import cronstrue from 'cronstrue' |
| 5 | + |
| 6 | +import Logger from '../utilities/logger' |
| 7 | +import Utilities from '../utilities/utilities' |
| 8 | +/* |
| 9 | +* this Commands add support for cron jobs |
| 10 | +* */ |
| 11 | +export default class Cron extends Command { |
| 12 | + static description = 'Cron Expressions helper and scheduler' |
| 13 | + |
| 14 | + static RUN = 'run' |
| 15 | + static DESCRIBE = 'desc' |
| 16 | + |
| 17 | + static flags = { |
| 18 | + help: flags.help({char: 'h'}), |
| 19 | + string: flags.string({char: 's' , description: 'Cron expression'}), |
| 20 | + describe: flags.boolean({char: 'd' , description: 'Describe cron expressions into human readable descriptions'}), |
| 21 | + run: flags.boolean({char: 'r' , description: 'run command using cron expression'}), |
| 22 | + } |
| 23 | + |
| 24 | + static args = [{name: 'string'}] |
| 25 | + |
| 26 | + // only 2 parameters required HASH_TYPE and INPUT_STRING |
| 27 | + async run() { |
| 28 | + const {args, flags} = this.parse(Cron) |
| 29 | + |
| 30 | + args.string = Utilities.getInputStringFromCmd(this, flags, args) // from either -s or args |
| 31 | + args.action = this.getAction(flags, args) //by default let it be sha1 |
| 32 | + |
| 33 | + //check params after evaluating all |
| 34 | + this.checkParameters(flags, args) |
| 35 | + this.evalCron(flags, args) |
| 36 | + } |
| 37 | + |
| 38 | + // to check required parameters passed or not |
| 39 | + // tslint:disable-next-line:no-unused |
| 40 | + private checkParameters(flags: any, args: any) { |
| 41 | + if (args.string === undefined || args.string === '') |
| 42 | + Logger.error(this, 'Input string is empty or undefined') |
| 43 | + |
| 44 | + if (!isValidCron(args.string)) { |
| 45 | + Logger.error(this, `Invalid Cron expression : ${chalk.red(args.string)}`) |
| 46 | + } |
| 47 | + |
| 48 | + if (args.action === undefined || args.string === '') |
| 49 | + Logger.error(this, 'Action empty or undefined') |
| 50 | + } |
| 51 | + |
| 52 | + // tslint:disable-next-line:no-unused |
| 53 | + private evalCron(flags: any, args: any) { |
| 54 | + // Logger.success(this, `Action: ${chalk.green(args.action)}`) |
| 55 | + if (args.action === Cron.DESCRIBE) { |
| 56 | + let output = cronstrue.toString(args.string) |
| 57 | + Logger.success(this, output) |
| 58 | + } else if (args.action === Cron.RUN) { |
| 59 | + Logger.success(this, 'run command, coming soon...') |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // tslint:disable-next-line:no-unused |
| 64 | + private getAction(flags: any, args: any) { |
| 65 | + if (flags.describe) // find human readable descriptions for cron |
| 66 | + return Cron.DESCRIBE |
| 67 | + else if (flags.run) // if run is given |
| 68 | + return Cron.RUN |
| 69 | + Logger.error(this, 'Invalid Or Unsupported action') |
| 70 | + } |
| 71 | +} |
0 commit comments