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: interactive CLI with Clack (#72)
* feat: interactive CLI with Clack * fix up input-from-script * without-undefined-properties * fix: remove Math.random() * findPositionalFrom.test.ts * Reorganization * Undid bin/index.js changes * Add logError.ts * fix: bin/index.js with console * Basically the rest of it: positionals, suggestions, no more optionsAugment... * Most of the way through adding tests * Use import-local-or-npx * pnpm add import-local-or-npx * Fix linting * Don't crash on unknown zod schemas * override octokit system for runBlock and runPreset tests * missing Octokit import * more mock systems: produceBase, producePreset * one more produceBase system * eek .only
- Loading branch information
1 parent
a4bcc47
commit 78bc44b
Showing
95 changed files
with
4,022 additions
and
796 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env node | ||
import { runCli } from "../lib/cli/runCli.js"; | ||
|
||
process.exitCode = await runCli(...process.argv.slice(2)); | ||
process.exitCode = await runCli(process.argv.slice(2), console); |
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,58 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { findConfigFile } from "./findConfigFile.js"; | ||
|
||
const mockReaddir = vi.fn(); | ||
|
||
vi.mock("node:fs/promises", () => ({ | ||
get readdir() { | ||
return mockReaddir; | ||
}, | ||
})); | ||
|
||
describe("findConfigFile", () => { | ||
it("returns false when readdir rejects", async () => { | ||
mockReaddir.mockRejectedValueOnce(new Error("Oh no!")); | ||
|
||
const actual = await findConfigFile("."); | ||
|
||
expect(actual).toBe(undefined); | ||
}); | ||
|
||
it("returns a create.config.js when it exists", async () => { | ||
mockReaddir.mockResolvedValueOnce(["a", "create.config.js", "b"]); | ||
|
||
const actual = await findConfigFile("."); | ||
|
||
expect(actual).toBe("create.config.js"); | ||
}); | ||
|
||
it("returns a create.config.ts when it exists", async () => { | ||
mockReaddir.mockResolvedValueOnce(["a", "create.config.ts", "b"]); | ||
|
||
const actual = await findConfigFile("."); | ||
|
||
expect(actual).toBe("create.config.ts"); | ||
}); | ||
|
||
it("returns a create.config.ts when it is found after a create.config.js", async () => { | ||
mockReaddir.mockResolvedValueOnce([ | ||
"a", | ||
"create.config.js", | ||
"create.config.ts", | ||
"b", | ||
]); | ||
|
||
const actual = await findConfigFile("."); | ||
|
||
expect(actual).toBe("create.config.ts"); | ||
}); | ||
|
||
it("returns undefined when no create.config.* exists", async () => { | ||
mockReaddir.mockResolvedValueOnce(["a", "b"]); | ||
|
||
const actual = await findConfigFile("."); | ||
|
||
expect(actual).toBe(undefined); | ||
}); | ||
}); |
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,26 @@ | ||
import * as fs from "node:fs/promises"; | ||
|
||
export async function findConfigFile(directory: string) { | ||
try { | ||
const children = await fs.readdir(directory); | ||
let found: string | undefined; | ||
|
||
for (const child of children) { | ||
if (child === "create.config.ts") { | ||
return child; | ||
} | ||
|
||
if (isConfigFileName(child)) { | ||
found = child; | ||
} | ||
} | ||
|
||
return found; | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
function isConfigFileName(fileName: string) { | ||
return /create\.config\.\w+/.test(fileName); | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/create/src/cli/importers/tryImportAndInstallIfNecessary.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,78 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { tryImportAndInstallIfNecessary } from "./tryImportAndInstallIfNecessary.js"; | ||
|
||
const mockSpinner = { | ||
start: vi.fn(), | ||
stop: vi.fn(), | ||
}; | ||
|
||
vi.mock("@clack/prompts", () => ({ | ||
spinner: () => mockSpinner, | ||
})); | ||
|
||
const mockImportLocalOrNpx = vi.fn(); | ||
|
||
vi.mock("import-local-or-npx", () => ({ | ||
get importLocalOrNpx() { | ||
return mockImportLocalOrNpx; | ||
}, | ||
})); | ||
|
||
const errorLocal = new Error("Error: local"); | ||
const errorNpx = new Error("Error: npx"); | ||
|
||
describe("tryImportAndInstallIfNecessary", () => { | ||
it("returns the local error when importLocalOrNpx resolves with a failure for a local path", async () => { | ||
mockImportLocalOrNpx.mockResolvedValueOnce({ | ||
kind: "failure", | ||
local: errorLocal, | ||
npx: errorNpx, | ||
}); | ||
|
||
const actual = await tryImportAndInstallIfNecessary("../create-my-app"); | ||
|
||
expect(actual).toBe(errorLocal); | ||
expect(mockSpinner.start.mock.calls).toEqual([ | ||
["Retrieving ../create-my-app"], | ||
]); | ||
expect(mockSpinner.stop.mock.calls).toEqual([ | ||
["Could not retrieve ../create-my-app"], | ||
]); | ||
}); | ||
|
||
it("returns the npx error when importLocalOrNpx resolves with a failure for a package name", async () => { | ||
mockImportLocalOrNpx.mockResolvedValueOnce({ | ||
kind: "failure", | ||
local: errorLocal, | ||
npx: errorNpx, | ||
}); | ||
|
||
const actual = await tryImportAndInstallIfNecessary("create-my-app"); | ||
|
||
expect(actual).toBe(errorNpx); | ||
expect(mockSpinner.start.mock.calls).toEqual([ | ||
["Retrieving create-my-app"], | ||
]); | ||
expect(mockSpinner.stop.mock.calls).toEqual([ | ||
["Could not retrieve create-my-app"], | ||
]); | ||
}); | ||
|
||
it("returns the package when importLocalOrNpx resolves a package", async () => { | ||
const resolved = { happy: true }; | ||
|
||
mockImportLocalOrNpx.mockResolvedValueOnce({ | ||
kind: "local", | ||
resolved, | ||
}); | ||
|
||
const actual = await tryImportAndInstallIfNecessary("create-my-app"); | ||
|
||
expect(actual).toBe(resolved); | ||
expect(mockSpinner.start.mock.calls).toEqual([ | ||
["Retrieving create-my-app"], | ||
]); | ||
expect(mockSpinner.stop.mock.calls).toEqual([["Retrieved create-my-app"]]); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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); | ||
|
||
if (imported.kind === "failure") { | ||
spinner.stop(`Could not retrieve ${from}`); | ||
return isLocalPath(from) ? imported.local : imported.npx; | ||
} | ||
|
||
spinner.stop(`Retrieved ${from}`); | ||
|
||
return imported.resolved; | ||
} |
Oops, something went wrong.