From 21c539bff06922315a28ca2ac5185d2306d0bdfc Mon Sep 17 00:00:00 2001 From: Prokop Simek Date: Tue, 28 Jan 2020 17:26:27 +0100 Subject: [PATCH] fix: back to single-command CLI --- .releaserc | 9 -- .travis.yml | 14 +- CHANGELOG.md | 66 --------- README.md | 47 +------ bin/run | 2 - package.json | 5 +- src/commands/init.ts | 30 ---- src/commands/practices.ts | 48 ------- src/commands/run.ts | 118 ---------------- src/index.ts | 130 +++++++++++++++++- .../JsPackageManagementUsedPractice.ts | 2 +- src/scanner/ArgumentsProvider.ts | 6 +- src/scanner/Scanner.ts | 15 +- src/scanner/ScannerUtils.spec.ts | 19 --- src/scanner/ScannerUtils.ts | 4 - 15 files changed, 154 insertions(+), 361 deletions(-) delete mode 100644 src/commands/init.ts delete mode 100644 src/commands/practices.ts delete mode 100644 src/commands/run.ts diff --git a/.releaserc b/.releaserc index 5ec81cca2..df1dbe0b5 100644 --- a/.releaserc +++ b/.releaserc @@ -14,14 +14,5 @@ "markdownReleaseNotes": true } ] - ], - "branches": [ - "master", - "next", - { "name": "beta", "prerelease": "beta" }, - { "name": "alpha", "prerelease": "alpha" }, - { "name": "feature/*", "prerelease": '${name.replace(/feature\//, "pre-")}' }, - { "name": "hotfix/*", "prerelease": '${name.replace(/hotfix\//, "pre-")}' }, - { "name": "debug/*", "prerelease": '${name.replace(/debug\//, "pre-")}' } ] } diff --git a/.travis.yml b/.travis.yml index e3d0f2e20..3a206389f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,9 +38,9 @@ notifications: script: - yarn build - yarn test --coverage - - bin/run + - bin/run -v - yarn test:codecov:upload - - bin/run run . --fail=high + - bin/run . --fail=high jobs: include: @@ -52,11 +52,11 @@ jobs: - stage: runtime test if: branch = master scripts: - - 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 + - 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 os: windows - os: linux - stage: release diff --git a/CHANGELOG.md b/CHANGELOG.md index 4805e5762..5f82bdd23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,69 +1,3 @@ -# [2.0.0](https://github.com/dxheroes/dx-scanner/compare/v1.38.0...v2.0.0) (2020-01-28) - - -### Bug Fixes - -* allow fail and recursive to be optional ([457eb72](https://github.com/dxheroes/dx-scanner/commit/457eb72426d4f4ccca135f43e599ea577c4f0077)) -* inform user if the config file already exists ([569725a](https://github.com/dxheroes/dx-scanner/commit/569725a0e30acb8d03523b70da9db981188d5023)) -* parse flags with init subcommand ([3ede8e0](https://github.com/dxheroes/dx-scanner/commit/3ede8e0fd3f02078d91ca85e58d2068bd0d9cf11)) -* remove comments a comment ideas ([e604238](https://github.com/dxheroes/dx-scanner/commit/e6042385ab862fac7ea888d155ed23189b5e61eb)) -* remove unnecessary code ([bb88dd4](https://github.com/dxheroes/dx-scanner/commit/bb88dd4bd3ab615cd5a9989f63f18c4dc69977ad)) -* remove unused commented code and import reflect-metadata ([5ad591a](https://github.com/dxheroes/dx-scanner/commit/5ad591a5d7ffafbc527643d202077c06085a7b6b)) -* remove unused flag, aliases and set new examples ([cccdf57](https://github.com/dxheroes/dx-scanner/commit/cccdf574f080db6039cd91f302774f6d6c4159ae)) -* rename method ([d8a88cf](https://github.com/dxheroes/dx-scanner/commit/d8a88cfbcabaffb0a8dd48e102e0e91d6c4c24f2)) -* rename subcommand ([e939b9e](https://github.com/dxheroes/dx-scanner/commit/e939b9e1f26b7bd14f6bc79ac34a9e67292a21bd)) -* revert change ([b4b6497](https://github.com/dxheroes/dx-scanner/commit/b4b6497f2909aa1bbfd45129883af5e4afadb8da)) -* set arguments back to be required ([dc8edd6](https://github.com/dxheroes/dx-scanner/commit/dc8edd602f81da53bb14d3891a4c1f73824338ac)) -* typo ([19e8bd1](https://github.com/dxheroes/dx-scanner/commit/19e8bd1ecbe785d1c6e47b2e91160742654e101c)) - - -### Features - -* **core:** CLI changed to multi-command ([cbf03b0](https://github.com/dxheroes/dx-scanner/commit/cbf03b016d6569c10638c9177a96cfaa962dcc77)) -* add two subcommands ([9661d6b](https://github.com/dxheroes/dx-scanner/commit/9661d6b40f348cc66cef7b64fa033e65e851c935)) -* convert init flag to subcommand ([9594d3a](https://github.com/dxheroes/dx-scanner/commit/9594d3ae7183d95eca7de2a68784bde27d4084bd)) -* implement method to get practices ([871f7f2](https://github.com/dxheroes/dx-scanner/commit/871f7f2bb3ca950c6313afc8b04baed3f2aa3301)) -* list practices in a table ([9d8a8f6](https://github.com/dxheroes/dx-scanner/commit/9d8a8f69b0f88f41052e46bd485a6d3c9abd97a1)) -* wip - add subcommand ([6c33fc4](https://github.com/dxheroes/dx-scanner/commit/6c33fc4f7fa4761cbbdd00a4a1f668f7aaabb572)) - - -### BREAKING CHANGES - -* **core:** now exists commands such as init, practices and run - -# [2.0.0-pre-list-practices.1](https://github.com/dxheroes/dx-scanner/compare/v1.38.0...v2.0.0-pre-list-practices.1) (2020-01-28) - - -### Bug Fixes - -* allow fail and recursive to be optional ([457eb72](https://github.com/dxheroes/dx-scanner/commit/457eb72426d4f4ccca135f43e599ea577c4f0077)) -* inform user if the config file already exists ([569725a](https://github.com/dxheroes/dx-scanner/commit/569725a0e30acb8d03523b70da9db981188d5023)) -* parse flags with init subcommand ([3ede8e0](https://github.com/dxheroes/dx-scanner/commit/3ede8e0fd3f02078d91ca85e58d2068bd0d9cf11)) -* remove comments a comment ideas ([e604238](https://github.com/dxheroes/dx-scanner/commit/e6042385ab862fac7ea888d155ed23189b5e61eb)) -* remove unnecessary code ([bb88dd4](https://github.com/dxheroes/dx-scanner/commit/bb88dd4bd3ab615cd5a9989f63f18c4dc69977ad)) -* remove unused commented code and import reflect-metadata ([5ad591a](https://github.com/dxheroes/dx-scanner/commit/5ad591a5d7ffafbc527643d202077c06085a7b6b)) -* remove unused flag, aliases and set new examples ([cccdf57](https://github.com/dxheroes/dx-scanner/commit/cccdf574f080db6039cd91f302774f6d6c4159ae)) -* rename method ([d8a88cf](https://github.com/dxheroes/dx-scanner/commit/d8a88cfbcabaffb0a8dd48e102e0e91d6c4c24f2)) -* rename subcommand ([e939b9e](https://github.com/dxheroes/dx-scanner/commit/e939b9e1f26b7bd14f6bc79ac34a9e67292a21bd)) -* revert change ([b4b6497](https://github.com/dxheroes/dx-scanner/commit/b4b6497f2909aa1bbfd45129883af5e4afadb8da)) -* set arguments back to be required ([dc8edd6](https://github.com/dxheroes/dx-scanner/commit/dc8edd602f81da53bb14d3891a4c1f73824338ac)) -* typo ([19e8bd1](https://github.com/dxheroes/dx-scanner/commit/19e8bd1ecbe785d1c6e47b2e91160742654e101c)) - - -### Features - -* **core:** CLI changed to multi-command ([cbf03b0](https://github.com/dxheroes/dx-scanner/commit/cbf03b016d6569c10638c9177a96cfaa962dcc77)) -* add two subcommands ([9661d6b](https://github.com/dxheroes/dx-scanner/commit/9661d6b40f348cc66cef7b64fa033e65e851c935)) -* convert init flag to subcommand ([9594d3a](https://github.com/dxheroes/dx-scanner/commit/9594d3ae7183d95eca7de2a68784bde27d4084bd)) -* implement method to get practices ([871f7f2](https://github.com/dxheroes/dx-scanner/commit/871f7f2bb3ca950c6313afc8b04baed3f2aa3301)) -* list practices in a table ([9d8a8f6](https://github.com/dxheroes/dx-scanner/commit/9d8a8f69b0f88f41052e46bd485a6d3c9abd97a1)) -* wip - add subcommand ([6c33fc4](https://github.com/dxheroes/dx-scanner/commit/6c33fc4f7fa4761cbbdd00a4a1f668f7aaabb572)) - - -### BREAKING CHANGES - -* **core:** now exists commands such as init, practices and run - # [1.38.0](https://github.com/dxheroes/dx-scanner/compare/v1.37.1...v1.38.0) (2020-01-27) diff --git a/README.md b/README.md index d474861a2..588dbeebb 100644 --- a/README.md +++ b/README.md @@ -62,43 +62,16 @@ OPTIONS --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 run - dx-scanner run ./ --fail=high - dx-scanner run github.com/DXHeroes/dx-scanner + dx-scanner + dx-scanner ./ --fail=high + dx-scanner github.com/DXHeroes/dx-scanner ``` -
-List of All Practices 🔍 - -Practice | Impact | Language Independent | JavaScript/TypeScript | Java -------------- | ------------- | ------------- | ------------- | ------------- -Create a Readme File | high | ✅ | ✅ | ✅ -Create a License File | medium | ✅ | ✅ | ✅ -Create a Lockfile | high | ✅ | ✅ | ✅ -Create a .gitignore | high | ✅ | ✅ | ✅ -Write in Typescript | medium | ❌ | ✅ | ❌ -Set .gitignore Correctly | high | ❌ | ✅ | ✅ -Use Continuous Integration | high | ✅ | ✅ | ✅ -Use Docker | small | ✅ | ✅ | ✅ -Use .editorconfig | small | ✅ | ✅ | ✅ -Format your code automatically | small | ❌ | ✅ | ❌ -Use ESLint | medium | ❌ | ✅ | ❌ -ESLint Without Errors | medium | ❌ | ✅ | ❌ -Use a different linter | medium | ❌ | ✅ | ❌ -Use JS Frontend Testing Framework | medium | ❌ | ✅ | ❌ -Use JS Frontend Build Tools | medium | ❌ | ✅ | ❌ -Use JS Backend Testing Frameworks | high | ❌ | ✅ | ❌ -Use a JS Logging Library | small | ❌ | ✅ | ❌ -Use JS Package Management | high | ❌ | ✅ | ❌ -Configure Scripts in package.json | medium | ❌ | ✅ | ❌ -Update Dependencies of Major Level | small | ❌ | ✅ | ❌ -Update Dependencies of Minor and Patch Level | high | ❌ | ✅ | ❌ -Do PullRequests | medium | ✅ | ✅ | ✅ -Solve Pull Requests Continuously | medium | ✅ | ✅ | ✅ -Write Commit Messages by Convention | small | ✅ | ✅ | ✅ -
- ## Configuration ⚙️ Add ```dxscannerrc.*``` config file to change default configuration settings. It can be a ```.json```, ```.yml```, and even a dotfile! @@ -196,9 +169,3 @@ Many thanks to these wonderful people ([emoji key](https://allcontributors.org/d This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Any kind of contributions are welcome! - - - - - - diff --git a/bin/run b/bin/run index 4c7b03a36..ee3701af0 100755 --- a/bin/run +++ b/bin/run @@ -8,10 +8,8 @@ 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/package.json b/package.json index 7855bca79..7a1adc73b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dx-scanner", "description": "Scan your project for possible DX recommendations.", - "version": "2.0.0", + "version": "1.38.0", "author": "DX Heroes LTD (https://dxheroes.io)", "homepage": "https://github.com/dxheroes/dx-scanner", "repository": "ssh://git@github.com/dxheroes/dx-scanner.git", @@ -114,8 +114,7 @@ "/lib" ], "oclif": { - "bin": "dx-scanner", - "commands": "./src/commands" + "bin": "dx-scanner" }, "keywords": [ "oclif", diff --git a/src/commands/init.ts b/src/commands/init.ts deleted file mode 100644 index 23471a08f..000000000 --- a/src/commands/init.ts +++ /dev/null @@ -1,30 +0,0 @@ -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 flags = { - help: flags.help({ char: 'h' }), - }; - - async run() { - const scanPath = process.cwd(); - - const container = createRootContainer({ - uri: scanPath, - json: false, - auth: undefined, - ci: false, - recursive: false, - fail: PracticeImpact.off, - fix: false, - fixPattern: undefined, - }); - const scanner = container.get(Scanner); - - await scanner.init(scanPath); - } -} diff --git a/src/commands/practices.ts b/src/commands/practices.ts deleted file mode 100644 index 6c57bd4f3..000000000 --- a/src/commands/practices.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Command, flags } from '@oclif/command'; -import { createRootContainer } from '../inversify.config'; -import { Scanner } from '../scanner'; -import { PracticeImpact } from '../model'; -import { ReporterData } from '../reporters/ReporterData'; - -export default class PracticesCommand extends Command { - static description = 'List all practices id with name and impact.'; - - static flags = { - help: flags.help({ char: 'h' }), - json: flags.boolean({ char: 'j', description: 'Print practices in JSON' }), - }; - - async run() { - const { flags } = this.parse(PracticesCommand); - const scanPath = process.cwd(); - - const container = createRootContainer({ - uri: scanPath, - json: flags.json, - auth: undefined, - ci: false, - recursive: false, - fail: PracticeImpact.off, - fix: false, - fixPattern: undefined, - }); - - const scanner = container.get(Scanner); - - 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)); - } else { - console.log(ReporterData.table(['PRACTICE ID', 'PRACTICE NAME', 'PRACTICE IMPACT'], practicesToReport)); - } - } -} diff --git a/src/commands/run.ts b/src/commands/run.ts deleted file mode 100644 index 91c42a649..000000000 --- a/src/commands/run.ts +++ /dev/null @@ -1,118 +0,0 @@ -/* eslint-disable no-process-env */ -import { Command, flags } from '@oclif/command'; -import cli from 'cli-ux'; -import debug from 'debug'; -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'; - -export class RunCommand extends Command { - static description = 'Scan your project for possible DX recommendations.'; - - static flags = { - version: flags.version({ char: 'v', description: 'Output the version number' }), - help: flags.help({ char: 'h' }), - authorization: flags.string({ - char: 'a', - description: - 'Credentials to the repository. (in format "token" or "username:token"; can be set as ENV variable DX_GIT_SERVICE_TOKEN)', - }), - json: flags.boolean({ char: 'j', description: 'Print report in JSON' }), - recursive: flags.boolean({ char: 'r', description: 'Scan all components recursively in all sub folders' }), - ci: flags.boolean({ - description: 'CI mode', - default: () => process.env.CI === 'true', - }), - fail: flags.string({ - options: ['high', 'medium', 'small', 'off', 'all'], - description: 'Run scanner in failure mode. Exits process with code 1 for any non-practicing condition of given level.', - default: PracticeImpact.high, - }), - fix: flags.boolean({ char: 'f', description: 'Tries to fix problems automatically', default: false }), - fixPattern: flags.string({ description: 'Fix only rules with IDs matching the regex.' }), - }; - - static args = [{ name: 'path', default: process.cwd() }]; - - static aliases = ['dxs', 'dxscanner']; - 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(RunCommand); - debug('cli args')(args); - debug('cli flags')(flags); - const scanPath = args.path; - - let authorization = flags.authorization ? flags.authorization : this.loadAuthTokenFromEnvs(); - const json = flags.json; - const fail = flags.fail; - - const notifier = updateNotifier({ pkg: this.config.pjson }); - const hrstart = process.hrtime(); - - cli.action.start(`Scanning URI: ${scanPath}`); - - const container = createRootContainer({ - uri: scanPath, - auth: authorization, - json, - fail, - recursive: flags.recursive, - ci: flags.ci, - fix: flags.fix, - fixPattern: flags.fixPattern, - }); - const scanner = container.get(Scanner); - - let scanResult = await scanner.scan(); - - if (scanResult.needsAuth && !flags.ci) { - if (ScanningStrategyDetectorUtils.isGitHubPath(scanPath) || scanResult.serviceType === ServiceType.github) { - authorization = await cli.prompt('Insert your GitHub personal access token. https://github.com/settings/tokens\n', { - type: 'hide', - }); - } else if (ScanningStrategyDetectorUtils.isBitbucketPath(scanPath) || scanResult.serviceType === ServiceType.bitbucket) { - authorization = await cli.prompt( - 'Insert your Bitbucket credentials (in format "appPassword" or "username:appPasword"). https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html\n', - { type: 'hide' }, - ); - } - - const container = createRootContainer({ - uri: scanPath, - auth: authorization, - json, - fail, - recursive: flags.recursive, - ci: flags.ci, - fix: flags.fix, - fixPattern: flags.fixPattern, - }); - const scanner = container.get(Scanner); - - scanResult = await scanner.scan({ determineRemote: false }); - } - cli.action.stop(); - notifier.notify({ isGlobal: true }); - - const hrend = process.hrtime(hrstart); - - console.info('Scan duration %ds.', hrend[0]); - - if (scanResult.shouldExitOnEnd) { - this.exit(1); - } - } - - /** - * Loads API token from environment variables - */ - private loadAuthTokenFromEnvs = (): string | undefined => { - // eslint-disable-next-line no-process-env - const ev = process.env; - return ev.DX_GIT_SERVICE_TOKEN || ev.GITHUB_TOKEN; - }; -} diff --git a/src/index.ts b/src/index.ts index daee635c5..f258adec5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,130 @@ -export { run } from '@oclif/command'; +/* eslint-disable no-process-env */ +import { Command, flags } from '@oclif/command'; +import cli from 'cli-ux'; +import debug from 'debug'; import 'reflect-metadata'; +import updateNotifier from 'update-notifier'; +import { ServiceType } from './detectors/ScanningStrategyDetector'; +import { ScanningStrategyDetectorUtils } from './detectors/utils/ScanningStrategyDetectorUtils'; +import { createRootContainer } from './inversify.config'; +import { PracticeImpact } from './model'; +import { Scanner } from './scanner/Scanner'; + +class DXScannerCommand 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) + authorization: flags.string({ + char: 'a', + description: + 'Credentials to the repository. (in format "token" or "username:token"; can be set as ENV variable DX_GIT_SERVICE_TOKEN)', + }), + json: flags.boolean({ char: 'j', description: 'Print report in JSON' }), + recursive: flags.boolean({ char: 'r', description: 'Scan all components recursively in all sub folders' }), + init: flags.boolean({ char: 'i', description: 'Initialize DX Scanner configuration' }), + ci: flags.boolean({ + description: 'CI mode', + default: () => process.env.CI === 'true', + }), + fail: flags.string({ + options: ['high', 'medium', 'small', 'off', 'all'], + description: 'Run scanner in failure mode. Exits process with code 1 for any non-practicing condition of given level.', + default: PracticeImpact.high, + }), + fix: flags.boolean({ char: 'f', description: 'Tries to fix problems automatically', default: false }), + fixPattern: flags.string({ description: 'Fix only rules with IDs matching the regex.' }), + }; + + static args = [{ name: 'path', default: process.cwd() }]; + + static aliases = ['dxs', 'dxscanner']; + 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; + + let authorization = flags.authorization ? flags.authorization : this.loadAuthTokenFromEnvs(); + const json = flags.json; + const fail = flags.fail; + + const notifier = updateNotifier({ pkg: this.config.pjson }); + const hrstart = process.hrtime(); + + cli.action.start(`Scanning URI: ${scanPath}`); + + const container = createRootContainer({ + uri: scanPath, + auth: authorization, + json, + fail, + recursive: flags.recursive, + ci: flags.ci, + fix: flags.fix, + fixPattern: flags.fixPattern, + }); + const scanner = container.get(Scanner); + + if (flags.init) { + await scanner.init(scanPath); + process.exit(0); + } + + let scanResult = await scanner.scan(); + + if (scanResult.needsAuth && !flags.ci) { + if (ScanningStrategyDetectorUtils.isGitHubPath(scanPath) || scanResult.serviceType === ServiceType.github) { + authorization = await cli.prompt('Insert your GitHub personal access token. https://github.com/settings/tokens\n', { + type: 'hide', + }); + } else if (ScanningStrategyDetectorUtils.isBitbucketPath(scanPath) || scanResult.serviceType === ServiceType.bitbucket) { + authorization = await cli.prompt( + 'Insert your Bitbucket credentials (in format "appPassword" or "username:appPasword"). https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html\n', + { type: 'hide' }, + ); + } + + const container = createRootContainer({ + uri: scanPath, + auth: authorization, + json, + fail, + recursive: flags.recursive, + ci: flags.ci, + fix: flags.fix, + fixPattern: flags.fixPattern, + }); + const scanner = container.get(Scanner); + + scanResult = await scanner.scan({ determineRemote: false }); + } + cli.action.stop(); + notifier.notify({ isGlobal: true }); + + const hrend = process.hrtime(hrstart); + + console.info('Scan duration %ds.', hrend[0]); + + if (scanResult.shouldExitOnEnd) { + process.exit(1); + } + } + + /** + * Loads API token from environment variables + */ + private loadAuthTokenFromEnvs = (): string | undefined => { + // eslint-disable-next-line no-process-env + const ev = process.env; + return ev.DX_GIT_SERVICE_TOKEN || ev.GITHUB_TOKEN; + }; +} + +export = DXScannerCommand; diff --git a/src/practices/JavaScript/JsPackageManagementUsedPractice.ts b/src/practices/JavaScript/JsPackageManagementUsedPractice.ts index 9c02ba5a9..df0ce77eb 100644 --- a/src/practices/JavaScript/JsPackageManagementUsedPractice.ts +++ b/src/practices/JavaScript/JsPackageManagementUsedPractice.ts @@ -6,7 +6,7 @@ import { PracticeContext } from '../../contexts/practice/PracticeContext'; @DxPractice({ id: 'Javascript.PackageManagementUsed', name: 'Use JS Package Management', - impact: PracticeImpact.high, + impact: PracticeImpact.high, //which impact? suggestion: 'Use Package.json to keep track of packages that are being used in your application.', reportOnlyOnce: true, url: 'https://docs.npmjs.com/files/package.json', diff --git a/src/scanner/ArgumentsProvider.ts b/src/scanner/ArgumentsProvider.ts index 7d15a27a3..80561a14c 100644 --- a/src/scanner/ArgumentsProvider.ts +++ b/src/scanner/ArgumentsProvider.ts @@ -3,9 +3,9 @@ import { PracticeImpact } from '../model'; export type ArgumentsProvider = { uri: string; auth: string | undefined; - json: boolean; - fail: PracticeImpact | 'all'; // optional as the AP is used also with command dxs practices - recursive: boolean; // optional as the AP is used also with command dxs practices + json: boolean | undefined; + fail: PracticeImpact | 'all'; + recursive: boolean; ci: boolean; fix: boolean; fixPattern: string | undefined; diff --git a/src/scanner/Scanner.ts b/src/scanner/Scanner.ts index 72a339bb9..0f5010cb2 100644 --- a/src/scanner/Scanner.ts +++ b/src/scanner/Scanner.ts @@ -102,17 +102,15 @@ export class Scanner { */ async init(scanPath: string): Promise { const filePath = path.resolve(scanPath, '.dxscannerrc'); - // check if .dxscannerrc* already exists + cli.action.start(`Initializing configuration: ${filePath}.yaml`); + // check if .dxscannerrc.yaml already exists const fileExists: boolean = await this.fileSystemService.exists(`${filePath}`); const yamlExists: boolean = await this.fileSystemService.exists(`${filePath}.yaml`); const ymlExists: boolean = await this.fileSystemService.exists(`${filePath}.yml`); const jsonExists: boolean = await this.fileSystemService.exists(`${filePath}.json`); if (!yamlExists && !fileExists && !ymlExists && !jsonExists) { - cli.action.start(`Initializing configuration: ${filePath}.yaml`); await this.createConfiguration(filePath); - } else { - console.log(`You already have a dx-scanner config.`); } cli.action.stop(); } @@ -326,8 +324,9 @@ export class Scanner { private async createConfiguration(filePath: string) { let yamlInitContent = `# practices:`; - - for (const practice of this.listPractices()) { + // get Metadata and sort it alphabetically using id + const sortedInitializedPractices = this.practices.sort((a, b) => a.getMetadata().id.localeCompare(b.getMetadata().id)); + for (const practice of sortedInitializedPractices) { const dataObject = practice.getMetadata(); yamlInitContent += `\n# ${dataObject.id}: ${dataObject.impact}`; } @@ -337,10 +336,6 @@ export class Scanner { throw ErrorFactory.newInternalError(`Error during configuration file initialization: ${err.message}`); } } - - listPractices(): IPracticeWithMetadata[] { - return ScannerUtils.sortAlphabetically(this.practices); - } } interface ProjectComponentAndLangContext { diff --git a/src/scanner/ScannerUtils.spec.ts b/src/scanner/ScannerUtils.spec.ts index 2235f3232..26534d476 100644 --- a/src/scanner/ScannerUtils.spec.ts +++ b/src/scanner/ScannerUtils.spec.ts @@ -46,25 +46,6 @@ describe('ScannerUtils', () => { const practice = ScannerUtils.initPracticeWithMetadata(InvalidTestPractice); expect(() => ScannerUtils.sortPractices([practice])).toThrow(expectedErrMsg); }); - - it('sorts practices alphabetically', () => { - const practices = [FirstTestPractice, SecondTestPractice, DeprecatedTSLintPractice, ESLintUsedPractice, TypeScriptUsedPractice].map( - ScannerUtils.initPracticeWithMetadata, - ); - const result = ScannerUtils.sortAlphabetically(practices); - const id = result.map((practice) => { - return practice.getMetadata().id; - }); - - expect(result).toHaveLength(5); - expect(id).toEqual([ - 'JavaScript.DeprecatedTSLint', - 'JavaScript.ESLintUsed', - 'JavaScript.TypeScriptUsed', - 'Mock.FirstTestPractice', - 'Mock.SecondTestPractice', - ]); - }); }); describe('#isFulfilled', () => { diff --git a/src/scanner/ScannerUtils.ts b/src/scanner/ScannerUtils.ts index b07af7949..9a9ae238e 100644 --- a/src/scanner/ScannerUtils.ts +++ b/src/scanner/ScannerUtils.ts @@ -133,8 +133,4 @@ export class ScannerUtils { (_.includes(ScannerUtils.getImpactFailureLevels(argumentsProvider.fail), pctx.overridenImpact) || argumentsProvider.fail === 'all'), ); }; - - static sortAlphabetically = (practices: IPracticeWithMetadata[]) => { - return practices.sort((a, b) => a.getMetadata().id.localeCompare(b.getMetadata().id)); - }; }