Skip to content

Commit 444de1c

Browse files
feat: extract codecov uploading to a blockCodecov (#1865)
## PR Checklist - [x] Addresses an existing open issue: fixes #1848 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Also removes `addons.flags` from `blockVitest` since this repo won't use them after #1839. 💖
1 parent 66db26e commit 444de1c

File tree

6 files changed

+195
-90
lines changed

6 files changed

+195
-90
lines changed

src/next/blocks/blockCodecov.test.ts

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { testBlock } from "create-testers";
2+
import { describe, expect, test } from "vitest";
3+
4+
import { blockCodecov } from "./blockCodecov.js";
5+
import { optionsBase } from "./options.fakes.js";
6+
7+
describe("blockCodecov", () => {
8+
test("without addons or mode", () => {
9+
const creation = testBlock(blockCodecov, {
10+
options: optionsBase,
11+
});
12+
13+
expect(creation).toMatchInlineSnapshot(`
14+
{
15+
"addons": [
16+
{
17+
"addons": {
18+
"apps": [
19+
{
20+
"name": "Codecov",
21+
"url": "https://github.com/apps/codecov",
22+
},
23+
],
24+
},
25+
"block": [Function],
26+
},
27+
{
28+
"addons": {
29+
"actionSteps": [
30+
{
31+
"if": "always()",
32+
"uses": "codecov/codecov-action@v3",
33+
},
34+
],
35+
},
36+
"block": [Function],
37+
},
38+
],
39+
}
40+
`);
41+
});
42+
43+
test("migration mode", () => {
44+
const creation = testBlock(blockCodecov, {
45+
mode: "migrate",
46+
options: optionsBase,
47+
});
48+
49+
expect(creation).toMatchInlineSnapshot(`
50+
{
51+
"addons": [
52+
{
53+
"addons": {
54+
"apps": [
55+
{
56+
"name": "Codecov",
57+
"url": "https://github.com/apps/codecov",
58+
},
59+
],
60+
},
61+
"block": [Function],
62+
},
63+
{
64+
"addons": {
65+
"actionSteps": [
66+
{
67+
"if": "always()",
68+
"uses": "codecov/codecov-action@v3",
69+
},
70+
],
71+
},
72+
"block": [Function],
73+
},
74+
],
75+
"scripts": [
76+
{
77+
"commands": [
78+
"rm .github/codecov.yml codecov.yml",
79+
],
80+
"phase": 0,
81+
},
82+
],
83+
}
84+
`);
85+
});
86+
87+
test("with addons", () => {
88+
const creation = testBlock(blockCodecov, {
89+
addons: {
90+
env: {
91+
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}",
92+
},
93+
},
94+
options: optionsBase,
95+
});
96+
97+
expect(creation).toMatchInlineSnapshot(`
98+
{
99+
"addons": [
100+
{
101+
"addons": {
102+
"apps": [
103+
{
104+
"name": "Codecov",
105+
"url": "https://github.com/apps/codecov",
106+
},
107+
],
108+
},
109+
"block": [Function],
110+
},
111+
{
112+
"addons": {
113+
"actionSteps": [
114+
{
115+
"env": {
116+
"CODECOV_TOKEN": "\${{ secrets.CODECOV_TOKEN }}",
117+
},
118+
"if": "always()",
119+
"uses": "codecov/codecov-action@v3",
120+
},
121+
],
122+
},
123+
"block": [Function],
124+
},
125+
],
126+
}
127+
`);
128+
});
129+
});

src/next/blocks/blockCodecov.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { z } from "zod";
2+
3+
import { base } from "../base.js";
4+
import { blockGitHubApps } from "./blockGitHubApps.js";
5+
import { blockVitest } from "./blockVitest.js";
6+
import { CommandPhase } from "./phases.js";
7+
8+
export const blockCodecov = base.createBlock({
9+
about: {
10+
name: "Codecov",
11+
},
12+
addons: { env: z.record(z.string(), z.string()).optional() },
13+
migrate() {
14+
return {
15+
scripts: [
16+
{
17+
commands: ["rm .github/codecov.yml codecov.yml"],
18+
phase: CommandPhase.Migrations,
19+
},
20+
],
21+
};
22+
},
23+
produce({ addons }) {
24+
const { env } = addons;
25+
return {
26+
addons: [
27+
blockGitHubApps({
28+
apps: [
29+
{
30+
name: "Codecov",
31+
url: "https://github.com/apps/codecov",
32+
},
33+
],
34+
}),
35+
blockVitest({
36+
actionSteps: [
37+
{
38+
...(env && { env }),
39+
if: "always()",
40+
uses: "codecov/codecov-action@v3",
41+
},
42+
],
43+
}),
44+
],
45+
};
46+
},
47+
});

src/next/blocks/blockGitHubActionsCI.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { createSoloWorkflowFile } from "../../steps/writing/creation/dotGitHub/c
66
import { base } from "../base.js";
77
import { CommandPhase } from "./phases.js";
88

9+
export const zActionStep = z.intersection(
10+
z.object({
11+
env: z.record(z.string(), z.string()).optional(),
12+
if: z.string().optional(),
13+
with: z.record(z.string(), z.string()).optional(),
14+
}),
15+
z.union([z.object({ run: z.string() }), z.object({ uses: z.string() })]),
16+
);
17+
918
export const blockGitHubActionsCI = base.createBlock({
1019
about: {
1120
name: "GitHub Actions CI",
@@ -15,19 +24,7 @@ export const blockGitHubActionsCI = base.createBlock({
1524
.array(
1625
z.object({
1726
name: z.string(),
18-
steps: z.array(
19-
z.intersection(
20-
z.object({
21-
env: z.record(z.string(), z.string()).optional(),
22-
if: z.string().optional(),
23-
with: z.record(z.string(), z.string()).optional(),
24-
}),
25-
z.union([
26-
z.object({ run: z.string() }),
27-
z.object({ uses: z.string() }),
28-
]),
29-
),
30-
),
27+
steps: z.array(zActionStep),
3128
}),
3229
)
3330
.optional(),

src/next/blocks/blockVitest.test.ts

+1-50
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,12 @@ describe("blockVitest", () => {
150150
{
151151
"run": "pnpm run test --coverage",
152152
},
153-
{
154-
"if": "always()",
155-
"uses": "codecov/codecov-action@v3",
156-
},
157153
],
158154
},
159155
],
160156
},
161157
"block": [Function],
162158
},
163-
{
164-
"addons": {
165-
"apps": [
166-
{
167-
"name": "Codecov",
168-
"url": "https://github.com/apps/codecov",
169-
},
170-
],
171-
},
172-
"block": [Function],
173-
},
174159
{
175160
"addons": {
176161
"properties": {
@@ -398,27 +383,12 @@ describe("blockVitest", () => {
398383
{
399384
"run": "pnpm run test --coverage",
400385
},
401-
{
402-
"if": "always()",
403-
"uses": "codecov/codecov-action@v3",
404-
},
405386
],
406387
},
407388
],
408389
},
409390
"block": [Function],
410391
},
411-
{
412-
"addons": {
413-
"apps": [
414-
{
415-
"name": "Codecov",
416-
"url": "https://github.com/apps/codecov",
417-
},
418-
],
419-
},
420-
"block": [Function],
421-
},
422392
{
423393
"addons": {
424394
"properties": {
@@ -499,7 +469,7 @@ describe("blockVitest", () => {
499469
"scripts": [
500470
{
501471
"commands": [
502-
"rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*",
472+
"rm .mocha* jest.config.* vitest.config.*",
503473
],
504474
"phase": 0,
505475
},
@@ -514,7 +484,6 @@ describe("blockVitest", () => {
514484
coverage: {
515485
directory: "coverage*",
516486
exclude: ["other"],
517-
flags: "unit",
518487
include: ["src/"],
519488
},
520489
exclude: ["lib/"],
@@ -662,30 +631,12 @@ describe("blockVitest", () => {
662631
{
663632
"run": "pnpm run test --coverage",
664633
},
665-
{
666-
"if": "always()",
667-
"uses": "codecov/codecov-action@v3",
668-
"with": {
669-
"flags": "unit",
670-
},
671-
},
672634
],
673635
},
674636
],
675637
},
676638
"block": [Function],
677639
},
678-
{
679-
"addons": {
680-
"apps": [
681-
{
682-
"name": "Codecov",
683-
"url": "https://github.com/apps/codecov",
684-
},
685-
],
686-
},
687-
"block": [Function],
688-
},
689640
{
690641
"addons": {
691642
"properties": {

src/next/blocks/blockVitest.ts

+6-27
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { blockCSpell } from "./blockCSpell.js";
55
import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js";
66
import { blockESLint } from "./blockESLint.js";
77
import { blockExampleFiles } from "./blockExampleFiles.js";
8-
import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
9-
import { blockGitHubApps } from "./blockGitHubApps.js";
8+
import { blockGitHubActionsCI, zActionStep } from "./blockGitHubActionsCI.js";
109
import { blockGitignore } from "./blockGitignore.js";
1110
import { blockPackageJson } from "./blockPackageJson.js";
1211
import { blockPrettier } from "./blockPrettier.js";
@@ -20,32 +19,28 @@ export const blockVitest = base.createBlock({
2019
name: "Vitest",
2120
},
2221
addons: {
22+
actionSteps: z.array(zActionStep).default([]),
2323
coverage: z
2424
.object({
2525
directory: z.string().optional(),
2626
exclude: z.array(z.string()).optional(),
27-
flags: z.string().optional(),
2827
include: z.array(z.string()).optional(),
2928
})
3029
.default({}),
31-
env: z.record(z.string(), z.string()).default({}),
3230
exclude: z.array(z.string()).default([]),
33-
flags: z.array(z.string()).default([]),
3431
},
3532
migrate() {
3633
return {
3734
scripts: [
3835
{
39-
commands: [
40-
"rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*",
41-
],
36+
commands: ["rm .mocha* jest.config.* vitest.config.*"],
4237
phase: CommandPhase.Migrations,
4338
},
4439
],
4540
};
4641
},
4742
produce({ addons }) {
48-
const { coverage, env, exclude = [], flags } = addons;
43+
const { actionSteps, coverage, exclude = [] } = addons;
4944
const coverageDirectory = coverage.directory ?? "coverage";
5045
const excludeText = JSON.stringify(exclude);
5146

@@ -152,23 +147,7 @@ describe("greet", () => {
152147
jobs: [
153148
{
154149
name: "Test",
155-
steps: [
156-
{ run: "pnpm run test --coverage" },
157-
{
158-
...(Object.keys(env).length && { env }),
159-
if: "always()",
160-
uses: "codecov/codecov-action@v3",
161-
...(coverage.flags && { with: { flags: coverage.flags } }),
162-
},
163-
],
164-
},
165-
],
166-
}),
167-
blockGitHubApps({
168-
apps: [
169-
{
170-
name: "Codecov",
171-
url: "https://github.com/apps/codecov",
150+
steps: [{ run: "pnpm run test --coverage" }, ...actionSteps],
172151
},
173152
],
174153
}),
@@ -181,7 +160,7 @@ describe("greet", () => {
181160
"vitest",
182161
),
183162
scripts: {
184-
test: `vitest ${flags.join(" ")}`.trim(),
163+
test: "vitest",
185164
},
186165
},
187166
}),

0 commit comments

Comments
 (0)