Skip to content

Commit

Permalink
add CI and script to reorganize README
Browse files Browse the repository at this point in the history
  • Loading branch information
takase1121 committed Dec 22, 2021
1 parent 0570164 commit 7bc11a2
Show file tree
Hide file tree
Showing 5 changed files with 2,046 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Reorganize README

on:
push:
branches: [master]

jobs:
reorganize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
- name: run the script
run: |
cd scripts
npm install
npm run main -- "https://github.com/${{ github.repository }}/blob/${GITHUB_REF##*/}/" ../README.md ../README.md
- name: create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Reorganize README.md
branch: reorganize-ci
add-paths: README.md
delete-branch: false
title: '[CI] Reorganize README.md'
body: |
This is a CI that makes sure entries are in alphabetical order and no links are invalid.
Please check the [CI run][1] for logs.
[1]: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
74 changes: 74 additions & 0 deletions scripts/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { readFile, writeFile } from 'fs/promises'

import centra from 'centra'
import pLimit from 'p-limit'
import { remark } from 'remark'
import remarkGfm from 'remark-gfm'
import { select, selectAll } from 'unist-util-select'

const baseurl = process.argv[2]
const inputFile = process.argv[3]
const outputFile = process.argv[4]

const selector = 'tableCell:first-child inlineCode'
const sortEntries = async tree => {
const rows = select('table', tree)
const header = rows.children.shift()

rows.children.sort((first, second) =>
select(selector, first).value
.localeCompare(select(selector, second).value))

rows.children.unshift(header)
return tree
}

const makeUrl = link => link.url.startsWith('http') ? link.url : baseurl + link.url

const get404s = async tree => {
const rows = select('table', tree)
const links = selectAll('tableCell:first-child link', rows)
.map(makeUrl)


const limit = pLimit(5)
const reqs = links
.map(url =>
limit(() => centra(url, 'HEAD')
.send()
.then(res => [url, res.statusCode])
.catch(() => [url, undefined])))

const status = await Promise.all(reqs)
const good = new Set(status.filter(([_, s]) => typeof s === 'number' && s < 400).map(([url]) => url))

if (good.size !== status.length) {
console.log(`Found ${status.length - good.size} broken URLs`)
status
.filter(([url]) => !good.has(url))
.forEach(([url, status]) => console.log(`${url} : ${typeof status === 'number' ? status : 'failed'}`))
}

// filter the table
rows.children = rows.children
.filter(row => {
const link = select('tableCell:first-child link', row)
return link ? good.has(makeUrl(link)) : true
})


return tree
}

const main = async () => {
const file = await readFile(inputFile, { encoding: 'utf-8' })
const output = await remark()
.use(remarkGfm)
.use(() => get404s)
.use(() => sortEntries)
.process(file)

await writeFile(outputFile, String(output))
}

main()
Loading

0 comments on commit 7bc11a2

Please sign in to comment.