chore: moved helpers in own file
This commit is contained in:
parent
ee824d11b3
commit
29bcf4188b
89
src/helpers.ts
Normal file
89
src/helpers.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import ejs from 'ejs';
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
import remark from 'remark';
|
||||||
|
import toc from 'remark-toc';
|
||||||
|
|
||||||
|
import MD_TEMPLATE from './template';
|
||||||
|
import GithubApi from './api';
|
||||||
|
import link from './link';
|
||||||
|
import git from './git';
|
||||||
|
|
||||||
|
import type { PaginationLink, ApiGetStarResponse } from './types';
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
const fsp = fs.promises;
|
||||||
|
|
||||||
|
export function wait(time = 200): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, time));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isLastPage(links: PaginationLink): boolean {
|
||||||
|
return links.next === links.last;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function renderer(
|
||||||
|
data: { [key: string]: any },
|
||||||
|
templateString = MD_TEMPLATE
|
||||||
|
): Promise<string> {
|
||||||
|
try {
|
||||||
|
return ejs.render(templateString, data);
|
||||||
|
} catch (error) {
|
||||||
|
core.error('#renderer');
|
||||||
|
core.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apiGetStar(url: string): Promise<ApiGetStarResponse> {
|
||||||
|
const { headers, body }: any = await GithubApi.get(url);
|
||||||
|
return {
|
||||||
|
data: body,
|
||||||
|
links: link.parse(headers.link).refs.reduce(
|
||||||
|
(acc, val) => ({
|
||||||
|
...acc,
|
||||||
|
[val.rel]: val.uri,
|
||||||
|
}),
|
||||||
|
{}
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const REPO_USERNAME = process.env.GITHUB_REPOSITORY?.split('/')[0];
|
||||||
|
export const API_STARRED_URL = `${process.env.GITHUB_API_URL}/users/${REPO_USERNAME}/starred`;
|
||||||
|
|
||||||
|
let links: PaginationLink = {
|
||||||
|
next: API_STARRED_URL,
|
||||||
|
last: undefined,
|
||||||
|
};
|
||||||
|
export async function paginate(): Promise<ApiGetStarResponse | null> {
|
||||||
|
if (!isLastPage(links)) return null;
|
||||||
|
const r = await apiGetStar(links.next);
|
||||||
|
links = r.links;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateMd(data: string): Promise<string> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
remark()
|
||||||
|
.use(toc)
|
||||||
|
.process(data, function (error, file) {
|
||||||
|
if (error) {
|
||||||
|
core.error('#generateMd');
|
||||||
|
core.error(error);
|
||||||
|
return resolve('');
|
||||||
|
}
|
||||||
|
return resolve(String(file));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const OUTPUT_FILENAME: string =
|
||||||
|
core.getInput('output-filename') || 'README.md';
|
||||||
|
export async function pushNewFile(markdown: string): Promise<any> {
|
||||||
|
await fsp.writeFile(OUTPUT_FILENAME, markdown);
|
||||||
|
await git.pull();
|
||||||
|
await git.add(OUTPUT_FILENAME);
|
||||||
|
await git.commit(`chore(${OUTPUT_FILENAME}): updated ${OUTPUT_FILENAME}`);
|
||||||
|
await git.push();
|
||||||
|
}
|
98
src/index.ts
98
src/index.ts
@ -1,86 +1,24 @@
|
|||||||
import fs from 'fs';
|
|
||||||
import ejs from 'ejs';
|
|
||||||
import remark from 'remark';
|
|
||||||
import toc from 'remark-toc';
|
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
|
|
||||||
import GithubApi from './api';
|
import {
|
||||||
import link from './link';
|
renderer,
|
||||||
import git from './git';
|
paginate,
|
||||||
import MD_TEMPLATE from './template';
|
REPO_USERNAME,
|
||||||
|
generateMd,
|
||||||
|
pushNewFile,
|
||||||
|
} from './helpers';
|
||||||
|
|
||||||
import type {
|
import type { SortedLanguageList, Stars, Star } from './types';
|
||||||
SortedLanguageList,
|
|
||||||
PaginationLink,
|
|
||||||
Stars,
|
|
||||||
Star,
|
|
||||||
ApiGetStarResponse,
|
|
||||||
} from './types';
|
|
||||||
|
|
||||||
const fsp = fs.promises;
|
|
||||||
|
|
||||||
const OUTPUT_FILENAME: string = core.getInput('output-filename') || 'README.md';
|
|
||||||
const REPO_USERNAME = process.env.GITHUB_REPOSITORY?.split('/')[0];
|
|
||||||
const API_STARRED_URL = `${process.env.GITHUB_API_URL}/users/${REPO_USERNAME}/starred`;
|
|
||||||
|
|
||||||
const renderer = async (data: any, templateString = MD_TEMPLATE) => {
|
|
||||||
try {
|
|
||||||
return ejs.render(templateString, data);
|
|
||||||
} catch (error) {
|
|
||||||
core.error('#renderer');
|
|
||||||
core.error(error);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const wait = (time = 200) =>
|
|
||||||
new Promise((resolve) => setTimeout(resolve, time));
|
|
||||||
|
|
||||||
async function apiGetStar(url: string): Promise<ApiGetStarResponse> {
|
|
||||||
const { headers, body }: any = await GithubApi.get(url);
|
|
||||||
return {
|
|
||||||
data: body,
|
|
||||||
links: link.parse(headers.link).refs.reduce(
|
|
||||||
(acc, val) => ({
|
|
||||||
...acc,
|
|
||||||
[val.rel]: val.uri,
|
|
||||||
}),
|
|
||||||
{}
|
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function isLastPage(links: PaginationLink): boolean {
|
|
||||||
return links.next === links.last;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateMd(data: string): Promise<string> {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
remark()
|
|
||||||
.use(toc)
|
|
||||||
.process(data, function (error, file) {
|
|
||||||
if (error) {
|
|
||||||
core.error('#generateMd');
|
|
||||||
core.error(error);
|
|
||||||
return resolve('');
|
|
||||||
}
|
|
||||||
return resolve(String(file));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function main(): Promise<any> {
|
export async function main(): Promise<any> {
|
||||||
let links: PaginationLink = {
|
|
||||||
next: API_STARRED_URL,
|
|
||||||
last: undefined,
|
|
||||||
};
|
|
||||||
let results: Stars = [];
|
let results: Stars = [];
|
||||||
do {
|
|
||||||
const r = await apiGetStar(links.next);
|
while (true) {
|
||||||
links = r.links;
|
// sorry.
|
||||||
|
const r = await paginate();
|
||||||
|
if (!r || r === null) break;
|
||||||
results = results.concat(r.data);
|
results = results.concat(r.data);
|
||||||
await wait();
|
}
|
||||||
} while (!isLastPage(links));
|
|
||||||
|
|
||||||
const sortedByLanguages = results.reduce(
|
const sortedByLanguages = results.reduce(
|
||||||
(acc: SortedLanguageList, val: Star) => {
|
(acc: SortedLanguageList, val: Star) => {
|
||||||
@ -103,13 +41,7 @@ export async function main(): Promise<any> {
|
|||||||
|
|
||||||
const markdown: string = await generateMd(rendered);
|
const markdown: string = await generateMd(rendered);
|
||||||
|
|
||||||
await fsp.writeFile(OUTPUT_FILENAME, markdown);
|
await pushNewFile(markdown);
|
||||||
|
|
||||||
await git.add(OUTPUT_FILENAME);
|
|
||||||
|
|
||||||
await git.commit(`chore(${OUTPUT_FILENAME}): updated ${OUTPUT_FILENAME}`);
|
|
||||||
|
|
||||||
await git.push();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function run(): Promise<any> {
|
export async function run(): Promise<any> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user