Skip to content

Commit

Permalink
feat(core): CLI changed to multi-command
Browse files Browse the repository at this point in the history
- small cleanup

BREAKING CHANGE: now exists commands such as init, practices and run
  • Loading branch information
prokopsimek committed Jan 28, 2020
1 parent 9d8a8f6 commit cbf03b0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 54 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<details open>
Expand Down
2 changes: 2 additions & 0 deletions bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
8 changes: 3 additions & 5 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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);
}
}
35 changes: 13 additions & 22 deletions src/commands/practices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -48,9 +45,3 @@ export default class Practices extends Command {
process.exit(0);
}
}

interface PracticeToReport {
id: string;
name: string;
impact: PracticeImpact;
}
13 changes: 4 additions & 9 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
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';
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:
Expand All @@ -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;
Expand Down Expand Up @@ -93,7 +90,7 @@ class DXScannerCommand extends Command {
console.info('Scan duration %ds.', hrend[0]);

if (scanResult.shouldExitOnEnd) {
process.exit(1);
this.exit(1);
}
}

Expand All @@ -106,5 +103,3 @@ class DXScannerCommand extends Command {
return ev.DX_GIT_SERVICE_TOKEN || ev.GITHUB_TOKEN;
};
}

export = DXScannerCommand;
4 changes: 2 additions & 2 deletions src/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand All @@ -328,7 +328,7 @@ export class Scanner {
}
}

async listPractices(): Promise<IPracticeWithMetadata[]> {
listPractices(): IPracticeWithMetadata[] {
return ScannerUtils.sortAlphabetically(this.practices);
}
}
Expand Down

0 comments on commit cbf03b0

Please sign in to comment.