From c8b9a4fb4ff29a18d1cd168b3f0500ce9d6fa624 Mon Sep 17 00:00:00 2001 From: PKief Date: Wed, 28 Aug 2019 22:24:07 +0200 Subject: [PATCH] Improve code quality of contributors script --- .../scripts/contributors/contributorOptions.ts | 5 ----- .../scripts/contributors/contributorsConfig.ts | 10 ++++++++++ src/scripts/contributors/index.ts | 17 ++++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) delete mode 100644 src/models/scripts/contributors/contributorOptions.ts create mode 100644 src/models/scripts/contributors/contributorsConfig.ts diff --git a/src/models/scripts/contributors/contributorOptions.ts b/src/models/scripts/contributors/contributorOptions.ts deleted file mode 100644 index 8ac7bdfe2d..0000000000 --- a/src/models/scripts/contributors/contributorOptions.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface ContributorOptions { - username: string; - repo: string; - imageSize: number; -} diff --git a/src/models/scripts/contributors/contributorsConfig.ts b/src/models/scripts/contributors/contributorsConfig.ts new file mode 100644 index 0000000000..c0e4369dc9 --- /dev/null +++ b/src/models/scripts/contributors/contributorsConfig.ts @@ -0,0 +1,10 @@ +export interface ContributorsConfig { + /** Owner of the repository */ + owner: string; + + /** Name of the repository */ + repo: string; + + /** Size of a contributor's profile image in pixels */ + imageSize: number; +} diff --git a/src/scripts/contributors/index.ts b/src/scripts/contributors/index.ts index 29558d7925..ff4297412c 100644 --- a/src/scripts/contributors/index.ts +++ b/src/scripts/contributors/index.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as https from 'https'; import * as path from 'path'; import { Contributor } from '../../models/scripts/contributors/contributor'; -import { ContributorOptions } from '../../models/scripts/contributors/contributorOptions'; +import { ContributorsConfig } from '../../models/scripts/contributors/contributorsConfig'; import * as painter from '../helpers/painter'; /** @@ -24,14 +24,17 @@ const parseLinkHeader = (linkHeader: string) => { /** * Get all contributors from GitHub API. + * @param page Results page + * @param owner Owner of the repository + * @param repo Name of the repository */ -const fetchContributors = (page: string, username: string, repo: string): +const fetchContributors = (page: string, owner: string, repo: string): Promise<{ contributorsOfPage: Contributor[], nextPage: string }> => { return new Promise((resolve, reject) => { const requestOptions: https.RequestOptions = { method: 'GET', hostname: 'api.github.com', - path: `/repos/${username}/${repo}/contributors?page=${page}`, + path: `/repos/${owner}/${repo}/contributors?page=${page}`, port: 443, headers: { 'link': 'next', @@ -84,13 +87,13 @@ const createLinkedImages = (contributors: Contributor[], imageSize: number = 40) return linkList; }; -const updateContributors = async ({ username, repo, imageSize }: ContributorOptions) => { +const updateContributors = async (config: ContributorsConfig) => { const contributors: Contributor[] = []; let page = '1'; // iterate over the pages of GitHub API while (page !== undefined) { - const result = await fetchContributors(page, username, repo); + const result = await fetchContributors(page, config.owner, config.repo); contributors.push(...result.contributorsOfPage); page = result.nextPage; } @@ -101,7 +104,7 @@ const updateContributors = async ({ username, repo, imageSize }: ContributorOpti console.log('> Material Icon Theme:', painter.red(`Error: Could not fetch contributors from GitHub!`)); throw Error(); } - const images = createLinkedImages(contributors, imageSize); + const images = createLinkedImages(contributors, config.imageSize); // create the image console.log('> Material Icon Theme:', painter.yellow(`Updating README.md ...`)); @@ -115,7 +118,7 @@ const updateContributors = async ({ username, repo, imageSize }: ContributorOpti }; updateContributors({ - username: 'pkief', + owner: 'pkief', repo: 'vscode-material-icon-theme', imageSize: 40, });