|
| 1 | +const { readFileSync, writeFileSync } = require("fs"); |
| 2 | +const { join } = require("path"); |
| 3 | + |
| 4 | +const prettier = require("prettier"); |
| 5 | + |
| 6 | +const ENDPOINTS = require("./generated/endpoints.json"); |
| 7 | +const WORKAROUNDS = require("./workarounds"); |
| 8 | + |
| 9 | +const README_PATH = join(__dirname, "..", "..", "README.md"); |
| 10 | + |
| 11 | +const newRoutes = {}; |
| 12 | + |
| 13 | +generateRoutes(); |
| 14 | + |
| 15 | +async function generateRoutes() { |
| 16 | + const examples = ENDPOINTS.concat(WORKAROUNDS) |
| 17 | + .map(endpoint => { |
| 18 | + const paramNames = endpoint.parameters |
| 19 | + .filter(parameter => !parameter.alias) |
| 20 | + .filter(parameter => !parameter.name.includes(".")) |
| 21 | + .filter(parameter => !["per_page", "page"].includes(parameter.name)) |
| 22 | + .map(parameter => parameter.name) |
| 23 | + .join(", "); |
| 24 | + |
| 25 | + const comment = endpoint.renamed |
| 26 | + ? `// DEPRECATED: octokit.${endpoint.renamed.before.scope}.${endpoint.renamed.before.id}() has been renamed to octokit.${endpoint.renamed.after.scope}.${endpoint.renamed.after.id}()` |
| 27 | + : `// ${endpoint.documentationUrl}`; |
| 28 | + |
| 29 | + return `${comment} |
| 30 | +octokit.${endpoint.scope}.${endpoint.id}(${ |
| 31 | + paramNames ? `{ ${paramNames} }` : "" |
| 32 | + });`; |
| 33 | + }) |
| 34 | + .join("\n\n"); |
| 35 | + |
| 36 | + // writeFileSync( |
| 37 | + // ROUTES_PATH, |
| 38 | + // prettier.format(JSON.stringify(sortKeys(newRoutes, { deep: true })), { |
| 39 | + // parser: "json" |
| 40 | + // }) |
| 41 | + // ); |
| 42 | + // console.log(`${ROUTES_PATH} written.`); |
| 43 | + |
| 44 | + const currentContent = readFileSync(README_PATH, "utf8"); |
| 45 | + const newContent = currentContent.replace( |
| 46 | + /const octokit = new MyOctokit\({ auth: "secret123" }\);[\s\S]*\`/, |
| 47 | + `const octokit = new MyOctokit({ auth: "secret123" }); |
| 48 | +
|
| 49 | +${prettier.format(examples, { |
| 50 | + parser: "babel" |
| 51 | +})}\`\`\`` |
| 52 | + ); |
| 53 | + |
| 54 | + writeFileSync(README_PATH, newContent); |
| 55 | + console.log(`${README_PATH} updated.`); |
| 56 | +} |
0 commit comments