Skip to content
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: add --words with default from cspell.json words #2021

Merged
merged 3 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ See [Bingo > Stratum > Concepts > Templates > `--preset`](https://www.create.bin
The following flags may be provided on the CLI to customize their values.
Each defaults to a value based on the running system, including an repository if transitioning one.

| Flag | Type | Description | Default |
| -------------- | ---------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| `--access` | `string` | Which [`npm publish --access`](https://docs.npmjs.com/cli/commands/npm-publish#access) to release npm packages with | `"public"` |
| `--author` | `string` | Username on npm to publish packages under | An existing npm author, or the currently logged in npm user, or `owner.toLowerCase()` |
| `--bin` | `string` | Value to set in `package.json`'s `"bin"` property, per [FAQs > How can I use `bin`?](./FAQs.md#how-can-i-use-bin) | _(none)_ |
| `--email` | `string` | Email address to be listed as the point of contact in docs and packages (e.g. `[email protected]`) | Yours from `gh`, `git config`, or `npm whoami` |
| `--emoji` | `string` | decorative emoji to use in descriptions and docs | The last emoji from `description`, or `"💖"` |
| `--funding` | `string` | GitHub organization or username to mention in `funding.yml` | The same as `owner` |
| `--keywords` | `string[]` | Any number of keywords to include in `package.json` | _(none)_ |
| `--owner` | `string` | Organization or user owning the repository | Yours from `gh` or `git config` |
| `--pnpm` | `string` | pnpm version for `package.json`'s `packageManager` field | Existing value in `package.json` if it exists |
| `--repository` | `string` | Name for the new repository | The same as `--directory` |
| `--title` | `string` | 'Title Case' title for the repository | Title-cased `repository` |
| `--version` | `string` | package version to publish as and store in `package.json` | Existing value in `package.json` if it exists, or `"0.0.0"` |
| Flag | Type | Description | Default |
| -------------- | ---------- | ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `--access` | `string` | Which [`npm publish --access`](https://docs.npmjs.com/cli/commands/npm-publish#access) to release npm packages with | `"public"` |
| `--author` | `string` | Username on npm to publish packages under | An existing npm author, or the currently logged in npm user, or `owner.toLowerCase()` |
| `--bin` | `string` | Value to set in `package.json`'s `"bin"` property, per [FAQs > How can I use `bin`?](./FAQs.md#how-can-i-use-bin) | _(none)_ |
| `--email` | `string` | Email address to be listed as the point of contact in docs and packages (e.g. `[email protected]`) | Yours from `gh`, `git config`, or `npm whoami` |
| `--emoji` | `string` | decorative emoji to use in descriptions and docs | The last emoji from `description`, or `"💖"` |
| `--funding` | `string` | GitHub organization or username to mention in `funding.yml` | The same as `owner` |
| `--keywords` | `string[]` | Any number of keywords to include in `package.json` | _(none)_ |
| `--owner` | `string` | Organization or user owning the repository | Yours from `gh` or `git config` |
| `--pnpm` | `string` | pnpm version for `package.json`'s `packageManager` field | Existing value in `package.json` if it exists |
| `--repository` | `string` | Name for the new repository | The same as `--directory` |
| `--title` | `string` | 'Title Case' title for the repository | Title-cased `repository` |
| `--version` | `string` | package version to publish as and store in `package.json` | Existing value in `package.json` if it exists, or `"0.0.0"` |
| `--words` | `string[]` | additional words to add to the CSpell dictionary | Existing `words` in a `cspell.json` file if it exists, and any new words in from other options |

For example, customizing the npm author and funding source to different values than what would be inferred:

Expand Down
2 changes: 2 additions & 0 deletions src/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe("base", () => {
title: "Create TypeScript App",
usage: expect.any(String),
version: expect.any(String),
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-require-imports
words: require("../cspell.json").words,
workflowsVersions: expect.any(Object),
});
});
Expand Down
8 changes: 8 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { readRepository } from "./options/readRepository.js";
import { readRulesetId } from "./options/readRulesetId.js";
import { readTitle } from "./options/readTitle.js";
import { readUsage } from "./options/readUsage.js";
import { readWords } from "./options/readWords.js";
import { readWorkflowsVersions } from "./options/readWorkflowsVersions.js";
import { zContributor, zWorkflowsVersions } from "./schemas.js";

Expand Down Expand Up @@ -158,6 +159,10 @@ export const base = createBase({
.string()
.optional()
.describe("package version to publish as and store in `package.json`"),
words: z
.array(z.string())
.optional()
.describe("additional words to add to the CSpell dictionary"),
workflowsVersions: zWorkflowsVersions
.optional()
.describe("existing versions of GitHub Actions workflows used"),
Expand Down Expand Up @@ -273,6 +278,8 @@ export const base = createBase({

const getVersion = lazyValue(async () => (await getPackageData()).version);

const getWords = lazyValue(async () => await readWords(take));

const getWorkflowData = lazyValue(
async () => await readWorkflowsVersions(take),
);
Expand Down Expand Up @@ -300,6 +307,7 @@ export const base = createBase({
title: getTitle,
usage: getUsage,
version: getVersion,
words: getWords,
workflowsVersions: getWorkflowData,
};
},
Expand Down
75 changes: 74 additions & 1 deletion src/blocks/blockCSpell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ vi.mock("../utils/resolveBin.js", () => ({
}));

describe("blockCSpell", () => {
test("without addons", () => {
test("without addons or options", () => {
const creation = testBlock(blockCSpell, {
options: optionsBase,
});
Expand Down Expand Up @@ -153,6 +153,79 @@ describe("blockCSpell", () => {
`);
});

test("with options", () => {
const creation = testBlock(blockCSpell, {
options: {
...optionsBase,
words: ["joshuakgoldberg"],
},
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": {
"Linting": {
"contents": {
"items": [
"- \`pnpm lint:spelling\` ([cspell](https://cspell.org)): Spell checks across all source files",
],
},
},
},
},
"block": [Function],
},
{
"addons": {
"extensions": [
"streetsidesoftware.code-spell-checker",
],
},
"block": [Function],
},
{
"addons": {
"jobs": [
{
"name": "Lint Spelling",
"steps": [
{
"run": "pnpm lint:spelling",
},
],
},
],
"removedWorkflows": [
"lint-spelling",
"spelling",
],
},
"block": [Function],
},
{
"addons": {
"properties": {
"devDependencies": {
"cspell": "8.17.5",
},
"scripts": {
"lint:spelling": "cspell "**" ".github/**/*"",
},
},
},
"block": [Function],
},
],
"files": {
"cspell.json": "{"dictionaries":["npm","node","typescript"],"ignorePaths":[".github","CHANGELOG.md","lib","node_modules","pnpm-lock.yaml"],"words":["joshuakgoldberg"]}",
},
}
`);
});

test("setup mode", () => {
const creation = testBlock(blockCSpell, {
mode: "setup",
Expand Down
8 changes: 6 additions & 2 deletions src/blocks/blockCSpell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export const blockCSpell = base.createBlock({
ignores: z.array(z.string()).default([]),
words: z.array(z.string()).default([]),
},
produce({ addons }) {
produce({ addons, options }) {
const { ignores, words } = addons;

const allWords = Array.from(
new Set([...(options.words ?? []), ...words]),
).sort();

return {
addons: [
blockDevelopmentDocs({
Expand Down Expand Up @@ -68,7 +72,7 @@ export const blockCSpell = base.createBlock({
"pnpm-lock.yaml",
...ignores,
].sort(),
...(words.length && { words: words.sort() }),
...(allWords.length && { words: allWords }),
}),
},
};
Expand Down
30 changes: 30 additions & 0 deletions src/options/readWords.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it, vi } from "vitest";

import { readWords } from "./readWords.js";

describe(readWords, () => {
it("returns undefined when the file does not exist", async () => {
const take = vi.fn().mockRejectedValueOnce(new Error("Oh no!"));

const actual = await readWords(take);

expect(actual).toBeUndefined();
});

it("returns undefined when the file has no words", async () => {
const take = vi.fn().mockResolvedValueOnce({});

const actual = await readWords(take);

expect(actual).toBeUndefined();
});

it("returns the words when the file has words", async () => {
const words = ["abc", "def"];
const take = vi.fn().mockResolvedValueOnce({ words });

const actual = await readWords(take);

expect(actual).toEqual(words);
});
});
15 changes: 15 additions & 0 deletions src/options/readWords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TakeInput } from "bingo";
import { inputFromFileJSON } from "input-from-file-json";

import { swallowErrorAsync } from "../utils/swallowErrorAsync.js";

export async function readWords(take: TakeInput) {
const cspell =
(await swallowErrorAsync(
take(inputFromFileJSON, {
filePath: "./cspell.json",
}),
)) || {};

return (cspell as { words?: string[] }).words;
}