Skip to content

Commit c97a8ca

Browse files
feat: add intake to blockTypeScript (#2164)
## PR Checklist - [x] Addresses an existing open issue: fixes #2115 - [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 6f1bd7c commit c97a8ca

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/blocks/blockTypeScript.test.ts

Lines changed: 44 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 } from "vitest";
1+
import { testBlock, testIntake } from "bingo-stratum-testers";
2+
import { describe, expect, it, test } from "vitest";
33

44
import { blockTypeScript } from "./blockTypeScript.js";
55
import { optionsBase } from "./options.fakes.js";
@@ -652,4 +652,46 @@ describe("blockTypeScript", () => {
652652
}
653653
`);
654654
});
655+
656+
describe("intake", () => {
657+
it("returns undefined when tsconfig.json does not exist", () => {
658+
const actual = testIntake(blockTypeScript, {
659+
files: {},
660+
});
661+
662+
expect(actual).toBeUndefined();
663+
});
664+
665+
it("returns undefined when tsconfig.json does not contain truthy data", () => {
666+
const actual = testIntake(blockTypeScript, {
667+
files: {
668+
"tsconfig.json": [JSON.stringify(null)],
669+
},
670+
});
671+
672+
expect(actual).toBeUndefined();
673+
});
674+
675+
it("returns undefined when tsconfig.json does not contain compilerOptions", () => {
676+
const actual = testIntake(blockTypeScript, {
677+
files: {
678+
"tsconfig.json": [JSON.stringify({ other: true })],
679+
},
680+
});
681+
682+
expect(actual).toBeUndefined();
683+
});
684+
685+
it("returns compilerOptions when tsconfig.json contains compilerOptions", () => {
686+
const compilerOptions = { module: "ESNext" };
687+
688+
const actual = testIntake(blockTypeScript, {
689+
files: {
690+
"tsconfig.json": [JSON.stringify({ compilerOptions })],
691+
},
692+
});
693+
694+
expect(actual).toEqual({ compilerOptions });
695+
});
696+
});
655697
});

src/blocks/blockTypeScript.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import JSON5 from "json5";
12
import sortKeys from "sort-keys";
23
import { CompilerOptionsSchema } from "zod-tsconfig";
34

@@ -14,6 +15,7 @@ import { blockPackageJson } from "./blockPackageJson.js";
1415
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
1516
import { blockVitest } from "./blockVitest.js";
1617
import { blockVSCode } from "./blockVSCode.js";
18+
import { intakeFile } from "./intake/intakeFile.js";
1719

1820
export const blockTypeScript = base.createBlock({
1921
about: {
@@ -22,6 +24,22 @@ export const blockTypeScript = base.createBlock({
2224
addons: {
2325
compilerOptions: CompilerOptionsSchema.optional(),
2426
},
27+
intake({ files }) {
28+
const tsconfig = intakeFile(files, ["tsconfig.json"]);
29+
if (!tsconfig) {
30+
return undefined;
31+
}
32+
33+
const raw = JSON5.parse<Record<string, undefined> | undefined>(tsconfig[0]);
34+
const { data } = CompilerOptionsSchema.safeParse(raw?.compilerOptions);
35+
if (!data) {
36+
return undefined;
37+
}
38+
39+
return {
40+
compilerOptions: data,
41+
};
42+
},
2543
produce({ addons, options }) {
2644
const { compilerOptions } = addons;
2745
const primaryBin = getPrimaryBin(options.bin, options.repository);

0 commit comments

Comments
 (0)