Skip to content

Commit

Permalink
Merge pull request #8 from lightness/feature/allow-search-by-repo
Browse files Browse the repository at this point in the history
Allow search by single repo
  • Loading branch information
lightness authored Aug 22, 2019
2 parents 109b25b + 88f17b4 commit 812cecd
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "github-repo-tools",
"version": "2.3.3",
"description": "Useful tool to get versions from all repos of Github user/org",
"version": "2.4.0",
"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"
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IFilterOptions {
export interface IOwnerOptions {
org?: string;
user?: string;
repo?: string;
}

export interface IPackageOptions {
Expand Down
31 changes: 20 additions & 11 deletions src/modules/base/base.report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,38 @@ export abstract class BaseReportService<T extends IReportItem> {
protected abstract handleRepo(repo: string, options: IProgramOptions): Promise<T>;

public async getReport(options: IProgramOptions): Promise<T[]> {
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 });

if (!repos) {
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<T> {
try {
return await this.handleRepo(repo, options);
}
catch (e) {
return { repo, error: e.message } as T;
}
}

}
7 changes: 6 additions & 1 deletion src/modules/cli/commander.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down
63 changes: 57 additions & 6 deletions src/modules/presenter/default.presenter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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';
}

}
3 changes: 1 addition & 2 deletions src/modules/presenter/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 4 additions & 8 deletions src/modules/presenter/presenter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

}
9 changes: 3 additions & 6 deletions src/modules/presenter/raw.json.presenter.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -47,13 +47,10 @@ export class RawJsonPresenterService implements IPresenterService {
public showSpinner() {
}

public hideSpinner() {
}

public showSearchNodeVersion() {
public showProcessingSpinner() {
}

public showSearchPackageVersion() {
public hideSpinner() {
}

}

0 comments on commit 812cecd

Please sign in to comment.