From 5c91a18e03be02e7d8a3c2875421163d1b18e4f7 Mon Sep 17 00:00:00 2001 From: Uladzimir Aleshka Date: Thu, 22 Aug 2019 15:50:19 +0300 Subject: [PATCH 1/3] Allow search by repo --- src/interfaces.ts | 1 + src/modules/base/base.report.service.ts | 31 +++++---- src/modules/cli/commander.service.ts | 7 ++- .../presenter/default.presenter.service.ts | 63 +++++++++++++++++-- src/modules/presenter/interfaces.ts | 3 +- src/modules/presenter/presenter.service.ts | 12 ++-- .../presenter/raw.json.presenter.service.ts | 9 +-- 7 files changed, 92 insertions(+), 34 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index cdff19d..6b5c97b 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -6,6 +6,7 @@ export interface IFilterOptions { export interface IOwnerOptions { org?: string; user?: string; + repo?: string; } export interface IPackageOptions { diff --git a/src/modules/base/base.report.service.ts b/src/modules/base/base.report.service.ts index 4177afa..b1513ff 100644 --- a/src/modules/base/base.report.service.ts +++ b/src/modules/base/base.report.service.ts @@ -13,7 +13,15 @@ export abstract class BaseReportService { protected abstract handleRepo(repo: string, options: IProgramOptions): Promise; public async getReport(options: IProgramOptions): Promise { - const { org, user, nvm, engines, token } = options; + const { org, user, token, repo } = options; + + if (repo) { + this.presenterService.showProcessingSpinner(options); + const repoReport = await this.getRepoReport(repo, options); + this.presenterService.hideSpinner({ success: true }); + + return [repoReport]; + } const repos = await this.octokitService.getRepos({ org, user, token }); @@ -21,21 +29,22 @@ export abstract class BaseReportService { return null; } - this.presenterService.showSearchNodeVersion({ nvm, engines }); - this.presenterService.showSpinner(`Search in every repo...`); + this.presenterService.showProcessingSpinner(options); const result = await Promise.all( - repos.map(async repo => { - try { - return await this.handleRepo(repo, options); - } - catch (e) { - return { repo, error: e.message } as T; - } - }) + repos.map(async repo => await this.getRepoReport(repo, options)) ); this.presenterService.hideSpinner({ success: true }); return result; } + private async getRepoReport(repo: string, options: IProgramOptions): Promise { + try { + return await this.handleRepo(repo, options); + } + catch (e) { + return { repo, error: e.message } as T; + } + } + } diff --git a/src/modules/cli/commander.service.ts b/src/modules/cli/commander.service.ts index 0cb47be..b2cd44a 100644 --- a/src/modules/cli/commander.service.ts +++ b/src/modules/cli/commander.service.ts @@ -25,6 +25,11 @@ export class CommanderService { describe: 'github user where search applied', type: 'string', }) + .option('repo', { + alias: 'r', + describe: 'github user where search applied', + type: 'string', + }) .option('package', { alias: 'p', describe: 'package to search', @@ -99,7 +104,7 @@ export class CommanderService { default: DEFAULT.token, type: 'string' }) - .group(['user', 'org'], 'Owner:') + .group(['user', 'org', 'repo'], 'Owner:') .group(['package', 'deps', 'dev-deps', 'peer-deps', 'yarn-lock', 'package-lock'], 'NPM package:') .group(['node', 'nvm', 'engines'], 'Node version:') .check(this.validation) diff --git a/src/modules/presenter/default.presenter.service.ts b/src/modules/presenter/default.presenter.service.ts index f15e851..ea98cea 100644 --- a/src/modules/presenter/default.presenter.service.ts +++ b/src/modules/presenter/default.presenter.service.ts @@ -21,7 +21,7 @@ export class DefaultPresenterService implements IPresenterService { } public write(str: string) { - this.stream.write(str+ '\n'); + this.stream.write(str + '\n'); } public configure() { @@ -70,6 +70,14 @@ export class DefaultPresenterService implements IPresenterService { this.spinner = ora({ prefixText: message }).start(); } + public showProcessingSpinner(options: IProgramOptions) { + this.write(`Search ${this.getWhatToSearch(options)}`); + this.write(`Search at ${this.getWhereToSearch(options)}`); + this.write(`Search in ${this.getRepoToSearch(options)}`); + + this.showSpinner(`Processing...`); + } + public hideSpinner({ success, message }: { success: boolean, message: string }) { if (message) { this.spinner.prefixText = message; @@ -82,14 +90,57 @@ export class DefaultPresenterService implements IPresenterService { } } - public showSearchNodeVersion({ nvm, engines }: { nvm: boolean, engines: boolean }) { - const sources = [nvm && '.nvmrc', engines && 'package.json engines'].filter(x => x).join(' and '); + protected getWhatToSearch(options: IProgramOptions): string { + const { node, package: packageName } = options; + + if (node) { + return 'node version'; + } + + if (packageName) { + return `version of npm package ${chalk.green(packageName)}`; + } + + return null; + } + + protected getWhereToSearch(options: IProgramOptions): string { + const { node, package: packageName } = options; + + const format = s => s.filter(x => x).join(', '); + + if (node) { + const { nvm, engines } = options; + + return format([ + nvm && '.nvmrc', + engines && 'package.json engines' + ]); + } + + if (packageName) { + const { yarnLock, packageLock, deps, devDeps, peerDeps } = options; + + return format([ + yarnLock && 'yarn.lock', + packageLock && 'package-lock.json', + (deps || devDeps || peerDeps) && `package.json (${ + format([ + deps && 'dependencies', + devDeps && 'devDependencies', + peerDeps && 'peerDependencies', + ]) + })`, + ]); + } - this.write(`Search node version from ${sources}`); + return null; } - public showSearchPackageVersion(packageName: string) { - this.write(`NPM package to search: ${chalk.green(packageName)}`); + protected getRepoToSearch(options: IProgramOptions): string { + const { repo } = options; + + return repo ? `${chalk.green(repo)}` : 'all repos'; } } diff --git a/src/modules/presenter/interfaces.ts b/src/modules/presenter/interfaces.ts index 1708540..a3d1e87 100644 --- a/src/modules/presenter/interfaces.ts +++ b/src/modules/presenter/interfaces.ts @@ -14,7 +14,6 @@ export interface IPresenterService { showError(message: string); showData(report: IReportItem[], options: IProgramOptions); showSpinner(message: string); + showProcessingSpinner(options: IProgramOptions); hideSpinner({ success, message }: { success: boolean, message?: string }); - showSearchNodeVersion({ nvm, engines }: { nvm: boolean, engines: boolean }); - showSearchPackageVersion(packageName: string); } diff --git a/src/modules/presenter/presenter.service.ts b/src/modules/presenter/presenter.service.ts index a101e09..544048f 100644 --- a/src/modules/presenter/presenter.service.ts +++ b/src/modules/presenter/presenter.service.ts @@ -57,16 +57,12 @@ export class PresenterService implements IPresenterService { this.presenter.showSpinner(message); } - public hideSpinner({ success, message }: { success: boolean, message?: string }) { - this.presenter.hideSpinner({ success, message }); - } - - public showSearchNodeVersion({ nvm, engines }: { nvm: boolean, engines: boolean }) { - this.presenter.showSearchNodeVersion({ nvm, engines }); + public showProcessingSpinner(options: IProgramOptions) { + this.presenter.showProcessingSpinner(options); } - public showSearchPackageVersion(packageName: string) { - this.presenter.showSearchPackageVersion(packageName); + public hideSpinner({ success, message }: { success: boolean, message?: string }) { + this.presenter.hideSpinner({ success, message }); } } diff --git a/src/modules/presenter/raw.json.presenter.service.ts b/src/modules/presenter/raw.json.presenter.service.ts index 1e5a9a6..8d7df53 100644 --- a/src/modules/presenter/raw.json.presenter.service.ts +++ b/src/modules/presenter/raw.json.presenter.service.ts @@ -1,9 +1,9 @@ +import { Writable } from 'stream'; import * as Octokit from '@octokit/rest'; import { Injectable, Inject } from '@nestjs/common'; import { IPresenterService } from './interfaces'; import { IReportItem, IProgramOptions } from '../../interfaces'; import { getFilter } from '../../util/result-filter'; -import { Writable } from 'stream'; @Injectable() export class RawJsonPresenterService implements IPresenterService { @@ -47,13 +47,10 @@ export class RawJsonPresenterService implements IPresenterService { public showSpinner() { } - public hideSpinner() { - } - - public showSearchNodeVersion() { + public showProcessingSpinner() { } - public showSearchPackageVersion() { + public hideSpinner() { } } From 59c2239f02f2cbf273efafe8786cc0bd1cc283d3 Mon Sep 17 00:00:00 2001 From: Uladzimir Aleshka Date: Thu, 22 Aug 2019 15:51:47 +0300 Subject: [PATCH 2/3] v2.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9f65e58..4d2d6d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "github-repo-tools", - "version": "2.3.3", + "version": "2.4.0", "description": "Useful tool to get versions from all repos of Github user/org", "main": "build/index.js", "bin": { From 88f17b450515bb17a45f51295ddfca49f340a71f Mon Sep 17 00:00:00 2001 From: Uladzimir Aleshka Date: Thu, 22 Aug 2019 15:56:21 +0300 Subject: [PATCH 3/3] Update package.json description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4d2d6d1..b7c500e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "github-repo-tools", "version": "2.4.0", - "description": "Useful tool to get versions from all repos of Github user/org", + "description": "Useful tool to get versions (node or npm package) from repo(s) of Github user/org", "main": "build/index.js", "bin": { "grt": "./build/app.js"