@@ -5,6 +5,7 @@ import path from 'path';
5
5
import fs from 'fs' ;
6
6
import _ from 'lodash' ;
7
7
import glob from 'glob' ;
8
+ import Gitdown from 'gitdown' ;
8
9
9
10
const trimCode = ( code ) => {
10
11
let lines = code . replace ( / ^ \n / , '' ) . trimEnd ( ) . split ( '\n' ) ;
@@ -66,10 +67,24 @@ const getAssertions = () => {
66
67
return _ . zipObject ( assertionNames , assertionCodes ) ;
67
68
} ;
68
69
69
- const updateDocuments = ( assertions ) => {
70
- const readmeDocumentPath = path . join ( __dirname , '../../README.md' ) ;
70
+ const getSomeBranch = ( ) => {
71
+ const gitConfig = fs . readFileSync ( path . join ( __dirname , '../../.git/config' ) ) . toString ( ) ;
72
+ const [ , branch ] = / \[ b r a n c h " ( [ ^ " ] + ) " \] / . exec ( gitConfig ) || [ ] ;
71
73
72
- let documentBody = fs . readFileSync ( readmeDocumentPath , 'utf8' ) ;
74
+ return branch ;
75
+ } ;
76
+
77
+ const generateReadme = async ( ) => {
78
+ const assertions = getAssertions ( ) ;
79
+ const gitdown = Gitdown . readFile ( path . join ( __dirname , '../../.README/README.md' ) ) ;
80
+
81
+ gitdown . setConfig ( {
82
+ gitinfo : {
83
+ defaultBranchName : getSomeBranch ( ) || 'master' ,
84
+ gitPath : path . join ( __dirname , '../../.git' )
85
+ }
86
+ } ) ;
87
+ let documentBody = await gitdown . get ( ) ;
73
88
74
89
documentBody = documentBody . replace ( / < ! - - a s s e r t i o n s ( [ a - z ] + ?) - - > / ig, ( assertionsBlock ) => {
75
90
const ruleName = assertionsBlock . match ( / a s s e r t i o n s ( [ a - z ] + ) / i) [ 1 ] ;
@@ -83,7 +98,45 @@ const updateDocuments = (assertions) => {
83
98
'\n````\n\nThe following patterns are not considered problems:\n\n````js\n' + ruleAssertions . valid . join ( '\n\n' ) + '\n````\n' ;
84
99
} ) ;
85
100
86
- fs . writeFileSync ( readmeDocumentPath , documentBody ) ;
101
+ return documentBody ;
102
+ } ;
103
+
104
+ const generateReadmeAndWriteToDisk = async ( ) => {
105
+ const readme = await generateReadme ( ) ;
106
+ const dist = path . join ( __dirname , '..' , '..' , 'README.md' ) ;
107
+ fs . writeFileSync ( dist , readme ) ;
108
+ } ;
109
+
110
+ const assertReadmeIsUpToDate = async ( ) => {
111
+ const readme = await generateReadme ( ) ;
112
+ const readmePath = path . join ( __dirname , '..' , '..' , 'README.md' ) ;
113
+
114
+ const isUpToDate = fs . readFileSync ( readmePath ) . toString ( ) === readme ;
115
+
116
+ if ( ! isUpToDate ) {
117
+ throw new Error ( 'Readme is not up to date, please run `npm run create-readme` to update it.' ) ;
118
+ }
87
119
} ;
88
120
89
- updateDocuments ( getAssertions ( ) ) ;
121
+ const main = async ( ) => {
122
+ try {
123
+ const hasCheckFlag = process . argv . some ( ( arg ) => {
124
+ return arg === '--check' ;
125
+ } ) ;
126
+
127
+ if ( hasCheckFlag ) {
128
+ await assertReadmeIsUpToDate ( ) ;
129
+ } else {
130
+ await generateReadmeAndWriteToDisk ( ) ;
131
+ }
132
+ } catch ( error ) {
133
+ /* eslint-disable-next-line no-console */
134
+ console . error ( error ) ;
135
+ /* eslint-disable-next-line no-process-exit */
136
+ process . exit ( 1 ) ;
137
+ }
138
+ } ;
139
+
140
+ main ( ) ;
141
+
142
+ export default generateReadme ;
0 commit comments