2020-09-27 15:10:11 +03:00
|
|
|
#!/usr/bin/env node
|
2021-07-29 23:28:30 +03:00
|
|
|
|
|
|
|
// [start-readme]
|
|
|
|
//
|
|
|
|
// This script crawls the script directory, hooks on special comment markers
|
|
|
|
// in each script, and adds the comment to `script/README.md`.
|
|
|
|
//
|
|
|
|
// [end-readme]
|
|
|
|
|
2021-07-14 23:49:18 +03:00
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import path from 'path'
|
2021-07-29 19:45:46 +03:00
|
|
|
import fs from 'fs/promises'
|
2021-07-14 23:49:18 +03:00
|
|
|
import walk from 'walk-sync'
|
|
|
|
import dedent from 'dedent'
|
|
|
|
import { difference } from 'lodash-es'
|
2021-07-29 23:28:30 +03:00
|
|
|
|
2021-07-14 23:49:18 +03:00
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
const readme = path.join(__dirname, 'README.md')
|
|
|
|
|
|
|
|
const startComment = 'start-readme'
|
|
|
|
const endComment = 'end-readme'
|
|
|
|
const startCommentRegex = new RegExp(startComment)
|
|
|
|
const endCommentRegex = new RegExp(endComment)
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
const ignoreList = ['README.md']
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
const scriptsToRuleThemAll = ['bootstrap', 'server', 'test']
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
const allScripts = walk(__dirname, { directories: false }).filter((script) =>
|
|
|
|
ignoreList.every((ignoredPath) => !script.includes(ignoredPath))
|
|
|
|
)
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
const otherScripts = difference(allScripts, scriptsToRuleThemAll)
|
|
|
|
|
|
|
|
// build an object with script name as key and readme comment as value
|
|
|
|
const allComments = {}
|
2021-07-29 19:45:46 +03:00
|
|
|
for (const script of allScripts) {
|
2020-09-27 15:10:11 +03:00
|
|
|
const fullPath = path.join(__dirname, script)
|
|
|
|
|
|
|
|
let addToReadme = false
|
2021-07-29 19:45:46 +03:00
|
|
|
const readmeComment = (await fs.readFile(fullPath, 'utf8'))
|
2020-09-27 15:10:11 +03:00
|
|
|
.split('\n')
|
2021-07-15 00:35:01 +03:00
|
|
|
.filter((cmt) => {
|
2020-09-27 15:10:11 +03:00
|
|
|
if (startCommentRegex.test(cmt)) addToReadme = true
|
|
|
|
if (endCommentRegex.test(cmt)) addToReadme = false
|
|
|
|
if (addToReadme && !cmt.includes(startComment) && !cmt.includes(endComment)) return cmt
|
2020-11-18 00:18:18 +03:00
|
|
|
return false
|
2020-09-27 15:10:11 +03:00
|
|
|
})
|
|
|
|
// remove comment markers and clean up newlines
|
2021-07-15 00:35:01 +03:00
|
|
|
.map((cmt) => cmt.replace(/^(\/\/|#) ?/m, ''))
|
2020-09-27 15:10:11 +03:00
|
|
|
.join('\n')
|
|
|
|
.trim()
|
|
|
|
|
|
|
|
allComments[script] = readmeComment
|
|
|
|
// preserve double newlines as multiline list items
|
|
|
|
.replace(/\n\n/g, '\n\n\n ')
|
|
|
|
// remove single newlines
|
|
|
|
.replace(/\n(?!\n)/g, ' ')
|
2021-07-29 19:45:46 +03:00
|
|
|
}
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
// turn the script names/comments into itemized lists in the README
|
|
|
|
const template = `# Scripts
|
|
|
|
|
|
|
|
## Scripts to rule them all
|
|
|
|
|
|
|
|
This directory follows the [Scripts to Rule Them All](https://githubengineering.com/scripts-to-rule-them-all/) pattern:
|
|
|
|
|
|
|
|
${createTemplate(scriptsToRuleThemAll)}
|
|
|
|
|
|
|
|
## Additional scripts
|
|
|
|
|
|
|
|
${createTemplate(otherScripts)}
|
|
|
|
`
|
|
|
|
|
|
|
|
// update the readme
|
2021-07-29 19:45:46 +03:00
|
|
|
if (template === (await fs.readFile(readme, 'utf8'))) {
|
2020-09-27 15:10:11 +03:00
|
|
|
console.log('The README is up-to-date!')
|
|
|
|
} else {
|
2021-07-29 19:45:46 +03:00
|
|
|
await fs.writeFile(readme, template)
|
2020-09-27 15:10:11 +03:00
|
|
|
console.log('The README.md has been updated!')
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
function createTemplate(arrayOfScripts) {
|
|
|
|
return arrayOfScripts
|
|
|
|
.map((script) => {
|
|
|
|
const comment = allComments[script]
|
|
|
|
return dedent`### [\`${script}\`](${script})\n\n${comment}\n\n---\n\n`
|
|
|
|
})
|
|
|
|
.join('\n')
|
2020-09-27 15:10:11 +03:00
|
|
|
}
|