forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request anuraghazra#196 from anuraghazra/auto-theme-readme
infra: added auto theme readme generator script
- Loading branch information
Showing
5 changed files
with
215 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Generate Theme Readme | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
paths: | ||
- "themes/index.js" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: setup node | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: "12.x" | ||
|
||
- name: npm install, generate readme | ||
run: | | ||
npm ci | ||
npm run theme-readme-gen | ||
env: | ||
CI: true | ||
|
||
- name: Run Script | ||
uses: skx/github-action-tester@master | ||
with: | ||
script: ./push-theme-readme.sh | ||
env: | ||
CI: true | ||
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }} | ||
GH_REPO: ${{ secrets.GH_REPO }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
const theme = require("../themes/index"); | ||
const fs = require("fs"); | ||
|
||
const TARGET_FILE = "./themes/README.md"; | ||
const REPO_CARD_LINKS_FLAG = "<!-- REPO_CARD_LINKS -->"; | ||
const STAT_CARD_LINKS_FLAG = "<!-- STATS_CARD_LINKS -->"; | ||
|
||
const STAT_CARD_TABLE_FLAG = "<!-- STATS_CARD_TABLE -->"; | ||
const REPO_CARD_TABLE_FLAG = "<!-- REPO_CARD_TABLE -->"; | ||
|
||
const THEME_TEMPLATE = `## Available Themes | ||
<!-- DO NOT EDIT THIS FILE DIRECTLY --> | ||
With inbuilt themes you can customize the look of the card without doing any manual customization. | ||
Use \`?theme=THEME_NAME\` parameter like so :- | ||
\`\`\`md | ||
![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=dark&show_icons=true) | ||
\`\`\` | ||
## Stats | ||
> These themes work both for the Stats Card and Repo Card. | ||
| | | | | ||
| :--: | :--: | :--: | | ||
${STAT_CARD_TABLE_FLAG} | ||
## Repo Card | ||
> These themes work both for the Stats Card and Repo Card. | ||
| | | | | ||
| :--: | :--: | :--: | | ||
${REPO_CARD_TABLE_FLAG} | ||
${REPO_CARD_LINKS_FLAG} | ||
${STAT_CARD_LINKS_FLAG} | ||
[add-theme]: https://github.com/anuraghazra/github-readme-stats/edit/master/themes/index.js | ||
Wanted to add a new theme? Consider reading the [contribution guidelines](../CONTRIBUTING.md#themes-contribution) :D | ||
`; | ||
|
||
const createRepoMdLink = (theme) => { | ||
return `\n[${theme}]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=${theme}`; | ||
}; | ||
const createStatMdLink = (theme) => { | ||
return `\n[${theme}]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=${theme}`; | ||
}; | ||
|
||
const generateLinks = (fn) => { | ||
return Object.keys(theme) | ||
.map((name) => fn(name)) | ||
.join(""); | ||
}; | ||
|
||
const createTableItem = ({ link, label }) => { | ||
return `\`${label}\` ![${link}][${link}]`; | ||
}; | ||
const generateTable = ({ isRepoCard }) => { | ||
const rows = []; | ||
const themes = Object.keys(theme).filter( | ||
(name) => name !== (!isRepoCard ? "default_repocard" : "default") | ||
); | ||
|
||
for (let i = 0; i < themes.length; i += 3) { | ||
const one = themes[i]; | ||
const two = themes[i + 1]; | ||
const three = themes[i + 2]; | ||
|
||
let tableItem1 = createTableItem({ link: one, label: one }); | ||
let tableItem2 = createTableItem({ link: two, label: two }); | ||
let tableItem3 = createTableItem({ link: one, label: one }); | ||
|
||
if (three === undefined) { | ||
tableItem3 = `[Add your theme][add-theme]`; | ||
} | ||
rows.push(`| ${tableItem1} | ${tableItem2} | ${tableItem3} |`); | ||
} | ||
|
||
return rows.join("\n"); | ||
}; | ||
|
||
const buildReadme = () => { | ||
return THEME_TEMPLATE.split("\n") | ||
.map((line) => { | ||
if (line.includes(REPO_CARD_LINKS_FLAG)) { | ||
return generateLinks(createRepoMdLink); | ||
} | ||
if (line.includes(STAT_CARD_LINKS_FLAG)) { | ||
return generateLinks(createStatMdLink); | ||
} | ||
if (line.includes(REPO_CARD_TABLE_FLAG)) { | ||
return generateTable({ isRepoCard: true }); | ||
} | ||
if (line.includes(STAT_CARD_TABLE_FLAG)) { | ||
return generateTable({ isRepoCard: false }); | ||
} | ||
return line; | ||
}) | ||
.join("\n"); | ||
}; | ||
|
||
fs.writeFileSync(TARGET_FILE, buildReadme()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
set -x | ||
set -e | ||
|
||
export BRANCH_NAME=updated-theme-readme | ||
git --version | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "Github Readme Stats Bot" | ||
git branch -d $BRANCH_NAME || true | ||
git checkout -b $BRANCH_NAME | ||
git add --all | ||
git commit --message "docs(theme): Auto update theme readme" || exit 0 | ||
git remote add origin-$BRANCH_NAME https://${PERSONAL_TOKEN}@github.com/${GH_REPO}.git | ||
git push --force --quiet --set-upstream origin-$BRANCH_NAME $BRANCH_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters