From 3e3736192dfc3329d2311a91f9ccf1f2394de046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vlas=C3=A1k?= Date: Thu, 27 Aug 2020 13:51:09 +0200 Subject: [PATCH 1/2] fix: run fix command only on local folder --- src/contexts/scanner/scannerContextBinding.ts | 11 ++++++++--- src/scanner/Scanner.ts | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/contexts/scanner/scannerContextBinding.ts b/src/contexts/scanner/scannerContextBinding.ts index 64f5ed5d2..92c875344 100644 --- a/src/contexts/scanner/scannerContextBinding.ts +++ b/src/contexts/scanner/scannerContextBinding.ts @@ -37,7 +37,7 @@ const createScanningContainer = (scanningStrategy: ScanningStrategy, discoveryCo bindLanguageContext(container); bindFileAccess(scanningStrategy, container); - bindReporters(container, args, scanningStrategy.accessType); + bindReporters(container, args, scanningStrategy); bindCollectors(container, args, scanningStrategy.accessType); container.bind(ScannerContext).toSelf(); return container; @@ -71,12 +71,17 @@ const bindCollectors = (container: Container, args: ArgumentsProvider, accessTyp } }; -const bindReporters = (container: Container, args: ArgumentsProvider, accessType: AccessType | undefined) => { +const bindReporters = ( + container: Container, + args: ArgumentsProvider, + { accessType, localPath }: Pick, +) => { if (args.json) { container.bind(Types.IReporter).to(JSONReporter); } else if (args.html) { container.bind(Types.IReporter).to(HTMLReporter); - } else if (args.fix) { + } else if (args.fix && !localPath) { + // fix only local runs container.bind(Types.IReporter).to(FixReporter); } else { container.bind(Types.IReporter).to(CLIReporter); diff --git a/src/scanner/Scanner.ts b/src/scanner/Scanner.ts index 8f2a1dddf..07da47698 100644 --- a/src/scanner/Scanner.ts +++ b/src/scanner/Scanner.ts @@ -78,6 +78,7 @@ export class Scanner { isOnline: scanStrategy.isOnline, }; } + const isLocal = !scanStrategy.localPath; scanStrategy = await this.preprocessData(scanStrategy); this.d(`Scan strategy (after preprocessing): ${inspect(scanStrategy)}`); const scannerContext = discoveryContext.getScanningContext(scanStrategy); @@ -89,8 +90,12 @@ export class Scanner { this.d(`Practices (${practicesWithContext.length}):`, inspect(practicesWithContext)); let practicesAfterFix: PracticeWithContext[] | undefined; if (this.argumentsProvider.fix) { - await this.fix(practicesWithContext); - practicesAfterFix = await this.detectPractices(projectComponents); + if (isLocal) { + cli.warn('`fix` command only works for local folder, not for online repositories'); + } else { + await this.fix(practicesWithContext); + practicesAfterFix = await this.detectPractices(projectComponents); + } } await this.report(scannerContext.reporters, practicesWithContext, practicesAfterFix); this.d( @@ -272,13 +277,9 @@ export class Scanner { const relevantPractices: PracticeWithContextForReporter[] = practicesWithContext.map(pwcForReporter); this.d(`Reporters length: ${reporters.length}`); - if (this.argumentsProvider.fix) { - const relevantPracticesAfterFix: PracticeWithContextForReporter[] = practicesWithContextAfterFix!.map(pwcForReporter); - await Promise.all( - reporters.map(async (r) => { - this.argumentsProvider.fix ? await r.report(relevantPractices, relevantPracticesAfterFix) : await r.report(relevantPractices); - }), - ); + if (practicesWithContextAfterFix) { + const relevantPracticesAfterFix: PracticeWithContextForReporter[] = practicesWithContextAfterFix.map(pwcForReporter); + await Promise.all(reporters.map((r) => r.report(relevantPractices, relevantPracticesAfterFix))); } else { await Promise.all(reporters.map(async (r) => await r.report(relevantPractices))); } From 1baa2dc2a64af21e6e3db4b8640f19d9f995ab21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vlas=C3=A1k?= Date: Thu, 27 Aug 2020 13:51:29 +0200 Subject: [PATCH 2/2] fix: update CLIReporter error message to be more clear about the fix command --- src/reporters/CLIReporter.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/reporters/CLIReporter.ts b/src/reporters/CLIReporter.ts index 470930880..04aa808f6 100644 --- a/src/reporters/CLIReporter.ts +++ b/src/reporters/CLIReporter.ts @@ -86,7 +86,15 @@ export class CLIReporter implements IReporter { p.practice.fix && p.evaluation === PracticeEvaluationResult.notPracticing; const fixablePractices = cwp.practicesAndComponents.filter(fixablePractice); if (fixablePractices.length) { - lines.push(bold(yellow(`These practices might be automatically fixed (re-run the command with ${italic('--fix')} option):`))); + lines.push( + bold( + yellow( + `These practices might be automatically fixed (re-run the command with ${italic('--fix')} option and on a ${bold( + 'local', + )} folder):`, + ), + ), + ); lines.push(''); for (const p of fixablePractices) {