diff --git a/src/next/blocks/blockCodecov.test.ts b/src/next/blocks/blockCodecov.test.ts new file mode 100644 index 000000000..878e424c3 --- /dev/null +++ b/src/next/blocks/blockCodecov.test.ts @@ -0,0 +1,129 @@ +import { testBlock } from "create-testers"; +import { describe, expect, test } from "vitest"; + +import { blockCodecov } from "./blockCodecov.js"; +import { optionsBase } from "./options.fakes.js"; + +describe("blockCodecov", () => { + test("without addons or mode", () => { + const creation = testBlock(blockCodecov, { + options: optionsBase, + }); + + expect(creation).toMatchInlineSnapshot(` + { + "addons": [ + { + "addons": { + "apps": [ + { + "name": "Codecov", + "url": "https://github.com/apps/codecov", + }, + ], + }, + "block": [Function], + }, + { + "addons": { + "actionSteps": [ + { + "if": "always()", + "uses": "codecov/codecov-action@v3", + }, + ], + }, + "block": [Function], + }, + ], + } + `); + }); + + test("migration mode", () => { + const creation = testBlock(blockCodecov, { + mode: "migrate", + options: optionsBase, + }); + + expect(creation).toMatchInlineSnapshot(` + { + "addons": [ + { + "addons": { + "apps": [ + { + "name": "Codecov", + "url": "https://github.com/apps/codecov", + }, + ], + }, + "block": [Function], + }, + { + "addons": { + "actionSteps": [ + { + "if": "always()", + "uses": "codecov/codecov-action@v3", + }, + ], + }, + "block": [Function], + }, + ], + "scripts": [ + { + "commands": [ + "rm .github/codecov.yml codecov.yml", + ], + "phase": 0, + }, + ], + } + `); + }); + + test("with addons", () => { + const creation = testBlock(blockCodecov, { + addons: { + env: { + CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}", + }, + }, + options: optionsBase, + }); + + expect(creation).toMatchInlineSnapshot(` + { + "addons": [ + { + "addons": { + "apps": [ + { + "name": "Codecov", + "url": "https://github.com/apps/codecov", + }, + ], + }, + "block": [Function], + }, + { + "addons": { + "actionSteps": [ + { + "env": { + "CODECOV_TOKEN": "\${{ secrets.CODECOV_TOKEN }}", + }, + "if": "always()", + "uses": "codecov/codecov-action@v3", + }, + ], + }, + "block": [Function], + }, + ], + } + `); + }); +}); diff --git a/src/next/blocks/blockCodecov.ts b/src/next/blocks/blockCodecov.ts new file mode 100644 index 000000000..36203aa96 --- /dev/null +++ b/src/next/blocks/blockCodecov.ts @@ -0,0 +1,47 @@ +import { z } from "zod"; + +import { base } from "../base.js"; +import { blockGitHubApps } from "./blockGitHubApps.js"; +import { blockVitest } from "./blockVitest.js"; +import { CommandPhase } from "./phases.js"; + +export const blockCodecov = base.createBlock({ + about: { + name: "Codecov", + }, + addons: { env: z.record(z.string(), z.string()).optional() }, + migrate() { + return { + scripts: [ + { + commands: ["rm .github/codecov.yml codecov.yml"], + phase: CommandPhase.Migrations, + }, + ], + }; + }, + produce({ addons }) { + const { env } = addons; + return { + addons: [ + blockGitHubApps({ + apps: [ + { + name: "Codecov", + url: "https://github.com/apps/codecov", + }, + ], + }), + blockVitest({ + actionSteps: [ + { + ...(env && { env }), + if: "always()", + uses: "codecov/codecov-action@v3", + }, + ], + }), + ], + }; + }, +}); diff --git a/src/next/blocks/blockGitHubActionsCI.ts b/src/next/blocks/blockGitHubActionsCI.ts index ad5f9d6e7..f9e995aa7 100644 --- a/src/next/blocks/blockGitHubActionsCI.ts +++ b/src/next/blocks/blockGitHubActionsCI.ts @@ -6,6 +6,15 @@ import { createSoloWorkflowFile } from "../../steps/writing/creation/dotGitHub/c import { base } from "../base.js"; import { CommandPhase } from "./phases.js"; +export const zActionStep = z.intersection( + z.object({ + env: z.record(z.string(), z.string()).optional(), + if: z.string().optional(), + with: z.record(z.string(), z.string()).optional(), + }), + z.union([z.object({ run: z.string() }), z.object({ uses: z.string() })]), +); + export const blockGitHubActionsCI = base.createBlock({ about: { name: "GitHub Actions CI", @@ -15,19 +24,7 @@ export const blockGitHubActionsCI = base.createBlock({ .array( z.object({ name: z.string(), - steps: z.array( - z.intersection( - z.object({ - env: z.record(z.string(), z.string()).optional(), - if: z.string().optional(), - with: z.record(z.string(), z.string()).optional(), - }), - z.union([ - z.object({ run: z.string() }), - z.object({ uses: z.string() }), - ]), - ), - ), + steps: z.array(zActionStep), }), ) .optional(), diff --git a/src/next/blocks/blockVitest.test.ts b/src/next/blocks/blockVitest.test.ts index 7e36afecb..3ae3e90fc 100644 --- a/src/next/blocks/blockVitest.test.ts +++ b/src/next/blocks/blockVitest.test.ts @@ -150,27 +150,12 @@ describe("blockVitest", () => { { "run": "pnpm run test --coverage", }, - { - "if": "always()", - "uses": "codecov/codecov-action@v3", - }, ], }, ], }, "block": [Function], }, - { - "addons": { - "apps": [ - { - "name": "Codecov", - "url": "https://github.com/apps/codecov", - }, - ], - }, - "block": [Function], - }, { "addons": { "properties": { @@ -398,27 +383,12 @@ describe("blockVitest", () => { { "run": "pnpm run test --coverage", }, - { - "if": "always()", - "uses": "codecov/codecov-action@v3", - }, ], }, ], }, "block": [Function], }, - { - "addons": { - "apps": [ - { - "name": "Codecov", - "url": "https://github.com/apps/codecov", - }, - ], - }, - "block": [Function], - }, { "addons": { "properties": { @@ -499,7 +469,7 @@ describe("blockVitest", () => { "scripts": [ { "commands": [ - "rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*", + "rm .mocha* jest.config.* vitest.config.*", ], "phase": 0, }, @@ -514,7 +484,6 @@ describe("blockVitest", () => { coverage: { directory: "coverage*", exclude: ["other"], - flags: "unit", include: ["src/"], }, exclude: ["lib/"], @@ -662,30 +631,12 @@ describe("blockVitest", () => { { "run": "pnpm run test --coverage", }, - { - "if": "always()", - "uses": "codecov/codecov-action@v3", - "with": { - "flags": "unit", - }, - }, ], }, ], }, "block": [Function], }, - { - "addons": { - "apps": [ - { - "name": "Codecov", - "url": "https://github.com/apps/codecov", - }, - ], - }, - "block": [Function], - }, { "addons": { "properties": { diff --git a/src/next/blocks/blockVitest.ts b/src/next/blocks/blockVitest.ts index d1281b79d..bdf8f6b9b 100644 --- a/src/next/blocks/blockVitest.ts +++ b/src/next/blocks/blockVitest.ts @@ -5,8 +5,7 @@ import { blockCSpell } from "./blockCSpell.js"; import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js"; import { blockESLint } from "./blockESLint.js"; import { blockExampleFiles } from "./blockExampleFiles.js"; -import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js"; -import { blockGitHubApps } from "./blockGitHubApps.js"; +import { blockGitHubActionsCI, zActionStep } from "./blockGitHubActionsCI.js"; import { blockGitignore } from "./blockGitignore.js"; import { blockPackageJson } from "./blockPackageJson.js"; import { blockPrettier } from "./blockPrettier.js"; @@ -20,32 +19,28 @@ export const blockVitest = base.createBlock({ name: "Vitest", }, addons: { + actionSteps: z.array(zActionStep).default([]), coverage: z .object({ directory: z.string().optional(), exclude: z.array(z.string()).optional(), - flags: z.string().optional(), include: z.array(z.string()).optional(), }) .default({}), - env: z.record(z.string(), z.string()).default({}), exclude: z.array(z.string()).default([]), - flags: z.array(z.string()).default([]), }, migrate() { return { scripts: [ { - commands: [ - "rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*", - ], + commands: ["rm .mocha* jest.config.* vitest.config.*"], phase: CommandPhase.Migrations, }, ], }; }, produce({ addons }) { - const { coverage, env, exclude = [], flags } = addons; + const { actionSteps, coverage, exclude = [] } = addons; const coverageDirectory = coverage.directory ?? "coverage"; const excludeText = JSON.stringify(exclude); @@ -152,23 +147,7 @@ describe("greet", () => { jobs: [ { name: "Test", - steps: [ - { run: "pnpm run test --coverage" }, - { - ...(Object.keys(env).length && { env }), - if: "always()", - uses: "codecov/codecov-action@v3", - ...(coverage.flags && { with: { flags: coverage.flags } }), - }, - ], - }, - ], - }), - blockGitHubApps({ - apps: [ - { - name: "Codecov", - url: "https://github.com/apps/codecov", + steps: [{ run: "pnpm run test --coverage" }, ...actionSteps], }, ], }), @@ -181,7 +160,7 @@ describe("greet", () => { "vitest", ), scripts: { - test: `vitest ${flags.join(" ")}`.trim(), + test: "vitest", }, }, }), diff --git a/src/next/presets/presetCommon.ts b/src/next/presets/presetCommon.ts index 2f402f447..fa0e03ef6 100644 --- a/src/next/presets/presetCommon.ts +++ b/src/next/presets/presetCommon.ts @@ -1,5 +1,6 @@ import { base } from "../base.js"; import { blockAllContributors } from "../blocks/blockAllContributors.js"; +import { blockCodecov } from "../blocks/blockCodecov.js"; import { blockReleaseIt } from "../blocks/blockReleaseIt.js"; import { blockVitest } from "../blocks/blockVitest.js"; import { presetMinimal } from "./presetMinimal.js"; @@ -13,6 +14,7 @@ export const presetCommon = base.createPreset({ blocks: [ ...presetMinimal.blocks, blockAllContributors, + blockCodecov, blockReleaseIt, blockVitest, ],