1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/script/update-readme.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

93 lines
2.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import walk from 'walk-sync'
import dedent from 'dedent'
import { difference } from 'lodash-es'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const readme = path.join(__dirname, 'README.md')
// [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]
const startComment = 'start-readme'
const endComment = 'end-readme'
const startCommentRegex = new RegExp(startComment)
const endCommentRegex = new RegExp(endComment)
const ignoreList = [
'README.md'
]
const scriptsToRuleThemAll = [
'bootstrap',
'server',
'test'
]
const allScripts = walk(__dirname, { directories: false })
.filter(script => ignoreList.every(ignoredPath => !script.includes(ignoredPath)))
const otherScripts = difference(allScripts, scriptsToRuleThemAll)
// build an object with script name as key and readme comment as value
const allComments = {}
allScripts.forEach(script => {
const fullPath = path.join(__dirname, script)
let addToReadme = false
const readmeComment = fs.readFileSync(fullPath, 'utf8')
.split('\n')
.filter(cmt => {
if (startCommentRegex.test(cmt)) addToReadme = true
if (endCommentRegex.test(cmt)) addToReadme = false
if (addToReadme && !cmt.includes(startComment) && !cmt.includes(endComment)) return cmt
return false
})
// remove comment markers and clean up newlines
.map(cmt => cmt.replace(/^(\/\/|#) ?/m, ''))
.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, ' ')
})
// 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
if (template === fs.readFileSync(readme, 'utf8')) {
console.log('The README is up-to-date!')
} else {
fs.writeFileSync(readme, template)
console.log('The README.md has been updated!')
}
function createTemplate (arrayOfScripts) {
return arrayOfScripts.map(script => {
const comment = allComments[script]
return dedent`### [\`${script}\`](${script})\n\n${comment}\n\n---\n\n`
}).join('\n')
}