Skip to content

Commit

Permalink
Improve code quality of contributors script
Browse files Browse the repository at this point in the history
  • Loading branch information
PKief committed Aug 28, 2019
1 parent 2e2bb34 commit c8b9a4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/models/scripts/contributors/contributorOptions.ts

This file was deleted.

10 changes: 10 additions & 0 deletions 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;
}
17 changes: 10 additions & 7 deletions src/scripts/contributors/index.ts
Expand Up @@ -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';

/**
Expand All @@ -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',
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 ...`));
Expand All @@ -115,7 +118,7 @@ const updateContributors = async ({ username, repo, imageSize }: ContributorOpti
};

updateContributors({
username: 'pkief',
owner: 'pkief',
repo: 'vscode-material-icon-theme',
imageSize: 40,
});

0 comments on commit c8b9a4f

Please sign in to comment.