generated from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fleshed out --help with template options (#117)
* test: fill in missing tryImportConfig tests The one root-level red line in coverage reports was bugging me. * feat: fleshed out --help with template options * Fix up linting and tests * Deduplicated spinner logic into tryImportTemplate * witho
- Loading branch information
1 parent
8a2853e
commit 224268c
Showing
51 changed files
with
1,162 additions
and
304 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
7 changes: 0 additions & 7 deletions
7
packages/create/src/cli/importers/tryImportAndInstallIfNecessary.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 |
---|---|---|
@@ -1,26 +1,19 @@ | ||
import * as prompts from "@clack/prompts"; | ||
import { importLocalOrNpx } from "import-local-or-npx"; | ||
|
||
import { isLocalPath } from "../utils.js"; | ||
|
||
export async function tryImportAndInstallIfNecessary( | ||
from: string, | ||
): Promise<Error | object> { | ||
const spinner = prompts.spinner(); | ||
spinner.start(`Retrieving ${from}`); | ||
|
||
const imported = await importLocalOrNpx(from, { | ||
// We ignore logs because we don't want to clutter CLI output | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
logger: () => {}, | ||
}); | ||
|
||
if (imported.kind === "failure") { | ||
spinner.stop(`Could not retrieve ${from}`); | ||
return isLocalPath(from) ? imported.local : imported.npx; | ||
} | ||
|
||
spinner.stop(`Retrieved ${from}`); | ||
|
||
return imported.resolved; | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/create/src/cli/importers/tryImportTemplate.test.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,58 @@ | ||
import chalk from "chalk"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { tryImportTemplate } from "./tryImportTemplate.js"; | ||
|
||
const mockSpinner = { | ||
start: vi.fn(), | ||
stop: vi.fn(), | ||
}; | ||
|
||
vi.mock("@clack/prompts", () => ({ | ||
spinner: () => mockSpinner, | ||
})); | ||
|
||
const mockTryImportWithPredicate = vi.fn(); | ||
|
||
vi.mock("../tryImportWithPredicate.js", () => ({ | ||
get tryImportWithPredicate() { | ||
return mockTryImportWithPredicate; | ||
}, | ||
})); | ||
|
||
describe("tryImportTemplate", () => { | ||
it("returns the error when tryImportWithPredicate resolves with an error", async () => { | ||
const error = new Error("Oh no!"); | ||
|
||
mockTryImportWithPredicate.mockResolvedValueOnce(error); | ||
|
||
const actual = await tryImportTemplate("create-my-app"); | ||
|
||
expect(actual).toEqual(error); | ||
expect(mockSpinner.start.mock.calls).toEqual([ | ||
[`Loading ${chalk.blue("create-my-app")}`], | ||
]); | ||
expect(mockSpinner.stop.mock.calls).toEqual([ | ||
[ | ||
`Could not load ${chalk.blue("create-my-app")}: ${chalk.red(error.message)}`, | ||
1, | ||
], | ||
]); | ||
}); | ||
|
||
it("returns the template when tryImportWithPredicate resolves with a preset", async () => { | ||
const template = { isTemplate: true }; | ||
|
||
mockTryImportWithPredicate.mockResolvedValueOnce(template); | ||
|
||
const actual = await tryImportTemplate("create-my-app"); | ||
|
||
expect(actual).toEqual(template); | ||
expect(mockSpinner.start.mock.calls).toEqual([ | ||
[`Loading ${chalk.blue("create-my-app")}`], | ||
]); | ||
expect(mockSpinner.stop.mock.calls).toEqual([ | ||
[`Loaded ${chalk.blue("create-my-app")}`], | ||
]); | ||
}); | ||
}); |
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,30 @@ | ||
import * as prompts from "@clack/prompts"; | ||
import chalk from "chalk"; | ||
|
||
import { isTemplate } from "../../predicates/isTemplate.js"; | ||
import { tryImportWithPredicate } from "../tryImportWithPredicate.js"; | ||
import { tryImportAndInstallIfNecessary } from "./tryImportAndInstallIfNecessary.js"; | ||
|
||
export async function tryImportTemplate(from: string) { | ||
const spinner = prompts.spinner(); | ||
spinner.start(`Loading ${chalk.blue(from)}`); | ||
|
||
const template = await tryImportWithPredicate( | ||
tryImportAndInstallIfNecessary, | ||
from, | ||
isTemplate, | ||
"Template", | ||
); | ||
|
||
if (template instanceof Error) { | ||
spinner.stop( | ||
`Could not load ${chalk.blue(from)}: ${chalk.red(template.message)}`, | ||
1, | ||
); | ||
return template; | ||
} | ||
|
||
spinner.stop(`Loaded ${chalk.blue(from)}`); | ||
|
||
return template; | ||
} |
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
11 changes: 2 additions & 9 deletions
11
packages/create/src/cli/importers/tryImportTemplatePreset.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
Oops, something went wrong.