Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

update-readme.js 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const walk = require('walk-sync')
  5. const dedent = require('dedent')
  6. const { difference } = require('lodash')
  7. const readme = path.join(__dirname, 'README.md')
  8. // [start-readme]
  9. //
  10. // This script crawls the script directory, hooks on special comment markers
  11. // in each script, and adds the comment to `script/README.md`.
  12. //
  13. // [end-readme]
  14. const startComment = 'start-readme'
  15. const endComment = 'end-readme'
  16. const startCommentRegex = new RegExp(startComment)
  17. const endCommentRegex = new RegExp(endComment)
  18. const ignoreList = [
  19. 'README.md'
  20. ]
  21. const scriptsToRuleThemAll = [
  22. 'bootstrap',
  23. 'server',
  24. 'test'
  25. ]
  26. const allScripts = walk(__dirname, { directories: false })
  27. .filter(script => ignoreList.every(ignoredPath => !script.includes(ignoredPath)))
  28. const otherScripts = difference(allScripts, scriptsToRuleThemAll)
  29. // build an object with script name as key and readme comment as value
  30. const allComments = {}
  31. allScripts.forEach(script => {
  32. const fullPath = path.join(__dirname, script)
  33. let addToReadme = false
  34. const readmeComment = fs.readFileSync(fullPath, 'utf8')
  35. .split('\n')
  36. .filter(cmt => {
  37. if (startCommentRegex.test(cmt)) addToReadme = true
  38. if (endCommentRegex.test(cmt)) addToReadme = false
  39. if (addToReadme && !cmt.includes(startComment) && !cmt.includes(endComment)) return cmt
  40. return false
  41. })
  42. // remove comment markers and clean up newlines
  43. .map(cmt => cmt.replace(/^(\/\/|#) ?/m, ''))
  44. .join('\n')
  45. .trim()
  46. allComments[script] = readmeComment
  47. // preserve double newlines as multiline list items
  48. .replace(/\n\n/g, '\n\n\n ')
  49. // remove single newlines
  50. .replace(/\n(?!\n)/g, ' ')
  51. })
  52. // turn the script names/comments into itemized lists in the README
  53. const template = `# Scripts
  54. ## Scripts to rule them all
  55. This directory follows the [Scripts to Rule Them All](https://githubengineering.com/scripts-to-rule-them-all/) pattern:
  56. ${createTemplate(scriptsToRuleThemAll)}
  57. ## Additional scripts
  58. ${createTemplate(otherScripts)}
  59. `
  60. // update the readme
  61. if (template === fs.readFileSync(readme, 'utf8')) {
  62. console.log('The README is up-to-date!')
  63. } else {
  64. fs.writeFileSync(readme, template)
  65. console.log('The README.md has been updated!')
  66. }
  67. function createTemplate (arrayOfScripts) {
  68. return arrayOfScripts.map(script => {
  69. const comment = allComments[script]
  70. return dedent`### [\`${script}\`](${script})\n\n${comment}\n\n---\n\n`
  71. }).join('\n')
  72. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...