Compare commits
72 Commits
v1.1.0-nex
...
v2.0.0
Author | SHA1 | Date | |
---|---|---|---|
|
f444d98079 | ||
|
754bdf3f68 | ||
|
51a4359d98 | ||
|
3b3f3777f6 | ||
|
236068f866 | ||
|
3ec4b7cd53 | ||
|
3170a35260 | ||
|
6cd9c30b20 | ||
|
e1f37af978 | ||
|
19c4c8f761 | ||
|
a141d23972 | ||
|
7ade95df85 | ||
|
445f562fb5 | ||
|
ec10b79a91 | ||
|
55cd9e9c58 | ||
|
f70a021b83 | ||
|
b24f13b9e9 | ||
|
f88cf3357e | ||
|
ce053290e3 | ||
|
57651eab5a | ||
|
346ba5d4b7 | ||
|
459ca45678 | ||
|
d1d4edd104 | ||
|
2c742b8205 | ||
|
9d140076f7 | ||
|
8ee913b561 | ||
|
78225b6f7e | ||
|
3ba4012b02 | ||
|
12f2bef8e5 | ||
|
b305b5276d | ||
|
0ab33ae6e8 | ||
|
3e66cb8b35 | ||
|
a6f431ff5c | ||
|
3e35a8e8e3 | ||
|
db410801f9 | ||
|
fcffe08be6 | ||
|
6f7de5aaff | ||
|
a448ddef8a | ||
|
b6dc6a44ec | ||
|
2f4a6f83a7 | ||
|
34f5d7369b | ||
|
1a46ff531e | ||
|
3ef135614f | ||
|
586e741afc | ||
|
3888e0bcd6 | ||
|
2e122981f5 | ||
|
1f2f524430 | ||
|
b7388a0b80 | ||
|
4cf5a6b2ec | ||
|
c39e5e788b | ||
|
4567c63113 | ||
|
5d5127ad06 | ||
|
70c8d54fb9 | ||
|
2459ad324b | ||
|
aa17d94db3 | ||
|
fe4ad219ed | ||
|
c0e3e60f4f | ||
|
4b365c4b69 | ||
|
00de1fc0c3 | ||
|
cec7c043b3 | ||
|
83853a2d61 | ||
|
24c8b9889e | ||
|
20b279668f | ||
|
fe70a95e4d | ||
|
f855c17ccb | ||
|
16a641ace0 | ||
|
b50b8ce93a | ||
|
0b0b70d114 | ||
|
c0d2fb6135 | ||
|
12db35c2c1 | ||
|
07ef9724cb | ||
|
d2477f5a96 |
21
.eslintrc
21
.eslintrc
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2020,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"env": {
|
||||
"es6": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": [
|
||||
"prettier/@typescript-eslint",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:prettier/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-var-requires": "off",
|
||||
"prefer-rest-params": "off",
|
||||
"@typescript-eslint/no-non-null-assertion": "off"
|
||||
}
|
||||
}
|
17
.eslintrc.js
Normal file
17
.eslintrc.js
Normal file
@@ -0,0 +1,17 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
parserOptions: {
|
||||
ecmaVersion: 2022,
|
||||
sourceType: 'module',
|
||||
},
|
||||
env: {
|
||||
node: true,
|
||||
},
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: ['@typescript-eslint'],
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:prettier/recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
],
|
||||
};
|
21
.github/workflows/merge.yml
vendored
21
.github/workflows/merge.yml
vendored
@@ -1,21 +0,0 @@
|
||||
name: Run tests
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
node-version: [16.x]
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm it
|
24
.github/workflows/release.yml
vendored
24
.github/workflows/release.yml
vendored
@@ -3,26 +3,7 @@ name: Release
|
||||
on: [workflow_dispatch]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
node-version: [16.x]
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm it
|
||||
|
||||
release:
|
||||
needs: [test]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/setup-node@v3
|
||||
@@ -30,7 +11,8 @@ jobs:
|
||||
node-version: 16
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
token: ${{ secrets.GH_TOKEN }}
|
||||
persist-credentials: false
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm i
|
||||
@@ -41,6 +23,6 @@ jobs:
|
||||
- name: Semantic release
|
||||
uses: codfish/semantic-release-action@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
GIT_AUTHOR_NAME: '${{ github.actor }}'
|
||||
GIT_COMMITTER_NAME: '${{ github.actor }}'
|
||||
|
@@ -10,7 +10,12 @@
|
||||
[
|
||||
"@semantic-release/git",
|
||||
{
|
||||
"assets": ["CHANGELOG.md", "package.json", "package-lock.json"]
|
||||
"assets": [
|
||||
"index.js",
|
||||
"CHANGELOG.md",
|
||||
"package.json",
|
||||
"package-lock.json"
|
||||
]
|
||||
}
|
||||
],
|
||||
"@semantic-release/github",
|
||||
|
77
CHANGELOG.md
77
CHANGELOG.md
@@ -1,35 +1,70 @@
|
||||
# [2.0.0](https://github.com/simonecorsi/mawesome/compare/v1.0.45...v2.0.0) (2022-05-11)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* fixes default template path ([6cd9c30](https://github.com/simonecorsi/mawesome/commit/6cd9c30b20acb0789668b9fd4cdbace2cb52d3ce))
|
||||
* fixes tsc build ([ec10b79](https://github.com/simonecorsi/mawesome/commit/ec10b79a91bc5894d35b80026d3e216420e0721a))
|
||||
* normalize template file loadup ([445f562](https://github.com/simonecorsi/mawesome/commit/445f562fb50567d995f0d080d4267fc8d494731b))
|
||||
* pre-tags ([d1d4edd](https://github.com/simonecorsi/mawesome/commit/d1d4edd104affc69984905c8408e859c25c58443))
|
||||
* removes unused tests ([a141d23](https://github.com/simonecorsi/mawesome/commit/a141d23972c31b3dbd7e9841168219ad42fa7a18))
|
||||
* reworking files ([19c4c8f](https://github.com/simonecorsi/mawesome/commit/19c4c8f761b244ddccbc445cc34078bf932559d2))
|
||||
* updates gh-star-fetch ([3ec4b7c](https://github.com/simonecorsi/mawesome/commit/3ec4b7cd53c1fe885a51fb64279047a201d535dc))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* release major ([51a4359](https://github.com/simonecorsi/mawesome/commit/51a4359d983be4c842410f0c62104fca1b28252f))
|
||||
* update to node16 ([e1f37af](https://github.com/simonecorsi/mawesome/commit/e1f37af978ebcb7f770949476ac7d6bc788a1fc2))
|
||||
* updates deps ([7ade95d](https://github.com/simonecorsi/mawesome/commit/7ade95df8566a59145652165400cddfd1afa4bed))
|
||||
* using gh-star-fetch ([346ba5d](https://github.com/simonecorsi/mawesome/commit/346ba5d4b7ba6a71bab99f2dbe3c2d010beb67d5))
|
||||
* **template:** adds templates ([2c742b8](https://github.com/simonecorsi/mawesome/commit/2c742b820558fd715de987178303c460f5871c29)), closes [#14](https://github.com/simonecorsi/mawesome/issues/14)
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* refactored code
|
||||
|
||||
# [1.1.0-next.2](https://github.com/simonecorsi/mawesome/compare/v1.1.0-next.1...v1.1.0-next.2) (2022-05-09)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* using gh-star-fetch ([a8b6577](https://github.com/simonecorsi/mawesome/commit/a8b657735b9879636cc039d79fddcdca33ccf38e))
|
||||
|
||||
# [1.1.0-next.1](https://github.com/simonecorsi/mawesome/compare/v1.0.45...v1.1.0-next.1) (2022-04-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* pre-tags ([602befc](https://github.com/simonecorsi/mawesome/commit/602befcb5494b5bfd74d2333899311b81ea9da6d))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **template:** adds templates ([791de9a](https://github.com/simonecorsi/mawesome/commit/791de9ab504de50e2e9cb031b9e373d7cc0589c0)), closes [#14](https://github.com/simonecorsi/mawesome/issues/14)
|
||||
|
||||
## <small>1.0.45 (2022-01-17)</small>
|
||||
|
||||
* build(deps): bump shelljs from 0.8.4 to 0.8.5 ([1da350a](https://github.com/simonecorsi/mawesome/commit/1da350a))
|
||||
|
||||
|
||||
- build(deps): bump shelljs from 0.8.4 to 0.8.5 ([1da350a](https://github.com/simonecorsi/mawesome/commit/1da350a))
|
||||
|
||||
## <small>1.0.44 (2021-10-26)</small>
|
||||
|
||||
* chore(release): v1.0.44 ([6ffe17f](https://github.com/simonecorsi/mawesome/commit/6ffe17f))
|
||||
* feat: git add multiple files at once ([4aef61f](https://github.com/simonecorsi/mawesome/commit/4aef61f))
|
||||
|
||||
|
||||
- chore(release): v1.0.44 ([6ffe17f](https://github.com/simonecorsi/mawesome/commit/6ffe17f))
|
||||
- feat: git add multiple files at once ([4aef61f](https://github.com/simonecorsi/mawesome/commit/4aef61f))
|
||||
|
||||
## <small>1.0.43 (2021-10-15)</small>
|
||||
|
||||
* chore(release): v1.0.43 ([5e0ef82](https://github.com/simonecorsi/mawesome/commit/5e0ef82))
|
||||
* feat: reduce json output size ([94635fe](https://github.com/simonecorsi/mawesome/commit/94635fe))
|
||||
|
||||
|
||||
- chore(release): v1.0.43 ([5e0ef82](https://github.com/simonecorsi/mawesome/commit/5e0ef82))
|
||||
- feat: reduce json output size ([94635fe](https://github.com/simonecorsi/mawesome/commit/94635fe))
|
||||
|
||||
## <small>1.0.42 (2021-10-14)</small>
|
||||
|
||||
* chore(release): v1.0.42 ([5b36813](https://github.com/simonecorsi/mawesome/commit/5b36813))
|
||||
* fix(paginator): last page now correctly matches rex ([dcf9898](https://github.com/simonecorsi/mawesome/commit/dcf9898))
|
||||
|
||||
|
||||
- chore(release): v1.0.42 ([5b36813](https://github.com/simonecorsi/mawesome/commit/5b36813))
|
||||
- fix(paginator): last page now correctly matches rex ([dcf9898](https://github.com/simonecorsi/mawesome/commit/dcf9898))
|
||||
|
||||
## <small>1.0.41 (2021-10-14)</small>
|
||||
|
||||
* chore(release): v1.0.41 ([884fc90](https://github.com/simonecorsi/mawesome/commit/884fc90))
|
||||
* test: fixs suite ([b97833f](https://github.com/simonecorsi/mawesome/commit/b97833f))
|
||||
* fix: should avoid index lock ([1d6848c](https://github.com/simonecorsi/mawesome/commit/1d6848c))
|
||||
|
||||
|
||||
|
||||
- chore(release): v1.0.41 ([884fc90](https://github.com/simonecorsi/mawesome/commit/884fc90))
|
||||
- test: fixs suite ([b97833f](https://github.com/simonecorsi/mawesome/commit/b97833f))
|
||||
- fix: should avoid index lock ([1d6848c](https://github.com/simonecorsi/mawesome/commit/1d6848c))
|
||||
|
24
README.md
24
README.md
@@ -8,7 +8,6 @@ You can see an example of the output at my own [simonecorsi/awesome](https://git
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Documentation](#documentation)
|
||||
- [Requirements](#requirements)
|
||||
- [Configuration](#configuration)
|
||||
@@ -21,32 +20,34 @@ You can see an example of the output at my own [simonecorsi/awesome](https://git
|
||||
|
||||
### Requirements
|
||||
|
||||
- An empty repository
|
||||
- A personal github api key
|
||||
- An empty repository
|
||||
- A personal github api key
|
||||
|
||||
### Configuration
|
||||
|
||||
The service can be configured setting the appropriate environment variables or writing an `.env` file.
|
||||
|
||||
| Variable | Description | Default |
|
||||
| -------------- | ------------------------------------------- | -------------------------------- |
|
||||
| `api-token` | Personal github api token. | `${{ secrets.API_TOKEN }}` |
|
||||
| `github-token` | Action Token | `${{ secrets.GITHUB_TOKEN }}` |
|
||||
| `github-name` | Name used for the commit, default to action | Github Action |
|
||||
| `github-email` | email used for commit, default to action | actions@users.noreply.github.com |
|
||||
| Variable | Description | Default |
|
||||
| ----------------- | -------------------------------------------------------------------- | -------------------------------- |
|
||||
| `api-token` | Personal github api token. | `${{ secrets.API_TOKEN }}` |
|
||||
| `github-token` | Action Token | `${{ secrets.GITHUB_TOKEN }}` |
|
||||
| `github-name` | Name used for the commit, default to action | Github Action |
|
||||
| `github-email` | email used for commit, default to action | actions@users.noreply.github.com |
|
||||
| `template-path` | Custom output template file ([EJS](https://ejs.co/) template engine) | [TEMPLATE.ejs](./TEMPLATE.ejs) |
|
||||
| `output-filename` | Output filename | `README.md` |
|
||||
|
||||
#### `api-token`
|
||||
|
||||
The Personal API Access Token is mandatory to fetch stars from the API without incurring in Rate Limits.
|
||||
|
||||
You'll have to generate a [personal api token](https://github.com/settings/tokens/new) and then add
|
||||
You'll have to generate a [personal api token](https://github.com/settings/tokens/new) and then add
|
||||
|
||||
## Example workflow
|
||||
|
||||
```yml
|
||||
name: Update awesome list
|
||||
|
||||
on:
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
@@ -63,5 +64,4 @@ jobs:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
github-email: ${{ secrets.USER_EMAIL }}
|
||||
github-name: ${{ github.repository_owner }}
|
||||
|
||||
```
|
||||
|
@@ -1,4 +1,4 @@
|
||||
export default `# <%= username %> Awesome List [](https://github.com/sindresorhus/awesome)
|
||||
# <%= username %> Awesome List [](https://github.com/sindresorhus/awesome)
|
||||
|
||||
> :star: generated with [simonecorsi/mawesome](https://github.com/simonecorsi/mawesome)
|
||||
|
||||
@@ -11,4 +11,3 @@ export default `# <%= username %> Awesome List [;
|
||||
|
||||
export default got.extend({
|
||||
headers: {
|
||||
Authorization: `token ${GITHUB_TOKEN}`,
|
||||
},
|
||||
responseType: 'json',
|
||||
});
|
55
src/git.ts
55
src/git.ts
@@ -2,10 +2,17 @@
|
||||
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import fs from 'fs/promises';
|
||||
|
||||
const { GITHUB_REPOSITORY, GITHUB_REF } = process.env;
|
||||
|
||||
const branch = GITHUB_REF?.replace('refs/heads/', '');
|
||||
|
||||
type File = {
|
||||
filename: string;
|
||||
data: string;
|
||||
};
|
||||
|
||||
class Git {
|
||||
constructor() {
|
||||
const githubToken = core.getInput('github-token', { required: true });
|
||||
@@ -34,28 +41,26 @@ class Git {
|
||||
return isShallow.trim().replace('\n', '') === 'true';
|
||||
};
|
||||
|
||||
exec = (command: string): Promise<string> => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let execOutput = '';
|
||||
async exec(command: string): Promise<string> {
|
||||
let execOutput = '';
|
||||
|
||||
const options = {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
execOutput += data.toString();
|
||||
},
|
||||
const options = {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
execOutput += data.toString();
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const exitCode = await exec.exec(`git ${command}`, undefined, options);
|
||||
const exitCode = await exec.exec(`git ${command}`, undefined, options);
|
||||
|
||||
if (exitCode === 0) {
|
||||
return resolve(execOutput);
|
||||
} else {
|
||||
core.error(`Command "git ${command}" exited with code ${exitCode}.`);
|
||||
return reject(`Command "git ${command}" exited with code ${exitCode}.`);
|
||||
}
|
||||
});
|
||||
};
|
||||
if (exitCode === 0) {
|
||||
return execOutput;
|
||||
} else {
|
||||
core.error(`Command "git ${command}" exited with code ${exitCode}.`);
|
||||
throw new Error(`Command "git ${command}" exited with code ${exitCode}.`);
|
||||
}
|
||||
}
|
||||
|
||||
config = (prop: string, value: string) =>
|
||||
this.exec(`config ${prop} "${value}"`);
|
||||
@@ -91,6 +96,20 @@ class Git {
|
||||
updateOrigin = (repo: string) => this.exec(`remote set-url origin ${repo}`);
|
||||
|
||||
createTag = (tag: string) => this.exec(`tag -a ${tag} -m "${tag}"`);
|
||||
|
||||
async pushNewFiles(files: File[] = []): Promise<any> {
|
||||
if (!files.length) return;
|
||||
|
||||
await this.pull();
|
||||
|
||||
await Promise.all(
|
||||
files.map(({ filename, data }) => fs.writeFile(filename, data))
|
||||
);
|
||||
|
||||
await this.add(files.map(({ filename }) => filename));
|
||||
await this.commit(`chore(updates): updated entries in files`);
|
||||
await this.push();
|
||||
}
|
||||
}
|
||||
|
||||
export default new Git();
|
||||
|
108
src/helpers.ts
108
src/helpers.ts
@@ -1,28 +1,14 @@
|
||||
import fs from 'fs';
|
||||
import ejs from 'ejs';
|
||||
import * as core from '@actions/core';
|
||||
import remark from 'remark';
|
||||
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, Stars, Star } from './types';
|
||||
|
||||
export const REPO_USERNAME = process.env.GITHUB_REPOSITORY?.split('/')[0];
|
||||
export const API_STARRED_URL = `${process.env.GITHUB_API_URL}/users/${REPO_USERNAME}/starred`;
|
||||
|
||||
const fsp = fs.promises;
|
||||
|
||||
export function wait(time = 200): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, time));
|
||||
}
|
||||
|
||||
export async function renderer(
|
||||
data: { [key: string]: any },
|
||||
templateString = MD_TEMPLATE
|
||||
templateString: string
|
||||
): Promise<string> {
|
||||
try {
|
||||
return ejs.render(templateString, data);
|
||||
@@ -32,74 +18,6 @@ export async function renderer(
|
||||
}
|
||||
}
|
||||
|
||||
export function getNextPage(links: PaginationLink[]): string | null {
|
||||
const next = links.find((l) => l.rel === 'next');
|
||||
const last = links.find((l) => l.rel === 'last');
|
||||
if (!next || !last) return null;
|
||||
const matchNext = next.uri.match(/page=([0-9]*)/);
|
||||
const matchLast = last.uri.match(/page=([0-9]*)/);
|
||||
if (!matchNext || !matchLast) return null;
|
||||
if (matchNext[1] === matchLast[1]) return null;
|
||||
return matchNext[1];
|
||||
}
|
||||
|
||||
async function* paginateStars(url: string): AsyncGenerator<Stars> {
|
||||
let nextPage: string | null = '1';
|
||||
while (nextPage) {
|
||||
try {
|
||||
const { headers, body } = await GithubApi.get(url, {
|
||||
searchParams: {
|
||||
page: nextPage,
|
||||
},
|
||||
});
|
||||
yield (body as unknown) as Stars;
|
||||
nextPage = getNextPage(link.parse(headers.link).refs);
|
||||
await wait(1000); // avoid limits
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiGetStar(
|
||||
url: string = API_STARRED_URL
|
||||
): Promise<ApiGetStarResponse> {
|
||||
const data: Partial<Star>[] = [];
|
||||
for await (const stars of paginateStars(url)) {
|
||||
for (const star of stars) {
|
||||
data.push({
|
||||
id: star.id,
|
||||
node_id: star.node_id,
|
||||
name: star.name,
|
||||
full_name: star.full_name,
|
||||
owner: {
|
||||
login: star?.owner?.login,
|
||||
id: star?.owner?.id,
|
||||
avatar_url: star?.owner?.avatar_url,
|
||||
url: star?.owner?.url,
|
||||
html_url: star?.owner?.html_url,
|
||||
},
|
||||
html_url: star.html_url,
|
||||
description: star.description,
|
||||
url: star.url,
|
||||
languages_url: star.languages_url,
|
||||
created_at: star.created_at,
|
||||
updated_at: star.updated_at,
|
||||
git_url: star.git_url,
|
||||
ssh_url: star.ssh_url,
|
||||
clone_url: star.clone_url,
|
||||
homepage: star.homepage,
|
||||
stargazers_count: star.stargazers_count,
|
||||
watchers_count: star.watchers_count,
|
||||
language: star.language,
|
||||
topics: star.topics,
|
||||
} as Partial<Star>);
|
||||
}
|
||||
}
|
||||
return (data as unknown) as Stars;
|
||||
}
|
||||
|
||||
export function generateMd(data: string): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
remark()
|
||||
@@ -115,24 +33,4 @@ export function generateMd(data: string): Promise<string> {
|
||||
});
|
||||
}
|
||||
|
||||
export const MARKDOWN_FILENAME: string =
|
||||
core.getInput('output-filename') || 'README.md';
|
||||
|
||||
type File = {
|
||||
filename: string;
|
||||
data: string;
|
||||
};
|
||||
|
||||
export async function pushNewFiles(files: File[] = []): Promise<any> {
|
||||
if (!files.length) return;
|
||||
|
||||
await git.pull();
|
||||
|
||||
await Promise.all(
|
||||
files.map(({ filename, data }) => fsp.writeFile(filename, data))
|
||||
);
|
||||
|
||||
await git.add(files.map(({ filename }) => filename));
|
||||
await git.commit(`chore(updates): updated entries in files`);
|
||||
await git.push();
|
||||
}
|
||||
export const MARKDOWN_FILENAME: string = core.getInput('output-filename');
|
||||
|
60
src/index.ts
60
src/index.ts
@@ -1,41 +1,50 @@
|
||||
import path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
import { readFile } from 'fs/promises';
|
||||
import ghStarFetch from 'gh-star-fetch';
|
||||
|
||||
import {
|
||||
renderer,
|
||||
REPO_USERNAME,
|
||||
generateMd,
|
||||
pushNewFiles,
|
||||
MARKDOWN_FILENAME,
|
||||
apiGetStar,
|
||||
} from './helpers';
|
||||
import git from './git';
|
||||
|
||||
import type { SortedLanguageList, Stars, Star } from './types';
|
||||
|
||||
export async function main(): Promise<any> {
|
||||
const results: Stars = await apiGetStar();
|
||||
|
||||
const sortedByLanguages = results.reduce(
|
||||
(acc: SortedLanguageList, val: Star) => {
|
||||
const language = val.language || 'generic';
|
||||
if (!acc[language]) {
|
||||
acc[language] = [val];
|
||||
} else {
|
||||
acc[language].push(val);
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
export async function main() {
|
||||
// set default template
|
||||
let template = await readFile(
|
||||
path.resolve(__dirname, './TEMPLATE.ejs'),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
const rendered = await renderer({
|
||||
username: REPO_USERNAME,
|
||||
stars: Object.entries(sortedByLanguages),
|
||||
updatedAt: Date.now(),
|
||||
// get template if found in the repo
|
||||
const customTemplatePath = core.getInput('template-path');
|
||||
core.info(`check if customTemplatePath: ${customTemplatePath} exists`);
|
||||
try {
|
||||
template = await readFile(customTemplatePath, 'utf8');
|
||||
} catch {
|
||||
core.info("Couldn't find template file, using default");
|
||||
}
|
||||
|
||||
const sortedByLanguages = await ghStarFetch({
|
||||
accessToken: core.getInput('api-token', { required: true }),
|
||||
compactByLanguage: true,
|
||||
});
|
||||
console.log('sortedByLanguages :>> ', sortedByLanguages);
|
||||
|
||||
const rendered = await renderer(
|
||||
{
|
||||
username: REPO_USERNAME,
|
||||
stars: Object.entries(sortedByLanguages),
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
template
|
||||
);
|
||||
|
||||
const markdown: string = await generateMd(rendered);
|
||||
|
||||
await pushNewFiles([
|
||||
await git.pushNewFiles([
|
||||
{
|
||||
filename: MARKDOWN_FILENAME,
|
||||
data: markdown,
|
||||
@@ -47,7 +56,7 @@ export async function main(): Promise<any> {
|
||||
]);
|
||||
}
|
||||
|
||||
export async function run(): Promise<any> {
|
||||
export async function run(): Promise<void> {
|
||||
try {
|
||||
await main();
|
||||
} catch (error) {
|
||||
@@ -57,8 +66,9 @@ export async function run(): Promise<any> {
|
||||
|
||||
const catchAll = (info: any) => {
|
||||
core.setFailed(`#catchAll: ${info}`);
|
||||
core.error(info);
|
||||
};
|
||||
process.on('unhandledRejection', catchAll);
|
||||
process.on('uncaughtException', catchAll);
|
||||
|
||||
run();
|
||||
run().catch(core.error);
|
||||
|
384
src/link.js
384
src/link.js
@@ -1,384 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2016 Jonas Hermsmeier
|
||||
* https://github.com/jhermsmeier/node-http-link-header
|
||||
*/
|
||||
|
||||
/* istanbul ignore file */
|
||||
'use strict';
|
||||
|
||||
var COMPATIBLE_ENCODING_PATTERN = /^utf-?8|ascii|utf-?16-?le|ucs-?2|base-?64|latin-?1$/i;
|
||||
var WS_TRIM_PATTERN = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
|
||||
var WS_CHAR_PATTERN = /\s|\uFEFF|\xA0/;
|
||||
var WS_FOLD_PATTERN = /\r?\n[\x20\x09]+/g;
|
||||
var DELIMITER_PATTERN = /[;,"]/;
|
||||
var WS_DELIMITER_PATTERN = /[;,"]|\s/;
|
||||
|
||||
/**
|
||||
* Token character pattern
|
||||
* @type {RegExp}
|
||||
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
|
||||
*/
|
||||
var TOKEN_PATTERN = /^[!#$%&'*+\-\.^_`|~\da-zA-Z]+$/;
|
||||
|
||||
var STATE = {
|
||||
IDLE: 1 << 0,
|
||||
URI: 1 << 1,
|
||||
ATTR: 1 << 2,
|
||||
};
|
||||
|
||||
function trim(value) {
|
||||
return value.replace(WS_TRIM_PATTERN, '');
|
||||
}
|
||||
|
||||
function hasWhitespace(value) {
|
||||
return WS_CHAR_PATTERN.test(value);
|
||||
}
|
||||
|
||||
function skipWhitespace(value, offset) {
|
||||
while (hasWhitespace(value[offset])) {
|
||||
offset++;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
function needsQuotes(value) {
|
||||
return WS_DELIMITER_PATTERN.test(value) || !TOKEN_PATTERN.test(value);
|
||||
}
|
||||
|
||||
class Link {
|
||||
/**
|
||||
* Link
|
||||
* @constructor
|
||||
* @param {String} [value]
|
||||
* @returns {Link}
|
||||
*/
|
||||
constructor(value) {
|
||||
/** @type {Array} URI references */
|
||||
this.refs = [];
|
||||
|
||||
if (value) {
|
||||
this.parse(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get refs with given relation type
|
||||
* @param {String} value
|
||||
* @returns {Array<Object>}
|
||||
*/
|
||||
rel(value) {
|
||||
var links = [];
|
||||
var type = value.toLowerCase();
|
||||
|
||||
for (var i = 0; i < this.refs.length; i++) {
|
||||
if (this.refs[i].rel.toLowerCase() === type) {
|
||||
links.push(this.refs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get refs where given attribute has a given value
|
||||
* @param {String} attr
|
||||
* @param {String} value
|
||||
* @returns {Array<Object>}
|
||||
*/
|
||||
get(attr, value) {
|
||||
attr = attr.toLowerCase();
|
||||
|
||||
var links = [];
|
||||
|
||||
for (var i = 0; i < this.refs.length; i++) {
|
||||
if (this.refs[i][attr] === value) {
|
||||
links.push(this.refs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return links;
|
||||
}
|
||||
|
||||
set(link) {
|
||||
this.refs.push(link);
|
||||
return this;
|
||||
}
|
||||
|
||||
has(attr, value) {
|
||||
attr = attr.toLowerCase();
|
||||
|
||||
for (var i = 0; i < this.refs.length; i++) {
|
||||
if (this.refs[i][attr] === value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
parse(value, offset) {
|
||||
offset = offset || 0;
|
||||
value = offset ? value.slice(offset) : value;
|
||||
|
||||
// Trim & unfold folded lines
|
||||
value = trim(value).replace(WS_FOLD_PATTERN, '');
|
||||
|
||||
var state = STATE.IDLE;
|
||||
var length = value.length;
|
||||
var offset = 0;
|
||||
var ref = null;
|
||||
|
||||
while (offset < length) {
|
||||
if (state === STATE.IDLE) {
|
||||
if (hasWhitespace(value[offset])) {
|
||||
offset++;
|
||||
continue;
|
||||
} else if (value[offset] === '<') {
|
||||
if (ref != null) {
|
||||
ref.rel != null
|
||||
? this.refs.push(...Link.expandRelations(ref))
|
||||
: this.refs.push(ref);
|
||||
}
|
||||
var end = value.indexOf('>', offset);
|
||||
if (end === -1)
|
||||
throw new Error(
|
||||
'Expected end of URI delimiter at offset ' + offset
|
||||
);
|
||||
ref = { uri: value.slice(offset + 1, end) };
|
||||
// this.refs.push( ref )
|
||||
offset = end;
|
||||
state = STATE.URI;
|
||||
} else {
|
||||
throw new Error(
|
||||
'Unexpected character "' + value[offset] + '" at offset ' + offset
|
||||
);
|
||||
}
|
||||
offset++;
|
||||
} else if (state === STATE.URI) {
|
||||
if (hasWhitespace(value[offset])) {
|
||||
offset++;
|
||||
continue;
|
||||
} else if (value[offset] === ';') {
|
||||
state = STATE.ATTR;
|
||||
offset++;
|
||||
} else if (value[offset] === ',') {
|
||||
state = STATE.IDLE;
|
||||
offset++;
|
||||
} else {
|
||||
throw new Error(
|
||||
'Unexpected character "' + value[offset] + '" at offset ' + offset
|
||||
);
|
||||
}
|
||||
} else if (state === STATE.ATTR) {
|
||||
if (value[offset] === ';' || hasWhitespace(value[offset])) {
|
||||
offset++;
|
||||
continue;
|
||||
}
|
||||
var end = value.indexOf('=', offset);
|
||||
if (end === -1)
|
||||
throw new Error('Expected attribute delimiter at offset ' + offset);
|
||||
var attr = trim(value.slice(offset, end)).toLowerCase();
|
||||
var attrValue = '';
|
||||
offset = end + 1;
|
||||
offset = skipWhitespace(value, offset);
|
||||
if (value[offset] === '"') {
|
||||
offset++;
|
||||
while (offset < length) {
|
||||
if (value[offset] === '"') {
|
||||
offset++;
|
||||
break;
|
||||
}
|
||||
if (value[offset] === '\\') {
|
||||
offset++;
|
||||
}
|
||||
attrValue += value[offset];
|
||||
offset++;
|
||||
}
|
||||
} else {
|
||||
var end = offset + 1;
|
||||
while (!DELIMITER_PATTERN.test(value[end]) && end < length) {
|
||||
end++;
|
||||
}
|
||||
attrValue = value.slice(offset, end);
|
||||
offset = end;
|
||||
}
|
||||
if (ref[attr] && Link.isSingleOccurenceAttr(attr)) {
|
||||
// Ignore multiples of attributes which may only appear once
|
||||
} else if (attr[attr.length - 1] === '*') {
|
||||
ref[attr] = Link.parseExtendedValue(attrValue);
|
||||
} else {
|
||||
attrValue = attr === 'type' ? attrValue.toLowerCase() : attrValue;
|
||||
if (ref[attr] != null) {
|
||||
if (Array.isArray(ref[attr])) {
|
||||
ref[attr].push(attrValue);
|
||||
} else {
|
||||
ref[attr] = [ref[attr], attrValue];
|
||||
}
|
||||
} else {
|
||||
ref[attr] = attrValue;
|
||||
}
|
||||
}
|
||||
switch (value[offset]) {
|
||||
case ',':
|
||||
state = STATE.IDLE;
|
||||
break;
|
||||
case ';':
|
||||
state = STATE.ATTR;
|
||||
break;
|
||||
}
|
||||
offset++;
|
||||
} else {
|
||||
throw new Error('Unknown parser state "' + state + '"');
|
||||
}
|
||||
}
|
||||
|
||||
if (ref != null) {
|
||||
ref.rel != null
|
||||
? this.refs.push(...Link.expandRelations(ref))
|
||||
: this.refs.push(ref);
|
||||
}
|
||||
|
||||
ref = null;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
toString() {
|
||||
var refs = [];
|
||||
var link = '';
|
||||
var ref = null;
|
||||
|
||||
for (var i = 0; i < this.refs.length; i++) {
|
||||
ref = this.refs[i];
|
||||
link = Object.keys(this.refs[i]).reduce(function (link, attr) {
|
||||
if (attr === 'uri') return link;
|
||||
return link + '; ' + Link.formatAttribute(attr, ref[attr]);
|
||||
}, '<' + ref.uri + '>');
|
||||
refs.push(link);
|
||||
}
|
||||
|
||||
return refs.join(', ');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether an encoding can be
|
||||
* natively handled with a `Buffer`
|
||||
* @param {String} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
Link.isCompatibleEncoding = function (value) {
|
||||
return COMPATIBLE_ENCODING_PATTERN.test(value);
|
||||
};
|
||||
|
||||
Link.parse = function (value, offset) {
|
||||
return new Link().parse(value, offset);
|
||||
};
|
||||
|
||||
Link.isSingleOccurenceAttr = function (attr) {
|
||||
return (
|
||||
attr === 'rel' ||
|
||||
attr === 'type' ||
|
||||
attr === 'media' ||
|
||||
attr === 'title' ||
|
||||
attr === 'title*'
|
||||
);
|
||||
};
|
||||
|
||||
Link.isTokenAttr = function (attr) {
|
||||
return attr === 'rel' || attr === 'type' || attr === 'anchor';
|
||||
};
|
||||
|
||||
Link.escapeQuotes = function (value) {
|
||||
return value.replace(/"/g, '\\"');
|
||||
};
|
||||
|
||||
Link.expandRelations = function (ref) {
|
||||
var rels = ref.rel.split(' ');
|
||||
return rels.map(function (rel) {
|
||||
var value = Object.assign({}, ref);
|
||||
value.rel = rel;
|
||||
return value;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Parses an extended value and attempts to decode it
|
||||
* @internal
|
||||
* @param {String} value
|
||||
* @return {Object}
|
||||
*/
|
||||
Link.parseExtendedValue = function (value) {
|
||||
var parts = /([^']+)?(?:'([^']+)')?(.+)/.exec(value);
|
||||
return {
|
||||
language: parts[2].toLowerCase(),
|
||||
encoding: Link.isCompatibleEncoding(parts[1])
|
||||
? null
|
||||
: parts[1].toLowerCase(),
|
||||
value: Link.isCompatibleEncoding(parts[1])
|
||||
? decodeURIComponent(parts[3])
|
||||
: parts[3],
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a given extended attribute and it's value
|
||||
* @param {String} attr
|
||||
* @param {Object} data
|
||||
* @return {String}
|
||||
*/
|
||||
Link.formatExtendedAttribute = function (attr, data) {
|
||||
var encoding = (data.encoding || 'utf-8').toUpperCase();
|
||||
var language = data.language || 'en';
|
||||
|
||||
var encodedValue = '';
|
||||
|
||||
if (Buffer.isBuffer(data.value) && Link.isCompatibleEncoding(encoding)) {
|
||||
encodedValue = data.value.toString(encoding);
|
||||
} else if (Buffer.isBuffer(data.value)) {
|
||||
encodedValue = data.value.toString('hex').replace(/[0-9a-f]{2}/gi, '%$1');
|
||||
} else {
|
||||
encodedValue = encodeURIComponent(data.value);
|
||||
}
|
||||
|
||||
return attr + '=' + encoding + "'" + language + "'" + encodedValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a given attribute and it's value
|
||||
* @param {String} attr
|
||||
* @param {String|Object} value
|
||||
* @return {String}
|
||||
*/
|
||||
Link.formatAttribute = function (attr, value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((item) => {
|
||||
return Link.formatAttribute(attr, item);
|
||||
})
|
||||
.join('; ');
|
||||
}
|
||||
|
||||
if (attr[attr.length - 1] === '*' || typeof value !== 'string') {
|
||||
return Link.formatExtendedAttribute(attr, value);
|
||||
}
|
||||
|
||||
if (Link.isTokenAttr(attr)) {
|
||||
value = needsQuotes(value)
|
||||
? '"' + Link.escapeQuotes(value) + '"'
|
||||
: Link.escapeQuotes(value);
|
||||
} else if (needsQuotes(value)) {
|
||||
value = encodeURIComponent(value);
|
||||
// We don't need to escape <SP> <,> <;> within quotes
|
||||
value = value
|
||||
.replace(/%20/g, ' ')
|
||||
.replace(/%2C/g, ',')
|
||||
.replace(/%3B/g, ';');
|
||||
|
||||
value = '"' + value + '"';
|
||||
}
|
||||
|
||||
return attr + '=' + value;
|
||||
};
|
||||
|
||||
module.exports = Link;
|
@@ -1,79 +0,0 @@
|
||||
import test from 'ava';
|
||||
import * as sinon from 'sinon';
|
||||
import fs from 'fs';
|
||||
|
||||
import * as core from '@actions/core';
|
||||
sinon.replace(core, 'getInput', sinon.fake());
|
||||
|
||||
import GithubApi from '../src/api';
|
||||
const GithubApiFake = sinon.fake((rul) => ({
|
||||
body: [],
|
||||
headers: {
|
||||
link:
|
||||
'<https://api.github.com/user/5617452/starred?page=2>; rel="next", <https://api.github.com/user/5617452/starred?page=2>; rel="last"',
|
||||
},
|
||||
}));
|
||||
sinon.replace(GithubApi, 'get', GithubApiFake);
|
||||
|
||||
import Git from '../src/git';
|
||||
const pull = sinon.fake();
|
||||
sinon.replace(Git, 'pull', pull);
|
||||
const add = sinon.fake();
|
||||
sinon.replace(Git, 'add', add);
|
||||
const commit = sinon.fake();
|
||||
sinon.replace(Git, 'commit', commit);
|
||||
const push = sinon.fake();
|
||||
sinon.replace(Git, 'push', push);
|
||||
sinon.replace(Git, 'config', sinon.fake());
|
||||
sinon.replace(Git, 'updateOrigin', sinon.fake());
|
||||
|
||||
const fsp = fs.promises;
|
||||
const writeFile = sinon.fake();
|
||||
sinon.replace(fsp, 'writeFile', writeFile);
|
||||
|
||||
import {
|
||||
wait,
|
||||
renderer,
|
||||
apiGetStar,
|
||||
generateMd,
|
||||
pushNewFiles,
|
||||
} from '../src/helpers';
|
||||
|
||||
test('wait should wait', async (t) => {
|
||||
await wait(200);
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test('renderer should render', async (t) => {
|
||||
const output = await renderer({ variable: 123 }, 'Test: <%= variable %>');
|
||||
t.is(output, 'Test: 123');
|
||||
});
|
||||
|
||||
test('apiGetStar', async (t) => {
|
||||
let stars = await apiGetStar('url');
|
||||
t.true(GithubApiFake.called);
|
||||
t.true(Array.isArray(stars));
|
||||
});
|
||||
|
||||
test('generateMd should create TOC', async (t) => {
|
||||
const tpl = `# title
|
||||
|
||||
## Table of Contents
|
||||
|
||||
## Javascript
|
||||
`;
|
||||
const result = await generateMd(tpl);
|
||||
t.is(
|
||||
result,
|
||||
`# title\n\n## Table of Contents\n\n* [Javascript](#javascript)\n\n## Javascript\n`
|
||||
);
|
||||
});
|
||||
|
||||
test('should push', async (t) => {
|
||||
await pushNewFiles([{ filename: 'README.md', data: '# title' }]);
|
||||
t.true(writeFile.called);
|
||||
t.true(pull.called);
|
||||
t.true(add.called);
|
||||
t.true(commit.called);
|
||||
t.true(push.called);
|
||||
});
|
Reference in New Issue
Block a user