Skip to content

Commit 04cab9d

Browse files
authored
Merge pull request #186 from codingtools/cdt-141
cdt-141 Add Cron Job support
2 parents 363affb + d14cf8e commit 04cab9d

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"axios": "^0.21.1",
2121
"chalk": "^3.0.0",
2222
"codecov": "^3.8.1",
23+
"cron-validator": "^1.2.1",
24+
"cronstrue": "^1.110.0",
2325
"crypto-js": "^4.0.0",
2426
"detect-character-encoding": "^0.8.0",
2527
"encoding-japanese": "^1.0.30",

src/commands/cron.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

src/utilities/utilities.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ export default class Utilities {
4040
return args.string
4141
}
4242

43+
// tslint:disable-next-line:no-unused
44+
public static getInputStringFromCmd(thisRef: Command, flags: any, args: any) { //need to make it static so Crypto can use this
45+
// if -s is not passed we will take it from args
46+
if (flags.string) //if -s given
47+
return flags.string
48+
else
49+
return args.string
50+
}
51+
4352
public static writeStringToFile(thisRef: Command, filePath: string, string: string) {
4453
if (!fs.existsSync(filePath))
4554
Logger.info(thisRef, `Could not find file: ${chalk.yellow(filePath + ', creating new one')}`) // this will output error and exit command

test/commands/cron.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {expect, test} from '@oclif/test'
2+
3+
describe('cron', () => {
4+
test
5+
.stdout()
6+
.command(['cron'])
7+
.exit(0)
8+
.it('Nothing is passed', ctx => {
9+
expect(ctx.stdout).to.contain('Invalid Or Unsupported action')
10+
})
11+
test
12+
.stdout()
13+
.command(['cron', '-d', '* * * x *'])
14+
.exit(0)
15+
.it('Invalid Cron Expression', ctx => {
16+
expect(ctx.stdout).to.contain('Invalid Cron expression')
17+
})
18+
19+
test
20+
.stdout()
21+
.command(['cron', '-d', '15 14 1 * *'])
22+
.it('Describe cron with arg for input', ctx => {
23+
expect(ctx.stdout).to.contain('At 02:15 PM, on day 1 of the month')
24+
})
25+
26+
test
27+
.stdout()
28+
.command(['cron', '-d', '-s', '0 22 * * 1-5'])
29+
.it('Describe cron with -s flag for input', ctx => {
30+
expect(ctx.stdout).to.contain('At 10:00 PM, Monday through Friday')
31+
})
32+
33+
})

0 commit comments

Comments
 (0)