From cbf03b016d6569c10638c9177a96cfaa962dcc77 Mon Sep 17 00:00:00 2001 From: Prokop Simek Date: Tue, 28 Jan 2020 09:32:34 +0100 Subject: [PATCH] feat(core): CLI changed to multi-command - small cleanup BREAKING CHANGE: now exists commands such as init, practices and run --- .travis.yml | 14 +++++++------- README.md | 13 ++++--------- bin/run | 2 ++ src/commands/init.ts | 8 +++----- src/commands/practices.ts | 35 +++++++++++++---------------------- src/commands/run.ts | 13 ++++--------- src/scanner/Scanner.ts | 4 ++-- 7 files changed, 35 insertions(+), 54 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3a206389f..e3d0f2e20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,9 +38,9 @@ notifications: script: - yarn build - yarn test --coverage - - bin/run -v + - bin/run - yarn test:codecov:upload - - bin/run . --fail=high + - bin/run run . --fail=high jobs: include: @@ -52,11 +52,11 @@ jobs: - stage: runtime test if: branch = master scripts: - - bin/run -v - - bin/run https://github.com/DXHeroes/dx-scanner - - bin/run https://github.com/moment/luxon - - bin/run https://github.com/intoli/remote-browser - - bin/run + - bin/run run + - bin/run run https://github.com/DXHeroes/dx-scanner + - bin/run run https://github.com/moment/luxon + - bin/run run https://github.com/intoli/remote-browser + - bin/run run os: windows - os: linux - stage: release diff --git a/README.md b/README.md index d92db6743..f1e1d7fd0 100644 --- a/README.md +++ b/README.md @@ -52,22 +52,17 @@ USAGE OPTIONS -a, --authorization=authorization Credentials to the repository. (in format "token" or "username:token"; can be set as ENV variable DX_GIT_SERVICE_TOKEN) - -h, --help Help - -i, --init Initialize DX Scanner configuration + -h, --help show CLI help -j, --json Print report in JSON -r, --recursive Scan all components recursively in all sub folders -v, --version Output the version number --ci CI mode --fail=high|medium|small|off|all [default: high] Run scanner in failure mode. Exits process with code 1 for any non-practicing condition of given level. -ALIASES - $ dx-scanner dxs - $ dx-scanner dxscanner - EXAMPLES - dx-scanner - dx-scanner ./ --fail=high - dx-scanner github.com/DXHeroes/dx-scanner + dx-scanner run + dx-scanner run ./ --fail=high + dx-scanner run github.com/DXHeroes/dx-scanner ```
diff --git a/bin/run b/bin/run index ee3701af0..4c7b03a36 100755 --- a/bin/run +++ b/bin/run @@ -8,8 +8,10 @@ const dev = fs.existsSync(project); if (dev) { require('ts-node').register({ project }); + console.warn("WARNING: You're running DX Scanner in development mode.") } require(`../${dev ? 'src' : 'lib'}`) .run() + .then(require('@oclif/command/flush')) .catch(require('@oclif/errors/handle')); diff --git a/src/commands/init.ts b/src/commands/init.ts index ae667d5c3..5b6e207ee 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,18 +1,16 @@ import { Command, flags } from '@oclif/command'; import { createRootContainer } from '../inversify.config'; import { Scanner } from '../scanner'; +import { PracticeImpact } from '../model'; export default class Init extends Command { static description = 'Initialize DX Scanner configuration.'; - static examples = [`$ dx-scanner init`]; - static flags = { help: flags.help({ char: 'h' }), }; async run() { - this.parse(Init); const scanPath = process.cwd(); const container = createRootContainer({ @@ -21,11 +19,11 @@ export default class Init extends Command { auth: undefined, ci: false, recursive: false, - fail: 'all', + fail: PracticeImpact.off, }); const scanner = container.get(Scanner); await scanner.init(scanPath); - process.exit(0); + this.exit(0); } } diff --git a/src/commands/practices.ts b/src/commands/practices.ts index 816c73faa..24d7931cc 100644 --- a/src/commands/practices.ts +++ b/src/commands/practices.ts @@ -4,41 +4,38 @@ import { Scanner } from '../scanner'; import { PracticeImpact } from '../model'; import { ReporterData } from '../reporters/ReporterData'; -export default class Practices extends Command { +export default class PracticesCommand extends Command { static description = 'List all practices id with name and impact.'; - static examples = [`$ dx-scanner practices`]; - static flags = { help: flags.help({ char: 'h' }), json: flags.boolean({ char: 'j', description: 'Print practices in JSON' }), }; async run() { - const { flags } = this.parse(Practices); - + const { flags } = this.parse(PracticesCommand); const scanPath = process.cwd(); - const json = flags.json; const container = createRootContainer({ uri: scanPath, - json, + json: flags.json, auth: undefined, ci: false, recursive: false, - fail: 'all', + fail: PracticeImpact.off, }); + const scanner = container.get(Scanner); - const practices = await scanner.listPractices(); - const practicesToReport: PracticeToReport[] = []; - practices.forEach((practice) => { - practicesToReport.push({ - id: practice.getMetadata().id, - name: practice.getMetadata().name, - impact: practice.getMetadata().impact, - }); + const practices = scanner.listPractices(); + const practicesToReport = practices.map((p) => { + return { + id: p.getMetadata().id, + name: p.getMetadata().name, + impact: p.getMetadata().impact, + }; }); + if (flags.json) { // print practices in JSON format console.log(JSON.stringify(practicesToReport, null, 2)); @@ -48,9 +45,3 @@ export default class Practices extends Command { process.exit(0); } } - -interface PracticeToReport { - id: string; - name: string; - impact: PracticeImpact; -} diff --git a/src/commands/run.ts b/src/commands/run.ts index 98da13b12..a53c87611 100644 --- a/src/commands/run.ts +++ b/src/commands/run.ts @@ -2,7 +2,6 @@ import { Command, flags } from '@oclif/command'; import cli from 'cli-ux'; import debug from 'debug'; -import 'reflect-metadata'; import updateNotifier from 'update-notifier'; import { PracticeImpact } from '../model'; import { createRootContainer } from '../inversify.config'; @@ -10,15 +9,13 @@ import { Scanner } from '../scanner'; import { ScanningStrategyDetectorUtils } from '../detectors/utils/ScanningStrategyDetectorUtils'; import { ServiceType } from '../detectors'; -class DXScannerCommand extends Command { +export class RunCommand extends Command { static description = 'Scan your project for possible DX recommendations.'; static usage = ['[PATH] [OPTIONS]']; static flags = { - // add --version flag to show CLI version version: flags.version({ char: 'v', description: 'Output the version number' }), - help: flags.help({ char: 'h', description: 'Help' }), - // flag with a value (-n, --name=VALUE) + help: flags.help({ char: 'h' }), authorization: flags.string({ char: 'a', description: @@ -42,7 +39,7 @@ class DXScannerCommand extends Command { static examples = ['dx-scanner run', 'dx-scanner run ./ --fail=high', 'dx-scanner run github.com/DXHeroes/dx-scanner']; async run() { - const { args, flags } = this.parse(DXScannerCommand); + const { args, flags } = this.parse(RunCommand); debug('cli args')(args); debug('cli flags')(flags); const scanPath = args.path; @@ -93,7 +90,7 @@ class DXScannerCommand extends Command { console.info('Scan duration %ds.', hrend[0]); if (scanResult.shouldExitOnEnd) { - process.exit(1); + this.exit(1); } } @@ -106,5 +103,3 @@ class DXScannerCommand extends Command { return ev.DX_GIT_SERVICE_TOKEN || ev.GITHUB_TOKEN; }; } - -export = DXScannerCommand; diff --git a/src/scanner/Scanner.ts b/src/scanner/Scanner.ts index ce1e38db9..6ab663bd1 100644 --- a/src/scanner/Scanner.ts +++ b/src/scanner/Scanner.ts @@ -317,7 +317,7 @@ export class Scanner { private async createConfiguration(filePath: string) { let yamlInitContent = `# practices:`; - for (const practice of ScannerUtils.sortAlphabetically(this.practices)) { + for (const practice of this.listPractices()) { const dataObject = practice.getMetadata(); yamlInitContent += `\n# ${dataObject.id}: ${dataObject.impact}`; } @@ -328,7 +328,7 @@ export class Scanner { } } - async listPractices(): Promise { + listPractices(): IPracticeWithMetadata[] { return ScannerUtils.sortAlphabetically(this.practices); } }