1- const { readFileSync, writeFileSync } = require ( "fs" ) ;
21const { join } = require ( "path" ) ;
32
3+ const { outputFileSync } = require ( "fs-extra" ) ;
44const prettier = require ( "prettier" ) ;
5+ const marked = require ( "marked" ) ;
56
67const ENDPOINTS = require ( "./generated/endpoints.json" ) ;
78const WORKAROUNDS = require ( "./workarounds" ) ;
@@ -13,41 +14,70 @@ const newRoutes = {};
1314generateRoutes ( ) ;
1415
1516async function generateRoutes ( ) {
16- const examples = ENDPOINTS . concat ( WORKAROUNDS )
17- . map ( endpoint => {
18- if ( endpoint . isLegacy && ! / ^ \/ t e a m s \/ \{ t e a m _ i d \} / . test ( endpoint . url ) ) {
19- // ignore legacy endpoints with the exception of the new teams legacy methods
20- return ;
21- }
22-
23- const paramNames = endpoint . parameters
24- . filter ( parameter => ! parameter . alias )
25- . filter ( parameter => ! parameter . name . includes ( "." ) )
26- . filter ( parameter => ! [ "per_page" , "page" ] . includes ( parameter . name ) )
27- . map ( parameter => parameter . name )
28- . join ( ", " ) ;
29-
30- const comment = endpoint . renamed
31- ? `// DEPRECATED: octokit.${ endpoint . renamed . before . scope } .${ endpoint . renamed . before . id } () has been renamed to octokit.${ endpoint . renamed . after . scope } .${ endpoint . renamed . after . id } ()`
32- : `// ${ endpoint . documentationUrl } ` ;
33-
34- return `${ comment }
35- octokit.${ endpoint . scope } .${ endpoint . id } (${
36- paramNames ? `{ ${ paramNames } }` : ""
37- } );`;
38- } )
39- . join ( "\n\n" ) ;
40-
41- const currentContent = readFileSync ( README_PATH , "utf8" ) ;
42- const newContent = currentContent . replace (
43- / c o n s t o c t o k i t = n e w M y O c t o k i t \( { a u t h : " s e c r e t 1 2 3 " } \) ; [ \s \S ] * \` / ,
44- `const octokit = new MyOctokit({ auth: "secret123" });
45-
46- ${ prettier . format ( examples , {
47- parser : "babel"
48- } ) } \`\`\``
17+ const endpoints = ENDPOINTS . concat ( WORKAROUNDS ) ;
18+
19+ for ( const endpoint of endpoints ) {
20+ const path = `docs/${ endpoint . scope } /${ endpoint . id } .md` ;
21+ outputFileSync ( path , template ( endpoint ) ) ;
22+ console . log ( `${ path } written` ) ;
23+ }
24+ }
25+
26+ function template ( endpoint ) {
27+ const deprecationNotice = endpoint . isDeprecated
28+ ? "**This method is deprecated.**"
29+ : "" ;
30+ const renameNotice = endpoint . renamed
31+ ? `**Deprecated:** This method has been renamed to ${ endpoint . renamed . after . scope } .${ endpoint . renamed . after . id } `
32+ : "" ;
33+
34+ const parameterRows = endpoint . parameters . map (
35+ param =>
36+ `<tr><td>${ param . name } </td><td>${ param . required ? "yes" : "no" } </td><td>
37+
38+ ${ param . description }
39+
40+ </td></tr>`
4941 ) ;
5042
51- writeFileSync ( README_PATH , newContent ) ;
52- console . log ( `${ README_PATH } updated.` ) ;
43+ const requiredParameterNames = endpoint . parameters
44+ . filter ( parameter => parameter . required )
45+ . map ( parameter => parameter . name ) ;
46+ const example = `octokit.${ endpoint . scope } .${
47+ endpoint . id
48+ } (${ requiredParameterNames . join ( ", " ) } )`;
49+
50+ const content = `
51+ # ${ endpoint . name }
52+
53+ ${ deprecationNotice }
54+
55+ ${ renameNotice }
56+
57+ ${ endpoint . description }
58+
59+ \`\`\`js
60+ ${ example }
61+ \`\`\`
62+
63+ ## Parameters
64+
65+ <table>
66+ <thead>
67+ <tr>
68+ <th>name</th>
69+ <th>required</th>
70+ <th>description</th>
71+ </tr>
72+ </thead>
73+ <tbody>
74+ ${ parameterRows . join ( "\n" ) }
75+ </tbody>
76+ </table>
77+
78+ See also: [GitHub Developer Guide documentation](endpoint.documentationUrl).` ;
79+
80+ return prettier . format ( content , {
81+ parser : "markdown"
82+ } ) ;
5383}
0 commit comments