Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"axios": "^0.21.1",
"chalk": "^3.0.0",
"codecov": "^3.8.1",
"cron-validator": "^1.2.1",
"cronstrue": "^1.110.0",
"crypto-js": "^4.0.0",
"detect-character-encoding": "^0.8.0",
"encoding-japanese": "^1.0.30",
Expand Down
71 changes: 71 additions & 0 deletions src/commands/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {Command, flags} from '@oclif/command'
import chalk from 'chalk'
import {isValidCron} from 'cron-validator'
import cronstrue from 'cronstrue'

import Logger from '../utilities/logger'
import Utilities from '../utilities/utilities'
/*
* this Commands add support for cron jobs
* */
export default class Cron extends Command {
static description = 'Cron Expressions helper and scheduler'

static RUN = 'run'
static DESCRIBE = 'desc'

static flags = {
help: flags.help({char: 'h'}),
string: flags.string({char: 's' , description: 'Cron expression'}),
describe: flags.boolean({char: 'd' , description: 'Describe cron expressions into human readable descriptions'}),
run: flags.boolean({char: 'r' , description: 'run command using cron expression'}),
}

static args = [{name: 'string'}]

// only 2 parameters required HASH_TYPE and INPUT_STRING
async run() {
const {args, flags} = this.parse(Cron)

args.string = Utilities.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
private checkParameters(flags: any, args: any) {
if (args.string === undefined || args.string === '')
Logger.error(this, 'Input string is empty or undefined')

if (!isValidCron(args.string)) {
Logger.error(this, `Invalid Cron expression : ${chalk.red(args.string)}`)
}

if (args.action === undefined || args.string === '')
Logger.error(this, 'Action empty or undefined')
}

// tslint:disable-next-line:no-unused
private evalCron(flags: any, args: any) {
// Logger.success(this, `Action: ${chalk.green(args.action)}`)
if (args.action === Cron.DESCRIBE) {
let output = cronstrue.toString(args.string)
Logger.success(this, output)
} else if (args.action === Cron.RUN) {
Logger.success(this, 'run command, coming soon...')
}
}

// tslint:disable-next-line:no-unused
private getAction(flags: any, args: any) {
if (flags.describe) // find human readable descriptions for cron
return Cron.DESCRIBE
else if (flags.run) // if run is given
return Cron.RUN
Logger.error(this, 'Invalid Or Unsupported action')
}
}
9 changes: 9 additions & 0 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export default class Utilities {
return args.string
}

// tslint:disable-next-line:no-unused
public static getInputStringFromCmd(thisRef: Command, flags: any, args: any) { //need to make it static so Crypto can use this
// if -s is not passed we will take it from args
if (flags.string) //if -s given
return flags.string
else
return args.string
}

public static writeStringToFile(thisRef: Command, filePath: string, string: string) {
if (!fs.existsSync(filePath))
Logger.info(thisRef, `Could not find file: ${chalk.yellow(filePath + ', creating new one')}`) // this will output error and exit command
Expand Down
33 changes: 33 additions & 0 deletions test/commands/cron.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {expect, test} from '@oclif/test'

describe('cron', () => {
test
.stdout()
.command(['cron'])
.exit(0)
.it('Nothing is passed', ctx => {
expect(ctx.stdout).to.contain('Invalid Or Unsupported action')
})
test
.stdout()
.command(['cron', '-d', '* * * x *'])
.exit(0)
.it('Invalid Cron Expression', ctx => {
expect(ctx.stdout).to.contain('Invalid Cron expression')
})

test
.stdout()
.command(['cron', '-d', '15 14 1 * *'])
.it('Describe cron with arg for input', ctx => {
expect(ctx.stdout).to.contain('At 02:15 PM, on day 1 of the month')
})

test
.stdout()
.command(['cron', '-d', '-s', '0 22 * * 1-5'])
.it('Describe cron with -s flag for input', ctx => {
expect(ctx.stdout).to.contain('At 10:00 PM, Monday through Friday')
})

})