Skip to content

Commit 7ce9c52

Browse files
committed
build: fold update-known-events into generate-types script
1 parent aad0787 commit 7ce9c52

File tree

6 files changed

+117
-176
lines changed

6 files changed

+117
-176
lines changed

README.md

Lines changed: 57 additions & 51 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 0 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*' README.md package.json",
1818
"pretest": "npm run -s lint",
1919
"test": "jest --coverage",
20-
"update-known-events": "node scripts/update-known-events.js",
2120
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node test/typescript-validate.ts"
2221
},
2322
"prettier": {},
@@ -61,10 +60,8 @@
6160
"axios": "^0.21.0",
6261
"get-port": "^5.0.0",
6362
"jest": "^26.2.2",
64-
"pascal-case": "^3.0.0",
6563
"prettier": "^2.0.1",
6664
"semantic-release": "^17.0.0",
67-
"table-builder": "^2.1.1",
6865
"ts-jest": "^26.2.0",
6966
"ts-node": "^9.1.1",
7067
"typescript": "^4.0.2"

scripts/generate-file.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

scripts/generate-types.ts

100644100755
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
116174
const 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

145205
run();

scripts/update-known-events.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)