Skip to content

Commit

Permalink
feat: add two subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
adelkahomolova committed Jan 21, 2020
1 parent 871f7f2 commit 9661d6b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
27 changes: 9 additions & 18 deletions src/commands/dx-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import { ScanningStrategyDetectorUtils } from '../detectors/utils/ScanningStrate
import { ServiceType } from '../detectors';

class DXScannerCommand extends Command {
// private readonly practices: IPracticeWithMetadata[];
// constructor(
// argv: any,
// config: any,
// // inject all practices registered under Types.Practice in inversify config
// @multiInject(Types.Practice) practices: IPracticeWithMetadata[],
// ) {
// super(() => {
// return this.argv;
// });
// this.practices = practices;
// }
static description = 'Scan your project for possible DX recommendations.';
static usage = ['[PATH] [OPTIONS]'];

Expand Down Expand Up @@ -56,18 +44,14 @@ class DXScannerCommand extends Command {

static args = [{ name: 'path', default: process.cwd() }];

static aliases = ['dxs', 'dxscanner'];
static aliases = ['dxs', 'dxscanner', 'run'];
static examples = ['dx-scanner', 'dx-scanner ./ --fail=high', 'dx-scanner github.com/DXHeroes/dx-scanner'];

async run() {
const { args, flags } = this.parse(DXScannerCommand);
debug('cli args')(args);
debug('cli flags')(flags);
const scanPath = args.path;
// if (flags.practices) {
// scanner.listPractices()
// process.exit(0);
// }

let authorization = flags.authorization ? flags.authorization : this.loadAuthTokenFromEnvs();
const json = flags.json;
Expand All @@ -78,7 +62,14 @@ class DXScannerCommand extends Command {

cli.action.start(`Scanning URI: ${scanPath}`);

const container = createRootContainer({ uri: scanPath, auth: authorization, json, fail, recursive: flags.recursive, ci: flags.ci });
const container = createRootContainer({
uri: scanPath,
auth: authorization,
json,
fail,
recursive: flags.recursive,
ci: flags.ci,
});
const scanner = container.get(Scanner);

if (flags.init) {
Expand Down
55 changes: 39 additions & 16 deletions src/commands/practices.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
import { Command, flags } from '@oclif/command';
import { createRootContainer } from '../inversify.config';
import { Scanner } from '../scanner';
import { PracticeImpact } from '../model';

export default class Practices extends Command {
static description = 'describe the command here';
static description = 'List all practices id with name and impact';

static examples = [
`$ example-multi-ts hello
hello world from ./src/hello.ts!
`,
];
static examples = [`$ dx-scanner practices`];

static flags = {
help: flags.help({ char: 'h' }),
// flag with a value (-n, --name=VALUE)
name: flags.string({ char: 'n', description: 'name to print' }),
// flag with no value (-f, --force)
force: flags.boolean({ char: 'f' }),
json: flags.boolean({ char: 'j', description: 'Print practices in JSON' }),
};

static args = [{ name: 'path', default: process.cwd() }];

async run() {
const { args, flags } = this.parse(Practices);

const name = flags.name || 'world';
this.log(`This is name: ${name} `);
this.log(`Log all practices`);
// if (args.file && flags.force) {
// this.log(`you input --force and --file: ${args.file}`);
// }
const scanPath = args.path;
const json = flags.json;

const container = createRootContainer({
uri: scanPath,
auth: undefined,
json,
ci: false,
});
const scanner = container.get(Scanner);

const practices = await scanner.getPractices();
const practicesToReport: PracticeToReport[] = [];
practices.forEach((practice) => {
const practiceId: string = practice.getMetadata().id;
practicesToReport.push({
[practiceId]: { name: practice.getMetadata().name, impact: practice.getMetadata().impact },
});
});
if (flags.json) {
// print practices in JSON format
console.log(JSON.stringify(practicesToReport, null, 2));
} else {
console.log(practicesToReport);
}
process.exit(0);
}
}

interface PracticeToReport {
[id: string]: {
name: string;
impact: PracticeImpact;
};
}

0 comments on commit 9661d6b

Please sign in to comment.