Skip to content

Commit

Permalink
Merge pull request #3 from lightness/feature/token-by-command-line-args
Browse files Browse the repository at this point in the history
Token can be passed by command line args
  • Loading branch information
lightness authored Jun 29, 2019
2 parents 7175604 + 76034ae commit 84c27c7
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 106 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-repo-tools",
"version": "2.1.2",
"version": "2.2.0",
"description": "Useful tool to get versions from all repos of Github user/org",
"main": "build/app.js",
"bin": {
Expand Down Expand Up @@ -42,6 +42,7 @@
"figlet": "^1.2.1",
"inquirer": "^6.3.1",
"lodash": "^4.17.11",
"lodash-decorators": "^6.0.1",
"moment": "^2.24.0",
"ora": "^3.4.0",
"reflect-metadata": "^0.1.13",
Expand Down
2 changes: 1 addition & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class AppService {
this.setupPresenter(options);

this.presenterService.showFiglet();
this.presenterService.showGithubTokenInfo();
this.presenterService.showGithubTokenInfo(options);

const { package: packageName, node, rateLimit } = options;

Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IPresenterOptions {

export interface IProgramOptions extends IFilterOptions, IOwnerOptions, IPackageOptions, INodeOptions, IPresenterOptions {
rateLimit?: boolean;
token?: string;
}

export interface IReportItem {
Expand Down
8 changes: 5 additions & 3 deletions src/modules/cli/cli.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Memoize } from 'lodash-decorators';
import { IProgramOptions } from '../../interfaces';
import { CommanderService } from './commander.service';
import { InquirerService } from './inquirer.service';
import { InputValidatorService } from './input.validator.service';
import { InvalidResult, ValidResult } from './interfaces';
import { InvalidResult } from './interfaces';

@Injectable()
export class CliService {
Expand All @@ -15,10 +16,11 @@ export class CliService {
) {
}

public async getProgramOptions(): Promise<IProgramOptions> {
@Memoize()
public getProgramOptions(): Promise<IProgramOptions> {
const options = this.commanderService.getProgramOptions();

return await this.patchOptions(options);
return this.patchOptions(options);
}

public async patchOptions(options): Promise<IProgramOptions> {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/cli/commander.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class CommanderService {
.usage('Search npm packages in all org repos. You can set GITHUB_TOKEN env var, if public access restricted')
.help('help')
.alias('h', 'help')
.showHelpOnFail(false, "Specify --help for available options")
.showHelpOnFail(false, 'Specify --help for available options')
.version()
.alias('v', 'version')
.describe('v', 'show version information')
Expand Down Expand Up @@ -92,6 +92,12 @@ export class CommanderService {
default: false,
type: 'boolean',
})
.option('token', {
alias: 't',
describe: 'token to auth on github. Env var GITHUB_TOKEN strictly prefered',
default: process.env.GITHUB_TOKEN,
type: 'string'
})
.group(['user', 'org'], 'Owner:')
.group(['package', 'deps', 'dev-deps', 'peer-deps', 'yarn-lock', 'package-lock'], 'NPM package:')
.group(['node', 'nvm', 'engines'], 'Node version:')
Expand Down
6 changes: 3 additions & 3 deletions src/modules/cli/input.validator.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { IProgramOptions, IOwnerOptions } from "../../interfaces";
import { Mode, ValidationResult } from "./interfaces";
import { Injectable } from '@nestjs/common';
import { IProgramOptions, IOwnerOptions } from '../../interfaces';
import { Mode, ValidationResult } from './interfaces';

@Injectable()
export class InputValidatorService {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/node-version/node.version.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from "@nestjs/common";
import { OctokitModule } from "../octokit/octokit.module";
import { NodeVersionService } from "./node.version.service";
import { PresenterModule } from "../presenter/presenter.module";
import { Module } from '@nestjs/common';
import { OctokitModule } from '../octokit/octokit.module';
import { NodeVersionService } from './node.version.service';
import { PresenterModule } from '../presenter/presenter.module';

@Module({
imports: [
Expand Down
10 changes: 5 additions & 5 deletions src/modules/node-version/node.version.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from "@nestjs/common";
import { IProgramOptions } from "../../interfaces";
import { INodeVersion } from "./interfaces";
import { OctokitService } from "../octokit/octokit.service";
import { PresenterService } from "../presenter/presenter.service";
import { Injectable } from '@nestjs/common';
import { IProgramOptions } from '../../interfaces';
import { INodeVersion } from './interfaces';
import { OctokitService } from '../octokit/octokit.service';
import { PresenterService } from '../presenter/presenter.service';

@Injectable()
export class NodeVersionService {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/npm-dependency-version/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IReportItem } from "../../interfaces";
import { IReportItem } from '../../interfaces';

export interface IPacakgeVersion extends IReportItem {
version?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from "@nestjs/common";
import { OctokitModule } from "../octokit/octokit.module";
import { NpmDependencyVersionService } from "./npm.dependency.version.service";
import { PackageLockVersionService } from "./package.lock.version.service";
import { YarnLockVersionService } from "./yarn.lock.version.service";
import { PresenterModule } from "../presenter/presenter.module";
import { Module } from '@nestjs/common';
import { OctokitModule } from '../octokit/octokit.module';
import { NpmDependencyVersionService } from './npm.dependency.version.service';
import { PackageLockVersionService } from './package.lock.version.service';
import { YarnLockVersionService } from './yarn.lock.version.service';
import { PresenterModule } from '../presenter/presenter.module';


@Module({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';
import chalk from 'chalk';
import { IProgramOptions, IPackageOptions } from "../../interfaces";
import { OctokitService } from "../octokit/octokit.service";
import { PackageLockVersionService } from "./package.lock.version.service";
import { YarnLockVersionService } from "./yarn.lock.version.service";
import { PresenterService } from "../presenter/presenter.service";
import { IPacakgeVersion as IPackageVersion } from "./interfaces";
import { IProgramOptions, IPackageOptions } from '../../interfaces';
import { OctokitService } from '../octokit/octokit.service';
import { PackageLockVersionService } from './package.lock.version.service';
import { YarnLockVersionService } from './yarn.lock.version.service';
import { PresenterService } from '../presenter/presenter.service';
import { IPacakgeVersion as IPackageVersion } from './interfaces';

@Injectable()
export class NpmDependencyVersionService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';
import { get } from 'lodash';
import { OctokitService } from "../octokit/octokit.service";
import { OctokitService } from '../octokit/octokit.service';

@Injectable()
export class PackageLockVersionService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';
import * as lockfile from '@yarnpkg/lockfile';
import { OctokitService } from "../octokit/octokit.service";
import { OctokitService } from '../octokit/octokit.service';

@Injectable()
export class YarnLockVersionService {
Expand Down
8 changes: 5 additions & 3 deletions src/modules/octokit/octokit.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Module } from "@nestjs/common";
import { OctokitService } from "./octokit.service";
import { PresenterModule } from "../presenter/presenter.module";
import { Module } from '@nestjs/common';
import { OctokitService } from './octokit.service';
import { PresenterModule } from '../presenter/presenter.module';
import { CliModule } from '../cli/cli.module';

@Module({
imports: [
PresenterModule,
CliModule,
],
providers: [OctokitService],
exports: [OctokitService]
Expand Down
41 changes: 23 additions & 18 deletions src/modules/octokit/octokit.service.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';
import * as Octokit from '@octokit/rest';
import * as RetryPlugin from '@octokit/plugin-retry';
import { PresenterService } from "../presenter/presenter.service";
import { PresenterService } from '../presenter/presenter.service';
import { CliService } from '../cli/cli.service';
import { Memoize } from 'lodash-decorators';
import { ClientHttp2Stream } from 'http2';

@Injectable()
export class OctokitService {

constructor(
private presentationService: PresenterService,
private cliService: CliService,
) {
}

private octokitInstance: Octokit;
@Memoize()
public async getOctokit() {
const { token } = await this.cliService.getProgramOptions();

public get octokit() {
if (!this.octokitInstance) {
this.octokitInstance = new (Octokit.plugin(RetryPlugin))({
auth: process.env.GITHUB_TOKEN,
retry: {
doNotRetry: [404],
}
});
}

return this.octokitInstance;
return new (Octokit.plugin(RetryPlugin))({
auth: token,
retry: {
doNotRetry: [404],
}
});
}

public async getFileContent(owner, repo, path) {
const octokit = await this.getOctokit();

try {
const response = await this.octokit.repos.getContents({
const response = await octokit.repos.getContents({
owner,
repo,
path,
Expand All @@ -48,13 +51,15 @@ export class OctokitService {
}

public async getRepos({ org, user }: { org: string, user: string }): Promise<string[] | null> {
const octokit = await this.getOctokit();

const options = org
? this.octokit.repos.listForOrg.endpoint.merge({ org })
: this.octokit.repos.listForUser.endpoint.merge({ username: user });
? octokit.repos.listForOrg.endpoint.merge({ org })
: octokit.repos.listForUser.endpoint.merge({ username: user });

this.presentationService.showSpinner('Searching for repos...');
try {
const repos = await this.octokit.paginate(options);
const repos = await octokit.paginate(options);
this.presentationService.hideSpinner({ success: true, message: `${repos.length} repos found` });

return repos.map(repo => repo.name);
Expand Down
20 changes: 15 additions & 5 deletions src/modules/presenter/default.presenter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as moment from 'moment';
import * as Octokit from '@octokit/rest';
import * as ora from 'ora';
import chalk from 'chalk';
import { TableService } from "../table/table.service";
import { IPresenterService } from "./interfaces";
import { Injectable } from '@nestjs/common';
import { TableService } from '../table/table.service';
import { IPresenterService } from './interfaces';
import { IReportItem, IProgramOptions } from '../../interfaces';
import { getFilter } from '../../util/result-filter';
import { Injectable } from '@nestjs/common';
import { CliService } from '../cli/cli.service';

@Injectable()
export class DefaultPresenterService implements IPresenterService {
Expand All @@ -25,13 +26,17 @@ export class DefaultPresenterService implements IPresenterService {
console.log(figlet.textSync('Github Repo Tools'));
}

public showGithubTokenInfo() {
const withGithubToken = !!process.env.GITHUB_TOKEN;
public showGithubTokenInfo(options: IProgramOptions) {
const withGithubToken = !!options.token;

console.log(`Use GITHUB_TOKEN env: ${withGithubToken ? chalk.green('yes') : chalk.red('no')}`);
}

public showRateLimit(rateLimit: Octokit.RateLimitGetResponseRate) {
if (!rateLimit) {
return;
}

const { limit, remaining, reset } = rateLimit;
const resetMoment = moment(reset * 1000);
const resetIn = resetMoment.fromNow();
Expand All @@ -45,6 +50,11 @@ export class DefaultPresenterService implements IPresenterService {
}

public showData(report: IReportItem[], options: IProgramOptions) {
if (!report) {
console.log('No data found');
return;
}

const filteredReport: IReportItem[] = report.filter(getFilter(options));
const output: string = this.tableService.format(filteredReport, options);

Expand Down
2 changes: 1 addition & 1 deletion src/modules/presenter/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export enum PresentationMode {

export interface IPresenterService {
showFiglet();
showGithubTokenInfo();
showGithubTokenInfo(options: IProgramOptions);
showRateLimit(rateLimit: Octokit.RateLimitGetResponseRate, isMainInfo: boolean);
showError(message: string);
showData(report: IReportItem[], options: IProgramOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/presenter/json.presenter.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from "@nestjs/common";
import { RawJsonPresenterService } from "./raw.json.presenter.service";
import { Injectable } from '@nestjs/common';
import { RawJsonPresenterService } from './raw.json.presenter.service';

@Injectable()
export class JsonPresenterService extends RawJsonPresenterService {
Expand Down
12 changes: 6 additions & 6 deletions src/modules/presenter/presenter.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from "@nestjs/common";
import { PresenterService } from "./presenter.service";
import { TableModule } from "../table/table.module";
import { DefaultPresenterService } from "./default.presenter.service";
import { JsonPresenterService } from "./json.presenter.service";
import { RawJsonPresenterService } from "./raw.json.presenter.service";
import { Module } from '@nestjs/common';
import { PresenterService } from './presenter.service';
import { TableModule } from '../table/table.module';
import { DefaultPresenterService } from './default.presenter.service';
import { JsonPresenterService } from './json.presenter.service';
import { RawJsonPresenterService } from './raw.json.presenter.service';

@Module({
providers: [
Expand Down
16 changes: 8 additions & 8 deletions src/modules/presenter/presenter.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';
import * as Octokit from '@octokit/rest';
import { PresentationMode, IPresenterService } from "./interfaces";
import { DefaultPresenterService } from "./default.presenter.service";
import { JsonPresenterService } from "./json.presenter.service";
import { IReportItem, IProgramOptions } from "../../interfaces";
import { RawJsonPresenterService } from "./raw.json.presenter.service";
import { PresentationMode, IPresenterService } from './interfaces';
import { DefaultPresenterService } from './default.presenter.service';
import { JsonPresenterService } from './json.presenter.service';
import { IReportItem, IProgramOptions } from '../../interfaces';
import { RawJsonPresenterService } from './raw.json.presenter.service';

@Injectable()
export class PresenterService implements IPresenterService {
Expand Down Expand Up @@ -37,8 +37,8 @@ export class PresenterService implements IPresenterService {
this.presenter.showFiglet();
}

public showGithubTokenInfo() {
this.presenter.showGithubTokenInfo();
public showGithubTokenInfo(options: IProgramOptions) {
this.presenter.showGithubTokenInfo(options);
}

public showRateLimit(rateLimit: Octokit.RateLimitGetResponseRate, isMainInfo: boolean) {
Expand Down
13 changes: 9 additions & 4 deletions src/modules/presenter/raw.json.presenter.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Octokit from '@octokit/rest';
import { Injectable } from "@nestjs/common";
import { IPresenterService } from "./interfaces";
import { IReportItem, IProgramOptions } from "../../interfaces";
import { getFilter } from "../../util/result-filter";
import { Injectable } from '@nestjs/common';
import { IPresenterService } from './interfaces';
import { IReportItem, IProgramOptions } from '../../interfaces';
import { getFilter } from '../../util/result-filter';

@Injectable()
export class RawJsonPresenterService implements IPresenterService {
Expand All @@ -27,6 +27,11 @@ export class RawJsonPresenterService implements IPresenterService {
}

public showData(report: IReportItem[], options: IProgramOptions) {
if (!report) {
this.log({ error: 'No data found' });
return;
}

const filteredReport: IReportItem[] = report.filter(getFilter(options));

this.log(filteredReport);
Expand Down
Loading

0 comments on commit 84c27c7

Please sign in to comment.