Skip to content

Commit 94fe394

Browse files
committed
refactor: convert project to esm module
1 parent 5397704 commit 94fe394

File tree

10 files changed

+35
-38
lines changed

10 files changed

+35
-38
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

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

2323
steps:

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# open-community-kit
22
Tools and stats for open-source communities
33

4-
![test](https://github.com/gitcommitshow/open-community-kit/actions/workflows/test.yml/badge.svg)
4+
[![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)
55

66
# Installation
77

@@ -39,10 +39,13 @@ ock yourGitHubOrgName yourGitHubPersonalToken
3939
### Using code
4040

4141
```javascript
42-
const OCK = require('open-community-kit').OCK;
43-
OCK.contributors.github.archive('your_github_org_or_username',
44-
{ GITHUB_PERSONAL_TOKEN: 'your_gh_personal_token_optional'
45-
});
42+
import OCK from 'open-community-kit';
43+
OCK.contributors.github.archive(
44+
'your_github_org_or_username',
45+
{
46+
GITHUB_PERSONAL_TOKEN: 'your_gh_personal_token_optional'
47+
}
48+
);
4649
```
4750

4851
## Settings for repeated usage

cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const OCK = require('./index').OCK;
3+
import OCK from './index.js';
44

55
let mainCommand = process.argv[1];
66
console.log("Running "+mainCommand+"...");

contributors.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
* @example To archive contributors leaderboard data in csv file, run `node contributors.js`
44
*/
55

6-
exports.archiveContributorsLeaderboard = archiveContributorsLeaderboard;
7-
exports.getAllRepos = getAllRepos;
8-
exports.getAllContributors = getAllContributors;
9-
exports.getRepoContributors = getRepoContributors;
6+
import * as path from 'path';
7+
import * as fs from 'fs';
108

11-
const path = require('path');
12-
13-
const { makeRequest } = require('./network.js');
9+
import { makeRequest } from './network.js';
1410

1511
// Configurations (Optional)
1612
// Repo owner that you want to analyze
@@ -37,7 +33,7 @@ if(GITHUB_PERSONAL_TOKEN){
3733
* @returns Promise<Array<Object> | String> JSON array of data on success, error on failure
3834
* @example getAllRepos('myorghandle').then((repos) => console.log(repos)).catch((err) => console.log(err))
3935
*/
40-
async function getAllRepos(owner=REPO_OWNER, options) {
36+
export async function getAllRepos(owner=REPO_OWNER, options) {
4137
let pageNo = (options && options.pageNo) ? options.pageNo : 1;
4238
if(options && options.GITHUB_PERSONAL_TOKEN){
4339
GITHUB_REQUEST_OPTIONS.headers["Authorization"] = "token "+options.GITHUB_PERSONAL_TOKEN;
@@ -68,7 +64,7 @@ async function getAllRepos(owner=REPO_OWNER, options) {
6864
* @returns Promise<Array<Object> | String>
6965
* @example getRepoContributors('myorghandle/myreponame').then((contributors) => console.log(contributors)).catch((err) => console.log(err))
7066
*/
71-
async function getRepoContributors(fullRepoName, pageNo = 1) {
67+
export async function getRepoContributors(fullRepoName, pageNo = 1) {
7268
let url = `https://api.github.com/repos/${fullRepoName}/contributors?per_page=100&page=${pageNo}`;
7369
console.log(url);
7470
const { res, data } = await makeRequest('GET', url, Object.assign({},GITHUB_REQUEST_OPTIONS));
@@ -93,7 +89,7 @@ async function getRepoContributors(fullRepoName, pageNo = 1) {
9389
* @param {string} owner github user or org handle
9490
* @param {Object} options Additional options
9591
*/
96-
async function getAllContributors(owner=REPO_OWNER, options) {
92+
export async function getAllContributors(owner=REPO_OWNER, options) {
9793
let repos = await getAllRepos(owner, options);
9894
if (!repos || repos.length < 1) {
9995
console.log("Error in getting repos for " + owner)
@@ -182,7 +178,6 @@ function writeContributorLeaderboardToFile(contributors, options={}) {
182178
}
183179
const ARCHIVE_FOLDER = options.archiveFolder || process.cwd();
184180
const ARCHIVE_FULL_PATH = path.join(ARCHIVE_FOLDER, options.archiveFileName || 'archive-gh-contributors-leaderboard.csv');
185-
const fs = require('fs');
186181
let ghContributorLeaderboard = contributors.map((contributor) => {
187182
return ["@" + contributor.login, contributor.contributions, contributor.html_url, contributor.avatar_url, contributor.topContributedRepo, contributor.allContributedRepos].join();
188183
}).join("\n");
@@ -200,7 +195,7 @@ function writeContributorLeaderboardToFile(contributors, options={}) {
200195
* @param {string} owner The organization or user name on GitHub
201196
* @param {Object} options Additional options
202197
*/
203-
async function archiveContributorsLeaderboard(owner=REPO_OWNER, options) {
198+
export async function archiveContributorsLeaderboard(owner=REPO_OWNER, options) {
204199
let contributors = await getAllContributors(owner, options);
205200
if (!contributors || contributors.length < 1) {
206201
console.log("Failed to get contributors for "+owner);

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const contributorsLib = require('./contributors');
1+
import * as contributorsLib from './contributors.js';
22

33
/**
44
* Bundling all APIs together
@@ -15,4 +15,4 @@ const OCK = {
1515
}
1616
}
1717

18-
exports.OCK = OCK;
18+
export default OCK

network.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const https = require('https');
2-
3-
exports.makeRequest = makeRequest;
1+
import * as https from 'https';
42

53
/**
64
* Sends an HTTPS request based on the specified method and options
@@ -23,7 +21,7 @@ exports.makeRequest = makeRequest;
2321
* }
2422
* }
2523
* */
26-
async function makeRequest(method, url, options) {
24+
export async function makeRequest(method, url, options) {
2725
return new Promise((resolve, reject) => {
2826
// Ensure options is an object and set the method
2927
options = typeof options === 'object' ? options : {};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.1.1",
44
"description": "Tools and stats for open-source communities",
55
"main": "contributors.js",
6+
"type": "module",
67
"bin": {
78
"ock": "cli.js",
89
"open-community-kit": "cli.js"

test/contributors.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { expect, assert } = require("chai");
2-
const contributorsLib = require("../contributors.js");
1+
import { expect, assert } from "chai";
2+
import * as contributorsLib from "../contributors.js";
33

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

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

test/fixtures/contributors.fixture.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
exports.VALID_REPO_OWNER = "Git-Commit-Show";
2-
exports.VALID_REPO = "Git-Commit-Show/landing-page";
3-
exports.REPO_COUNT_MIN = 10;
4-
exports.REPO_CONTRIBUTOR_COUNT_MIN = 10;
5-
exports.ALL_REPO_CONTRIBUTOR_COUNT_MIN = 49;
6-
exports.VALID_CONTRIBUTOR_SAMPLE = {
1+
export const VALID_REPO_OWNER = "Git-Commit-Show";
2+
export const VALID_REPO = "Git-Commit-Show/landing-page";
3+
export const REPO_COUNT_MIN = 10;
4+
export const REPO_CONTRIBUTOR_COUNT_MIN = 10;
5+
export const ALL_REPO_CONTRIBUTOR_COUNT_MIN = 49;
6+
export const VALID_CONTRIBUTOR_SAMPLE = {
77
login: "thenerdsuperuser",
88
id: 11832723,
99
node_id: "MDQ6VXNlcjExODMyNzIz",

test/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { expect, assert } = require("chai");
1+
import { expect, assert } from "chai";
22

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

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

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

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

0 commit comments

Comments
 (0)