Skip to content

Commit

Permalink
feat: manually generate all-contributors table in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jan 15, 2025
1 parent 444de1c commit 7f74f52
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 58 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"input-from-script": "0.1.0-alpha.4",
"js-yaml": "^4.1.0",
"lazy-value": "^3.0.0",
"lodash": "^4.17.21",
"npm-user": "^6.1.1",
"object-strings-deep": "^0.1.1",
"octokit": "^4.0.2",
Expand All @@ -82,6 +83,7 @@
"@types/git-url-parse": "9.0.3",
"@types/html-to-text": "9.0.4",
"@types/js-yaml": "4.0.9",
"@types/lodash": "^4.17.14",
"@types/node": "22.10.2",
"@types/parse-author": "2.0.3",
"@vitest/coverage-v8": "2.1.8",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions src/next/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import { readDescription } from "./readDescription.js";
import { readDocumentation } from "./readDocumentation.js";
import { swallowError } from "./utils/swallowError.js";

const zContributor = z.object({
avatar_url: z.string(),
contributions: z.array(z.string()),
login: z.string(),
name: z.string(),
profile: z.string(),
});

export type Contributor = z.infer<typeof zContributor>;

export const base = createBase({
options: {
access: z
Expand All @@ -36,15 +46,7 @@ export const base = createBase({
.optional()
.describe('value to set in `package.json`\'s `"bin"` property'),
contributors: z
.array(
z.object({
avatar_url: z.string(),
contributions: z.array(z.string()),
login: z.string(),
name: z.string(),
profile: z.string(),
}),
)
.array(zContributor)
.optional()
.describe("AllContributors contributors to store in .all-contributorsrc"),
description: z
Expand Down
34 changes: 31 additions & 3 deletions src/next/blocks/blockAllContributors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ describe("blockAllContributors", () => {
expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": undefined,
},
"block": [Function],
},
{
"addons": {
"secrets": [
Expand Down Expand Up @@ -52,7 +58,6 @@ describe("blockAllContributors", () => {
"scripts": [
{
"commands": [
"pnpx all-contributors-cli generate",
"pnpx all-contributors-cli add test-owner code,content,doc,ideas,infra,maintenance,projectManagement,tool",
],
"phase": 3,
Expand All @@ -62,7 +67,7 @@ describe("blockAllContributors", () => {
`);
});

it("includes contributors when not provided", () => {
it("includes contributors when provided", () => {
const creation = testBlock(blockAllContributors, {
options: {
...optionsBase,
Expand All @@ -81,6 +86,30 @@ describe("blockAllContributors", () => {
expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": [
"## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="http://www.joshuakgoldberg.com"><img src="https://avatars.githubusercontent.com/u/3335181?v=4?s=100" width="100px;" alt="Josh Goldberg"/><br /><sub><b>Josh Goldberg</b></sub></a><br /><a href="https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=author%3AJoshuaKGoldberg" title="Bug reports">🐛</a> <a href="#ideas-JoshuaKGoldberg" title="Ideas, Planning, & Feedback">🤔</a></td>
</tr>
</tbody>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->",
],
},
"block": [Function],
},
{
"addons": {
"secrets": [
Expand Down Expand Up @@ -122,7 +151,6 @@ describe("blockAllContributors", () => {
"scripts": [
{
"commands": [
"pnpx all-contributors-cli generate",
"pnpx all-contributors-cli add test-owner code,content,doc,ideas,infra,maintenance,projectManagement,tool",
],
"phase": 3,
Expand Down
76 changes: 74 additions & 2 deletions src/next/blocks/blockAllContributors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { sortBy } from "lodash";

import { createSoloWorkflowFile } from "../../steps/writing/creation/dotGitHub/createSoloWorkflowFile.js";
import { base } from "../base.js";
import { base, Contributor } from "../base.js";
import { blockREADME } from "./blockREADME.js";
import { blockRepositorySecrets } from "./blockRepositorySecrets.js";
import { CommandPhase } from "./phases.js";

Expand All @@ -10,6 +13,11 @@ export const blockAllContributors = base.createBlock({
produce({ options }) {
return {
addons: [
blockREADME({
sections: options.contributors
? [printAllContributorsTable(options.contributors)]
: undefined,
}),
blockRepositorySecrets({
secrets: [
{
Expand Down Expand Up @@ -52,7 +60,6 @@ export const blockAllContributors = base.createBlock({
scripts: [
{
commands: [
`pnpx all-contributors-cli generate`,
`pnpx all-contributors-cli add ${options.owner} code,content,doc,ideas,infra,maintenance,projectManagement,tool`,
],
phase: CommandPhase.Process,
Expand All @@ -61,3 +68,68 @@ export const blockAllContributors = base.createBlock({
};
},
});

function printAllContributorsTable(contributors: Contributor[]) {
return [
`## Contributors`,
``,
`<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->`,
`<!-- prettier-ignore-start -->`,
`<!-- markdownlint-disable -->`,
`<table>`,
` <tbody>`,
` <tr>`,
// This intentionally uses the same sort as all-contributors-cli:
// https://github.com/all-contributors/cli/blob/74bc388bd6f0ae2658e6495e9d3781d737438a97/src/generate/index.js#L76
...sortBy(contributors, "name").flatMap((contributor, i) => {
const row = printContributorCell(contributor);

return i && i % 7 === 0 ? [` </tr>`, ` <tr>`, row] : [row];
}),
` </tr>`,
` </tbody>`,
`</table>`,
``,
`<!-- markdownlint-restore -->`,
`<!-- prettier-ignore-end -->`,
``,
`<!-- ALL-CONTRIBUTORS-LIST:END -->`,
].join("\n");
}

function printContributorCell(contributor: Contributor) {
return [
` <td align="center" valign="top" width="14.28%">`,
`<a href="${contributor.profile}">`,
`<img src="${contributor.avatar_url}?s=100" width="100px;" alt="${contributor.name}"/>`,
`<br />`,
`<sub><b>${contributor.name}</b></sub></a><br />`,
...contributor.contributions
.map((contribution) => {
switch (contribution) {
case "bug":
return `<a href="https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=author%3A${contributor.login}" title="Bug reports">🐛</a>`;
case "code":
return `<a href="https://github.com/JoshuaKGoldberg/create-typescript-app/commits?author=${contributor.login}" title="Code">💻</a>`;
case "design":
return `<a href="#design-${contributor.login}" title="Design">🎨</a>`;
case "doc":
return `<a href="https://github.com/JoshuaKGoldberg/create-typescript-app/commits?author=${contributor.login}" title="Documentation">📖</a>`;
case "ideas":
return `<a href="#ideas-${contributor.login}" title="Ideas, Planning, & Feedback">🤔</a>`;
case "infra":
return `<a href="#infra-${contributor.login}" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a>`;
case "maintenance":
return `<a href="#maintenance-${contributor.login}" title="Maintenance">🚧</a>`;
case "review":
return `<a href="https://github.com/JoshuaKGoldberg/create-typescript-app/pulls?q=is%3Apr+reviewed-by%3A${contributor.login}" title="Reviewed Pull Requests">👀</a>`;
case "test":
return `<a href="https://github.com/JoshuaKGoldberg/create-typescript-app/commits?author=${contributor.login}" title="Tests">⚠️</a>`;
case "tool":
return `<a href="#tool-${contributor.login}" title="Tools">🔧</a>`;
}
})
.join(" "),
`</td>`,
].join("");
}
36 changes: 0 additions & 36 deletions src/next/blocks/blockREADME.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
",
},
}
Expand Down Expand Up @@ -97,12 +91,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
",
},
}
Expand Down Expand Up @@ -150,12 +138,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
",
},
}
Expand Down Expand Up @@ -205,12 +187,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
",
},
}
Expand Down Expand Up @@ -250,12 +226,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
",
},
}
Expand Down Expand Up @@ -298,12 +268,6 @@ describe("blockREADME", () => {
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
> Hello, world! 💖",
},
Expand Down
11 changes: 3 additions & 8 deletions src/next/blocks/blockREADME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const blockREADME = base.createBlock({
},
addons: {
notices: z.array(z.string()).default([]),
sections: z.array(z.string()).default([]),
},
produce({ addons, options }) {
const { notices } = addons;
const { notices, sections } = addons;

const logo = options.logo
? `\n<img ${printAttributes({ align: "right", ...options.logo })}>\n`
Expand Down Expand Up @@ -48,13 +49,7 @@ ${options.usage}
See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
Thanks! 💖
## Contributors
<!-- spellchecker: disable -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- spellchecker: enable -->
${sections.map((section) => `\n${section}\n`).join("")}
${notices.length ? `\n${notices.map((notice) => notice.trim()).join("\n\n")}` : ""}`,
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/steps/writing/creation/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ describe("createStructure", () => {
"input-from-script",
"js-yaml",
"lazy-value",
"lodash",
"npm-user",
"object-strings-deep",
"octokit",
Expand All @@ -242,6 +243,7 @@ describe("createStructure", () => {
"@release-it/conventional-changelog",
"@types/git-url-parse",
"@types/html-to-text",
"@types/lodash",
"@types/js-yaml",
"@types/parse-author",
"all-contributors-cli",
Expand Down

0 comments on commit 7f74f52

Please sign in to comment.