From c619013e5e18a35208a97dc831037dcbdc1857c4 Mon Sep 17 00:00:00 2001 From: ashish Date: Sat, 20 Mar 2021 22:02:51 +0530 Subject: [PATCH] cdt-142 Use ShellJs to create utilities using shell --- package-lock.json | 80 +++++++++++++++++++++++++++++++++++++++ package.json | 2 + src/commands/run.ts | 44 +++++++++++++++++++++ test/commands/run.test.ts | 26 +++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 src/commands/run.ts create mode 100644 test/commands/run.test.ts diff --git a/package-lock.json b/package-lock.json index ceb09e4..76499d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "moment-timezone": "^0.5.32", "nyc": "^15.1.0", "ora": "^5.2.0", + "shelljs": "^0.8.4", "signale": "^1.4.0", "snyk": "^1.425.4", "test": "^0.6.0", @@ -47,6 +48,7 @@ "@types/crypto-js": "^3.1.43", "@types/mocha": "^7.0.0", "@types/node": "13.9.3", + "@types/shelljs": "^0.8.8", "@types/signale": "^1.2.1", "chai": "^4.2.0", "globby": "^10.0.1", @@ -2112,6 +2114,16 @@ "@types/node": "*" } }, + "node_modules/@types/shelljs": { + "version": "0.8.8", + "resolved": "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.8.8.tgz", + "integrity": "sha512-lD3LWdg6j8r0VRBFahJVaxoW0SIcswxKaFUrmKl33RJVeeoNYQAz4uqCJ5Z6v4oIBOsC5GozX+I5SorIKiTcQA==", + "dev": true, + "dependencies": { + "@types/glob": "*", + "@types/node": "*" + } + }, "node_modules/@types/signale": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@types/signale/-/signale-1.2.1.tgz", @@ -4822,6 +4834,14 @@ "node": ">=8" } }, + "node_modules/interpret": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.4.0.tgz", + "integrity": "sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/inversify": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/inversify/-/inversify-5.0.1.tgz", @@ -7381,6 +7401,17 @@ "node": ">= 8" } }, + "node_modules/rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", + "dependencies": { + "resolve": "^1.1.6" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/redeyed": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/redeyed/-/redeyed-2.1.1.tgz", @@ -7618,6 +7649,22 @@ "node": ">=0.10.0" } }, + "node_modules/shelljs": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.4.tgz", + "integrity": "sha512-7gk3UZ9kOfPLIAbslLzyWeGiEqx9e3rxwZM0KE6EL8GlGwjym9Mrlx5/p33bWTu9YG6vcS4MBxYZDHYr5lr8BQ==", + "dependencies": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + }, + "bin": { + "shjs": "bin/shjs" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/signal-exit": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", @@ -11713,6 +11760,16 @@ "@types/node": "*" } }, + "@types/shelljs": { + "version": "0.8.8", + "resolved": "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.8.8.tgz", + "integrity": "sha512-lD3LWdg6j8r0VRBFahJVaxoW0SIcswxKaFUrmKl33RJVeeoNYQAz4uqCJ5Z6v4oIBOsC5GozX+I5SorIKiTcQA==", + "dev": true, + "requires": { + "@types/glob": "*", + "@types/node": "*" + } + }, "@types/signale": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@types/signale/-/signale-1.2.1.tgz", @@ -13846,6 +13903,11 @@ } } }, + "interpret": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.4.0.tgz", + "integrity": "sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==" + }, "inversify": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/inversify/-/inversify-5.0.1.tgz", @@ -15900,6 +15962,14 @@ "picomatch": "^2.0.4" } }, + "rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", + "requires": { + "resolve": "^1.1.6" + } + }, "redeyed": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/redeyed/-/redeyed-2.1.1.tgz", @@ -16089,6 +16159,16 @@ "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" }, + "shelljs": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.4.tgz", + "integrity": "sha512-7gk3UZ9kOfPLIAbslLzyWeGiEqx9e3rxwZM0KE6EL8GlGwjym9Mrlx5/p33bWTu9YG6vcS4MBxYZDHYr5lr8BQ==", + "requires": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + } + }, "signal-exit": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", diff --git a/package.json b/package.json index 4dbe4a5..5116ac9 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "moment-timezone": "^0.5.32", "nyc": "^15.1.0", "ora": "^5.2.0", + "shelljs": "^0.8.4", "signale": "^1.4.0", "snyk": "^1.425.4", "test": "^0.6.0", @@ -45,6 +46,7 @@ "@types/crypto-js": "^3.1.43", "@types/mocha": "^7.0.0", "@types/node": "13.9.3", + "@types/shelljs": "^0.8.8", "@types/signale": "^1.2.1", "chai": "^4.2.0", "globby": "^10.0.1", diff --git a/src/commands/run.ts b/src/commands/run.ts new file mode 100644 index 0000000..2ce4253 --- /dev/null +++ b/src/commands/run.ts @@ -0,0 +1,44 @@ +import {Command, flags} from '@oclif/command' +import chalk from 'chalk' +import shelljs from 'shelljs' + +import Logger from '../utilities/logger' +import Utilities from '../utilities/utilities' +/* +* this Commands add support for cron jobs +* */ +export default class Run extends Command { + static description = 'Cron Expressions helper and scheduler' + + static flags = { + help: flags.help({char: 'h'}), + string: flags.string({char: 's' , description: 'unix command'}), + } + + static args = [{name: 'string'}] + + // only 2 parameters required HASH_TYPE and INPUT_STRING + async run() { + const {args, flags} = this.parse(Run) + + args.string = Utilities.getInputStringFromCmd(this, flags, args) // from either -s or args + + //check params after evaluating all + this.checkParameters(flags, args) + this.evalRun(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, 'Command empty or undefined') + } + + // tslint:disable-next-line:no-unused + private evalRun(flags: any, args: any) { + Logger.success(this, `running: ${chalk.green(args.string)}`) + shelljs.exec(args.string) + } + +} diff --git a/test/commands/run.test.ts b/test/commands/run.test.ts new file mode 100644 index 0000000..a224521 --- /dev/null +++ b/test/commands/run.test.ts @@ -0,0 +1,26 @@ +import {expect, test} from '@oclif/test' + +describe('run', () => { + test + .stdout() + .command(['run']) + .exit(0) + .it('Nothing is passed', ctx => { + expect(ctx.stdout).to.contain('Command empty or undefined') + }) + + test + .stdout() + .command(['run', 'echo test']) + .it('Run command with arg for input', ctx => { + expect(ctx.stdout).to.contain('running: echo test') + }) + + test + .stdout() + .command(['run', '-s', 'echo test']) + .it('Describe cron with -s flag for input', ctx => { + expect(ctx.stdout).to.contain('running: echo test') + }) + +})