-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use Knip version from installed dependency (#1792)
## PR Checklist - [x] Addresses an existing open issue: fixes #1638 - [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 Uses the same `readFileSafeAsJson` strategy as `writePackageJson`. 💖
- Loading branch information
1 parent
d789679
commit b39d65f
Showing
7 changed files
with
116 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { createKnipConfig } from "./createKnipConfig.js"; | ||
|
||
const mockReadFileSafeAsJson = vi.fn(); | ||
|
||
vi.mock("../../../shared/readFileSafeAsJson.js", () => ({ | ||
get readFileSafeAsJson() { | ||
return mockReadFileSafeAsJson; | ||
}, | ||
})); | ||
|
||
describe("createKnipConfig", () => { | ||
it("uses uses the knip version from package.json devDependencies when it exists", async () => { | ||
const version = "1.2.3"; | ||
mockReadFileSafeAsJson.mockResolvedValueOnce({ | ||
devDependencies: { knip: version }, | ||
}); | ||
|
||
const packageJson = await createKnipConfig(); | ||
|
||
expect(JSON.parse(packageJson)).toEqual({ | ||
$schema: `https://unpkg.com/knip@${version}/schema.json`, | ||
entry: ["src/index.ts!"], | ||
ignoreExportsUsedInFile: { | ||
interface: true, | ||
type: true, | ||
}, | ||
project: ["src/**/*.ts!"], | ||
}); | ||
}); | ||
|
||
it("uses version 'latest' when the package.json does not exist", async () => { | ||
mockReadFileSafeAsJson.mockResolvedValueOnce(undefined); | ||
|
||
const packageJson = await createKnipConfig(); | ||
|
||
expect(JSON.parse(packageJson)).toEqual({ | ||
$schema: `https://unpkg.com/knip@latest/schema.json`, | ||
entry: ["src/index.ts!"], | ||
ignoreExportsUsedInFile: { | ||
interface: true, | ||
type: true, | ||
}, | ||
project: ["src/**/*.ts!"], | ||
}); | ||
}); | ||
|
||
it("uses version 'latest' when the package.json exists but does not have knip in devDependencies", async () => { | ||
mockReadFileSafeAsJson.mockResolvedValueOnce({ | ||
dependencies: {}, | ||
}); | ||
|
||
const packageJson = await createKnipConfig(); | ||
|
||
expect(JSON.parse(packageJson)).toEqual({ | ||
$schema: `https://unpkg.com/knip@latest/schema.json`, | ||
entry: ["src/index.ts!"], | ||
ignoreExportsUsedInFile: { | ||
interface: true, | ||
type: true, | ||
}, | ||
project: ["src/**/*.ts!"], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { readFileSafeAsJson } from "../../../shared/readFileSafeAsJson.js"; | ||
import { PartialPackageData } from "../../../shared/types.js"; | ||
import { formatJson } from "./formatters/formatJson.js"; | ||
|
||
export async function createKnipConfig() { | ||
const existingPackageJson = (await readFileSafeAsJson( | ||
"./package.json", | ||
)) as null | PartialPackageData; | ||
|
||
return await formatJson({ | ||
$schema: `https://unpkg.com/knip@${existingPackageJson?.devDependencies?.knip ?? "latest"}/schema.json`, | ||
entry: ["src/index.ts!"], | ||
ignoreExportsUsedInFile: { | ||
interface: true, | ||
type: true, | ||
}, | ||
project: ["src/**/*.ts!"], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters