Skip to content

Commit 525a3bc

Browse files
authoredApr 4, 2025··
feat: switch Knip entry and project to Addons (#2150)
## PR Checklist - [x] Addresses an existing open issue: fixes #2147 - [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 Goes a bit beyond #2147's original intended scope of `entry` by also making `project` an Addon. This way, `blockTypeScript` and `blockVitest` fully communicate all file globs to `blockKnip`. Also expands `blockKnip`'s `intake` to read both of those new Addon properties. This will allow customizations such as https://github.com/JoshuaKGoldberg/refined-saved-replies's `entry: ["src/content-script.ts"]` to be inferred. 🎁
1 parent 0dfbac0 commit 525a3bc

7 files changed

+106
-15
lines changed
 

Diff for: ‎knip.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://unpkg.com/knip@5.46.0/schema.json",
3-
"entry": ["src/index.ts", "src/**/*.test.*"],
3+
"entry": ["src/**/*.test.*", "src/index.ts"],
44
"ignoreDependencies": [
55
"all-contributors-cli",
66
"cspell-populate-words",

Diff for: ‎src/blocks/blockKnip.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("blockKnip", () => {
6161
},
6262
],
6363
"files": {
64-
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","entry":["src/index.ts","src/**/*.test.*"],"ignoreExportsUsedInFile":{"interface":true,"type":true},"project":["src/**/*.ts"]}",
64+
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","ignoreExportsUsedInFile":{"interface":true,"type":true}}",
6565
},
6666
}
6767
`);
@@ -70,7 +70,9 @@ describe("blockKnip", () => {
7070
test("with addons", () => {
7171
const creation = testBlock(blockKnip, {
7272
addons: {
73+
entry: ["src/index.ts"],
7374
ignoreDependencies: ["abc", "def"],
75+
project: ["src/**/*.ts"],
7476
},
7577
options: optionsBase,
7678
});
@@ -122,7 +124,7 @@ describe("blockKnip", () => {
122124
},
123125
],
124126
"files": {
125-
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","entry":["src/index.ts","src/**/*.test.*"],"ignoreDependencies":["abc","def"],"ignoreExportsUsedInFile":{"interface":true,"type":true},"project":["src/**/*.ts"]}",
127+
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","entry":["src/index.ts"],"ignoreDependencies":["abc","def"],"ignoreExportsUsedInFile":{"interface":true,"type":true},"project":["src/**/*.ts"]}",
126128
},
127129
}
128130
`);
@@ -201,7 +203,7 @@ describe("blockKnip", () => {
201203
},
202204
],
203205
"files": {
204-
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","entry":["src/index.ts","src/**/*.test.*"],"ignoreExportsUsedInFile":{"interface":true,"type":true},"project":["src/**/*.ts"]}",
206+
"knip.json": "{"$schema":"https://unpkg.com/knip@5.46.0/schema.json","ignoreExportsUsedInFile":{"interface":true,"type":true}}",
205207
},
206208
}
207209
`);

Diff for: ‎src/blocks/blockKnip.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import removeUndefinedObjects from "remove-undefined-objects";
12
import { z } from "zod";
23

34
import { base } from "../base.js";
@@ -12,29 +13,32 @@ import { blockRemoveFiles } from "./blockRemoveFiles.js";
1213
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
1314
import { intakeFileAsJson } from "./intake/intakeFileAsJson.js";
1415

15-
const zIgnoreDependencies = z.array(z.string());
16+
const zStringArray = z.array(z.string());
1617

1718
export const blockKnip = base.createBlock({
1819
about: {
1920
name: "Knip",
2021
},
2122
addons: {
22-
ignoreDependencies: zIgnoreDependencies.optional(),
23+
entry: zStringArray.optional(),
24+
ignoreDependencies: zStringArray.optional(),
25+
project: zStringArray.optional(),
2326
},
2427
intake({ files }) {
2528
const knipJson = intakeFileAsJson(files, ["knip.json"]);
26-
if (!knipJson?.ignoreDependencies) {
29+
if (!knipJson) {
2730
return undefined;
2831
}
2932

30-
return {
31-
ignoreDependencies: zIgnoreDependencies.safeParse(
32-
knipJson.ignoreDependencies,
33-
).data,
34-
};
33+
return removeUndefinedObjects({
34+
entry: zStringArray.safeParse(knipJson.entry).data,
35+
ignoreDependencies: zStringArray.safeParse(knipJson.ignoreDependencies)
36+
.data,
37+
project: zStringArray.safeParse(knipJson.project).data,
38+
});
3539
},
3640
produce({ addons }) {
37-
const { ignoreDependencies } = addons;
41+
const { entry, ignoreDependencies, project } = addons;
3842
return {
3943
addons: [
4044
blockDevelopmentDocs({
@@ -68,13 +72,13 @@ export const blockKnip = base.createBlock({
6872
files: {
6973
"knip.json": JSON.stringify({
7074
$schema: `https://unpkg.com/knip@${getPackageDependency("knip")}/schema.json`,
71-
entry: ["src/index.ts", "src/**/*.test.*"],
75+
entry: entry?.sort(),
7276
ignoreDependencies,
7377
ignoreExportsUsedInFile: {
7478
interface: true,
7579
type: true,
7680
},
77-
project: ["src/**/*.ts"],
81+
project: project?.sort(),
7882
}),
7983
},
8084
};

Diff for: ‎src/blocks/blockTypeScript.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ describe("blockTypeScript", () => {
9090
},
9191
"block": [Function],
9292
},
93+
{
94+
"addons": {
95+
"entry": [
96+
"src/index.ts",
97+
],
98+
"project": [
99+
"src/**/*.ts",
100+
],
101+
},
102+
"block": [Function],
103+
},
93104
{
94105
"addons": {
95106
"ignores": [
@@ -242,6 +253,17 @@ describe("blockTypeScript", () => {
242253
},
243254
"block": [Function],
244255
},
256+
{
257+
"addons": {
258+
"entry": [
259+
"src/index.ts",
260+
],
261+
"project": [
262+
"src/**/*.ts",
263+
],
264+
},
265+
"block": [Function],
266+
},
245267
{
246268
"addons": {
247269
"ignores": [
@@ -392,6 +414,17 @@ describe("blockTypeScript", () => {
392414
},
393415
"block": [Function],
394416
},
417+
{
418+
"addons": {
419+
"entry": [
420+
"src/index.ts",
421+
],
422+
"project": [
423+
"src/**/*.ts",
424+
],
425+
},
426+
"block": [Function],
427+
},
395428
{
396429
"addons": {
397430
"ignores": [
@@ -551,6 +584,17 @@ describe("blockTypeScript", () => {
551584
},
552585
"block": [Function],
553586
},
587+
{
588+
"addons": {
589+
"entry": [
590+
"src/index.ts",
591+
],
592+
"project": [
593+
"src/**/*.ts",
594+
],
595+
},
596+
"block": [Function],
597+
},
554598
{
555599
"addons": {
556600
"ignores": [

Diff for: ‎src/blocks/blockTypeScript.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js";
88
import { blockExampleFiles } from "./blockExampleFiles.js";
99
import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
1010
import { blockGitignore } from "./blockGitignore.js";
11+
import { blockKnip } from "./blockKnip.js";
1112
import { blockMarkdownlint } from "./blockMarkdownlint.js";
1213
import { blockPackageJson } from "./blockPackageJson.js";
1314
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
@@ -81,6 +82,10 @@ export * from "./types.js";
8182
blockGitHubActionsCI({
8283
jobs: [{ name: "Type Check", steps: [{ run: "pnpm tsc" }] }],
8384
}),
85+
blockKnip({
86+
entry: ["src/index.ts"],
87+
project: ["src/**/*.ts"],
88+
}),
8489
blockMarkdownlint({
8590
ignores: ["lib/"],
8691
}),

Diff for: ‎src/blocks/blockVitest.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ describe("blockVitest", () => {
165165
},
166166
"block": [Function],
167167
},
168+
{
169+
"addons": {
170+
"entry": [
171+
"src/**/*.test.*",
172+
],
173+
},
174+
"block": [Function],
175+
},
168176
{
169177
"addons": {
170178
"properties": {
@@ -403,6 +411,14 @@ describe("blockVitest", () => {
403411
},
404412
"block": [Function],
405413
},
414+
{
415+
"addons": {
416+
"entry": [
417+
"src/**/*.test.*",
418+
],
419+
},
420+
"block": [Function],
421+
},
406422
{
407423
"addons": {
408424
"properties": {
@@ -679,6 +695,14 @@ describe("blockVitest", () => {
679695
},
680696
"block": [Function],
681697
},
698+
{
699+
"addons": {
700+
"entry": [
701+
"src/**/*.test.*",
702+
],
703+
},
704+
"block": [Function],
705+
},
682706
{
683707
"addons": {
684708
"properties": {
@@ -926,6 +950,14 @@ describe("blockVitest", () => {
926950
},
927951
"block": [Function],
928952
},
953+
{
954+
"addons": {
955+
"entry": [
956+
"src/**/*.test.*",
957+
],
958+
},
959+
"block": [Function],
960+
},
929961
{
930962
"addons": {
931963
"properties": {

Diff for: ‎src/blocks/blockVitest.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { blockESLint } from "./blockESLint.js";
99
import { blockExampleFiles } from "./blockExampleFiles.js";
1010
import { blockGitHubActionsCI, zActionStep } from "./blockGitHubActionsCI.js";
1111
import { blockGitignore } from "./blockGitignore.js";
12+
import { blockKnip } from "./blockKnip.js";
1213
import { blockPackageJson } from "./blockPackageJson.js";
1314
import { blockPrettier } from "./blockPrettier.js";
1415
import { blockRemoveDependencies } from "./blockRemoveDependencies.js";
@@ -193,6 +194,9 @@ describe(greet, () => {
193194
},
194195
],
195196
}),
197+
blockKnip({
198+
entry: ["src/**/*.test.*"],
199+
}),
196200
blockPackageJson({
197201
properties: {
198202
devDependencies: getPackageDependencies(

0 commit comments

Comments
 (0)
Please sign in to comment.