-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: infer base from package json scripts during migration #993
Merged
JoshuaKGoldberg
merged 7 commits into
JoshuaKGoldberg:main
from
GV14982:feat/infer-base-from-scripts
Oct 31, 2023
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eb85456
feat: infer base from package json scripts during migration
GV14982 9f0ea34
remove console statement
GV14982 a1d71c3
add unit test, refactor getBase
GV14982 329c7fd
Merge branch 'main' into feat/infer-base-from-scripts
GV14982 c68a4a6
add additional test for `readOptions` function
GV14982 eafd4fb
rename to mockReadPackageData
JoshuaKGoldberg 4e17d37
Ah yes, getters
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { getBase } from "./getBase.js"; | ||
|
||
const readPackageData = vi.hoisted(() => vi.fn()); | ||
vi.mock("../packages.js", () => ({ | ||
readPackageData, | ||
})); | ||
|
||
describe("getBase", () => { | ||
it("should return minimum with minimum scripts", async () => { | ||
readPackageData.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
scripts: { | ||
build: "build", | ||
lint: "lint", | ||
test: "test", | ||
}, | ||
}), | ||
); | ||
|
||
expect(await getBase()).toBe("minimum"); | ||
}); | ||
it("should return common with common scripts", async () => { | ||
readPackageData.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
scripts: { | ||
build: "build", | ||
lint: "lint", | ||
"lint:knip": "knip", | ||
test: "test", | ||
}, | ||
}), | ||
); | ||
|
||
expect(await getBase()).toBe("common"); | ||
}); | ||
it("should return everything with everything scripts", async () => { | ||
readPackageData.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
scripts: { | ||
build: "build", | ||
lint: "lint", | ||
"lint:knip": "knip", | ||
"lint:md": "md", | ||
"lint:package-json": "package-json", | ||
"lint:packages": "packages", | ||
test: "test", | ||
}, | ||
}), | ||
); | ||
|
||
expect(await getBase()).toBe("everything"); | ||
}); | ||
}); |
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,34 @@ | ||
import { readPackageData } from "../packages.js"; | ||
import { OptionsBase } from "../types.js"; | ||
|
||
const commonScripts = new Set(["lint:knip", "should-semantic-release", "test"]); | ||
|
||
const everythingScripts = new Set([ | ||
"lint:md", | ||
"lint:package-json", | ||
"lint:packages", | ||
"lint:spelling", | ||
]); | ||
export async function getBase(): Promise<OptionsBase> { | ||
const scripts = Object.keys((await readPackageData()).scripts ?? {}); | ||
|
||
if ( | ||
scripts.reduce( | ||
(acc, curr) => (everythingScripts.has(curr) ? acc + 1 : acc), | ||
0, | ||
) >= 3 | ||
) { | ||
return "everything"; | ||
} | ||
|
||
if ( | ||
scripts.reduce( | ||
(acc, curr) => (commonScripts.has(curr) ? acc + 1 : acc), | ||
0, | ||
) >= 2 | ||
) { | ||
return "common"; | ||
} | ||
|
||
return "minimum"; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about
vi.hoisted
, very nice! https://vitest.dev/api/vi.html#vi-hoistedElsewhere in this repo I've used
mock*
as a naming convention. I think both are valid and reasonable... but themock*
is used everywhere else, so I'll go ahead and make the change.