Skip to content

Commit

Permalink
Merge pull request #15 from gitcommitshow/refactor-esm
Browse files Browse the repository at this point in the history
refactor: convert project to esm module
  • Loading branch information
gitcommitshow authored Dec 11, 2023
2 parents 5397704 + 94fe394 commit cac0676
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# open-community-kit
Tools and stats for open-source communities

![test](https://github.com/gitcommitshow/open-community-kit/actions/workflows/test.yml/badge.svg)
[![test](https://github.com/gitcommitshow/open-community-kit/actions/workflows/test.yml/badge.svg)](https://github.com/gitcommitshow/open-community-kit/actions/workflows/test.yml)

# Installation

Expand Down Expand Up @@ -39,10 +39,13 @@ ock yourGitHubOrgName yourGitHubPersonalToken
### Using code

```javascript
const OCK = require('open-community-kit').OCK;
OCK.contributors.github.archive('your_github_org_or_username',
{ GITHUB_PERSONAL_TOKEN: 'your_gh_personal_token_optional'
});
import OCK from 'open-community-kit';
OCK.contributors.github.archive(
'your_github_org_or_username',
{
GITHUB_PERSONAL_TOKEN: 'your_gh_personal_token_optional'
}
);
```

## Settings for repeated usage
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const OCK = require('./index').OCK;
import OCK from './index.js';

let mainCommand = process.argv[1];
console.log("Running "+mainCommand+"...");
Expand Down
19 changes: 7 additions & 12 deletions contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
* @example To archive contributors leaderboard data in csv file, run `node contributors.js`
*/

exports.archiveContributorsLeaderboard = archiveContributorsLeaderboard;
exports.getAllRepos = getAllRepos;
exports.getAllContributors = getAllContributors;
exports.getRepoContributors = getRepoContributors;
import * as path from 'path';
import * as fs from 'fs';

const path = require('path');

const { makeRequest } = require('./network.js');
import { makeRequest } from './network.js';

// Configurations (Optional)
// Repo owner that you want to analyze
Expand All @@ -37,7 +33,7 @@ if(GITHUB_PERSONAL_TOKEN){
* @returns Promise<Array<Object> | String> JSON array of data on success, error on failure
* @example getAllRepos('myorghandle').then((repos) => console.log(repos)).catch((err) => console.log(err))
*/
async function getAllRepos(owner=REPO_OWNER, options) {
export async function getAllRepos(owner=REPO_OWNER, options) {
let pageNo = (options && options.pageNo) ? options.pageNo : 1;
if(options && options.GITHUB_PERSONAL_TOKEN){
GITHUB_REQUEST_OPTIONS.headers["Authorization"] = "token "+options.GITHUB_PERSONAL_TOKEN;
Expand Down Expand Up @@ -68,7 +64,7 @@ async function getAllRepos(owner=REPO_OWNER, options) {
* @returns Promise<Array<Object> | String>
* @example getRepoContributors('myorghandle/myreponame').then((contributors) => console.log(contributors)).catch((err) => console.log(err))
*/
async function getRepoContributors(fullRepoName, pageNo = 1) {
export async function getRepoContributors(fullRepoName, pageNo = 1) {
let url = `https://api.github.com/repos/${fullRepoName}/contributors?per_page=100&page=${pageNo}`;
console.log(url);
const { res, data } = await makeRequest('GET', url, Object.assign({},GITHUB_REQUEST_OPTIONS));
Expand All @@ -93,7 +89,7 @@ async function getRepoContributors(fullRepoName, pageNo = 1) {
* @param {string} owner github user or org handle
* @param {Object} options Additional options
*/
async function getAllContributors(owner=REPO_OWNER, options) {
export async function getAllContributors(owner=REPO_OWNER, options) {
let repos = await getAllRepos(owner, options);
if (!repos || repos.length < 1) {
console.log("Error in getting repos for " + owner)
Expand Down Expand Up @@ -182,7 +178,6 @@ function writeContributorLeaderboardToFile(contributors, options={}) {
}
const ARCHIVE_FOLDER = options.archiveFolder || process.cwd();
const ARCHIVE_FULL_PATH = path.join(ARCHIVE_FOLDER, options.archiveFileName || 'archive-gh-contributors-leaderboard.csv');
const fs = require('fs');
let ghContributorLeaderboard = contributors.map((contributor) => {
return ["@" + contributor.login, contributor.contributions, contributor.html_url, contributor.avatar_url, contributor.topContributedRepo, contributor.allContributedRepos].join();
}).join("\n");
Expand All @@ -200,7 +195,7 @@ function writeContributorLeaderboardToFile(contributors, options={}) {
* @param {string} owner The organization or user name on GitHub
* @param {Object} options Additional options
*/
async function archiveContributorsLeaderboard(owner=REPO_OWNER, options) {
export async function archiveContributorsLeaderboard(owner=REPO_OWNER, options) {
let contributors = await getAllContributors(owner, options);
if (!contributors || contributors.length < 1) {
console.log("Failed to get contributors for "+owner);
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const contributorsLib = require('./contributors');
import * as contributorsLib from './contributors.js';

/**
* Bundling all APIs together
Expand All @@ -15,4 +15,4 @@ const OCK = {
}
}

exports.OCK = OCK;
export default OCK
6 changes: 2 additions & 4 deletions network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const https = require('https');

exports.makeRequest = makeRequest;
import * as https from 'https';

/**
* Sends an HTTPS request based on the specified method and options
Expand All @@ -23,7 +21,7 @@ exports.makeRequest = makeRequest;
* }
* }
* */
async function makeRequest(method, url, options) {
export async function makeRequest(method, url, options) {
return new Promise((resolve, reject) => {
// Ensure options is an object and set the method
options = typeof options === 'object' ? options : {};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.1.1",
"description": "Tools and stats for open-source communities",
"main": "contributors.js",
"type": "module",
"bin": {
"ock": "cli.js",
"open-community-kit": "cli.js"
Expand Down
6 changes: 3 additions & 3 deletions test/contributors.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect, assert } = require("chai");
const contributorsLib = require("../contributors.js");
import { expect, assert } from "chai";
import * as contributorsLib from "../contributors.js";

const contributorsFixture = require('./fixtures/contributors.fixture.js');
import * as contributorsFixture from './fixtures/contributors.fixture.js';

describe('contibutors.js', function() {

Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/contributors.fixture.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
exports.VALID_REPO_OWNER = "Git-Commit-Show";
exports.VALID_REPO = "Git-Commit-Show/landing-page";
exports.REPO_COUNT_MIN = 10;
exports.REPO_CONTRIBUTOR_COUNT_MIN = 10;
exports.ALL_REPO_CONTRIBUTOR_COUNT_MIN = 49;
exports.VALID_CONTRIBUTOR_SAMPLE = {
export const VALID_REPO_OWNER = "Git-Commit-Show";
export const VALID_REPO = "Git-Commit-Show/landing-page";
export const REPO_COUNT_MIN = 10;
export const REPO_CONTRIBUTOR_COUNT_MIN = 10;
export const ALL_REPO_CONTRIBUTOR_COUNT_MIN = 49;
export const VALID_CONTRIBUTOR_SAMPLE = {
login: "thenerdsuperuser",
id: 11832723,
node_id: "MDQ6VXNlcjExODMyNzIz",
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { expect, assert } = require("chai");
import { expect, assert } from "chai";

const { OCK } = require("../index.js");
import OCK from "../index.js";

const contributorsFixture = require("./fixtures/contributors.fixture.js");
import * as contributorsFixture from "./fixtures/contributors.fixture.js";

describe('index.js', function() {

Expand All @@ -15,7 +15,7 @@ describe('index.js', function() {
let contributorsHandlesArray = await OCK.contributors.github.archive(contributorsFixture.VALID_REPO_OWNER);
assert.isNotNull(contributorsHandlesArray, "No contributors github handles returned");
expect(contributorsHandlesArray).to.be.an('array');
expect(contributorsHandlesArray).to.have.lengthOf.at.least(contributorsFixture.ALL_REPO_CONTRIBUTOR_COUNT);
expect(contributorsHandlesArray).to.have.lengthOf.at.least(contributorsFixture.ALL_REPO_CONTRIBUTOR_COUNT_MIN);
})
})

Expand Down

0 comments on commit cac0676

Please sign in to comment.