Skip to content

Commit 5542d8b

Browse files
feat: add intake to blockCspell (#2165)
## PR Checklist - [x] Addresses an existing open issue: fixes #2113 - [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 🎁
1 parent 644f7a5 commit 5542d8b

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/blocks/blockCSpell.test.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { testBlock } from "bingo-stratum-testers";
2-
import { describe, expect, test, vi } from "vitest";
1+
import { testBlock, testIntake } from "bingo-stratum-testers";
2+
import { describe, expect, it, test, vi } from "vitest";
33

44
import { blockCSpell } from "./blockCSpell.js";
55
import { optionsBase } from "./options.fakes.js";
@@ -364,4 +364,49 @@ describe("blockCSpell", () => {
364364
}
365365
`);
366366
});
367+
368+
describe("intake", () => {
369+
it("returns undefined when cspell.json does not exist", () => {
370+
const actual = testIntake(blockCSpell, {
371+
files: {},
372+
});
373+
374+
expect(actual).toBeUndefined();
375+
});
376+
377+
it("returns undefined when cspell.json does not contain truthy data", () => {
378+
const actual = testIntake(blockCSpell, {
379+
files: {
380+
"cspell.json": [JSON.stringify(null)],
381+
},
382+
});
383+
384+
expect(actual).toBeUndefined();
385+
});
386+
387+
it("returns undefined when cspell.json contains invalid data", () => {
388+
const actual = testIntake(blockCSpell, {
389+
files: {
390+
"cspell.json": [JSON.stringify({ ignores: true })],
391+
},
392+
});
393+
394+
expect(actual).toBeUndefined();
395+
});
396+
397+
it("returns compilerOptions when cspell.json contains ignores and words", () => {
398+
const data = {
399+
ignores: ["other"],
400+
words: ["abc", "def"],
401+
};
402+
403+
const actual = testIntake(blockCSpell, {
404+
files: {
405+
"cspell.json": [JSON.stringify(data)],
406+
},
407+
});
408+
409+
expect(actual).toEqual(data);
410+
});
411+
});
367412
});

src/blocks/blockCSpell.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import JSON5 from "json5";
12
import { getObjectStringsDeep } from "object-strings-deep";
23
import { z } from "zod";
34

@@ -9,17 +10,35 @@ import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
910
import { blockPackageJson } from "./blockPackageJson.js";
1011
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
1112
import { blockVSCode } from "./blockVSCode.js";
13+
import { intakeFile } from "./intake/intakeFile.js";
1214
import { CommandPhase } from "./phases.js";
1315

1416
const filesGlob = `"**" ".github/**/*"`;
1517

18+
const addons = {
19+
ignores: z.array(z.string()).default([]),
20+
words: z.array(z.string()).default([]),
21+
};
22+
23+
const zAddons = z.object(addons);
24+
1625
export const blockCSpell = base.createBlock({
1726
about: {
1827
name: "CSpell",
1928
},
20-
addons: {
21-
ignores: z.array(z.string()).default([]),
22-
words: z.array(z.string()).default([]),
29+
addons,
30+
intake({ files }) {
31+
const cspellJson = intakeFile(files, ["cspell.json"]);
32+
if (!cspellJson) {
33+
return undefined;
34+
}
35+
36+
const { data } = zAddons.safeParse(JSON5.parse<unknown>(cspellJson[0]));
37+
if (!data) {
38+
return undefined;
39+
}
40+
41+
return data;
2342
},
2443
produce({ addons, options }) {
2544
const { ignores, words } = addons;

0 commit comments

Comments
 (0)