@@ -113,6 +113,64 @@ const generateTypeScriptFile = (name: string, contents: string[]) => {
113113 ) ;
114114} ;
115115
116+ const asCode = ( str : string ) : string => `\`${ str } \`` ;
117+ const asLink = ( event : string ) : string => {
118+ const link = `https://developer.github.com/v3/activity/events/types/#${ event . replace (
119+ / [ ^ a - z ] / g,
120+ ""
121+ ) } event`;
122+
123+ return `[${ asCode ( event ) } ](${ link } )` ;
124+ } ;
125+
126+ const updateReadme = ( properties : string [ ] ) => {
127+ const headers = "| Event | Actions |" ;
128+
129+ const events = properties
130+ . filter ( ( property ) => property !== "*" && property !== "error" )
131+ . reduce < Record < string , string [ ] > > ( ( events , property ) => {
132+ console . log ( property ) ;
133+ const [ event , action ] = property . split ( "." ) ;
134+
135+ events [ event ] ||= [ ] ;
136+
137+ if ( action ) {
138+ events [ event ] . push ( action ) ;
139+ }
140+
141+ return events ;
142+ } , { } ) ;
143+
144+ const rows = Object . entries ( events ) . map (
145+ ( [ event , actions ] ) =>
146+ `| ${ asLink ( event ) } | ${ actions . map ( asCode ) . join ( "<br>" ) } |`
147+ ) ;
148+
149+ const table = format ( [ headers , "| --- | --- |" , ...rows ] . join ( "\n" ) , {
150+ parser : "markdown" ,
151+ } ) ;
152+
153+ const readme = fs . readFileSync ( "README.md" , "utf-8" ) ;
154+
155+ const TableStartString =
156+ "<!-- autogenerated via scripts/generate-types.ts -->" ;
157+ const TableEndString =
158+ "<!-- /autogenerated via scripts/generate-types.ts -->" ;
159+ const tableStartIndex = readme . indexOf ( TableStartString ) ;
160+ const tableEndIndex = readme . indexOf ( TableEndString ) ;
161+
162+ assert . ok ( tableStartIndex !== - 1 , "cannot find start of table" ) ;
163+ assert . ok ( tableEndIndex !== - 1 , "cannot find end of table" ) ;
164+
165+ fs . writeFileSync (
166+ "README.md" ,
167+ `${ readme . slice (
168+ 0 ,
169+ tableStartIndex + TableStartString . length
170+ ) } \n\n${ table } \n${ readme . slice ( tableEndIndex ) } `
171+ ) ;
172+ } ;
173+
116174const run = ( ) => {
117175 const [ imports , properties ] = getImportsAndProperties ( ) ;
118176
@@ -140,6 +198,8 @@ const run = () => {
140198 ...properties . map ( ( [ key ] ) => `"${ key } ",` ) ,
141199 "];" ,
142200 ] ) ;
201+
202+ updateReadme ( properties . map ( ( [ key ] ) => key ) ) ;
143203} ;
144204
145205run ( ) ;
0 commit comments