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: clear local repository if hydrating from a template #92

Merged
merged 12 commits into from
Dec 31, 2024
2 changes: 1 addition & 1 deletion packages/create-testers/src/testBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
BlockWithoutAddons,
Creation,
produceBlock,
ProductionMode,
} from "create";
import { ProductionMode } from "create/lib/modes/types.js";

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

Expand Down
5 changes: 5 additions & 0 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
"execa": "^9.5.2",
"get-github-auth-token": "^0.1.0",
"hash-object": "^5.0.1",
"hosted-git-info": "^8.0.2",
"import-local-or-npx": "^0.1.0",
"octokit": "^4.0.2",
"prettier": "3.4.2",
"read-pkg": "^9.0.1",
"without-undefined-properties": "^0.1.1",
"zod": "^3.24.1"
},
"devDependencies": {
"@types/hosted-git-info": "^3.0.5"
},
"engines": {
"node": ">=18"
}
Expand Down
45 changes: 45 additions & 0 deletions packages/create/src/cli/createInitialCommit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, test, vi } from "vitest";

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

describe("createInitialCommit", () => {
test("runs without --amend when amend is falsy", async () => {
const runner = vi.fn();

await createInitialCommit(runner);

expect(runner.mock.calls).toMatchInlineSnapshot(`
[
[
"git add -A",
],
[
"git commit --message feat:\\ initialized\\ repo\\ ✨ --no-gpg-sign",
],
[
"git push -u origin main --force",
],
]
`);
});

test("runs with --amend when amend is true", async () => {
const runner = vi.fn();

await createInitialCommit(runner, true);

expect(runner.mock.calls).toMatchInlineSnapshot(`
[
[
"git add -A",
],
[
"git commit --message feat:\\ initialized\\ repo\\ ✨ --amend --no-gpg-sign",
],
[
"git push -u origin main --force",
],
]
`);
});
});
14 changes: 14 additions & 0 deletions packages/create/src/cli/createInitialCommit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SystemRunner } from "../types/system.js";

export async function createInitialCommit(
runner: SystemRunner,
amend?: boolean,
) {
for (const command of [
`git add -A`,
`git commit --message feat:\\ initialized\\ repo\\ ✨ ${amend ? "--amend " : ""}--no-gpg-sign`,
`git push -u origin main --force`,
]) {
await runner(command);
}
}
10 changes: 9 additions & 1 deletion packages/create/src/cli/display/createClackDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import { CachedFactory } from "cached-factory";

import { SystemDisplay, SystemDisplayItem } from "../../types/system.js";

export interface ClackDisplay extends SystemDisplay {
dumpItems(): SystemItemsDump;
spinner: ClackSpinner;
}

// TODO: suggest making a type for all these things to Clack :)
export type ClackSpinner = ReturnType<typeof prompts.spinner>;

export type SystemItemsDump = Record<string, Record<string, SystemDisplayItem>>;

export function createClackDisplay() {
export function createClackDisplay(): ClackDisplay {
const spinner = prompts.spinner();
const groups = new CachedFactory<
string,
Expand Down
16 changes: 16 additions & 0 deletions packages/create/src/cli/display/runSpinnerTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ClackDisplay } from "./createClackDisplay.js";

export async function runSpinnerTask<T>(
display: ClackDisplay,
start: string,
stop: string,
task: () => Promise<T>,
) {
display.spinner.start(`${start}...`);

const result = await task();

display.spinner.stop(`${stop}.`);

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ vi.mock("../tryImportWithPredicate.js", () => ({
}));

describe("tryImportTemplatePreset", () => {
it("returns an error when from is undefined", async () => {
const actual = await tryImportTemplatePreset(undefined);

expect(actual).toEqual(
new Error("Please specify a package to create from."),
);
expect(mockTryImportWithPredicate).not.toHaveBeenCalled();
});

it("returns the error when tryImportWithPredicate resolves with an error", async () => {
const error = new Error("Oh no!");

Expand Down
6 changes: 5 additions & 1 deletion packages/create/src/cli/importers/tryImportTemplatePreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import { tryImportWithPredicate } from "../tryImportWithPredicate.js";
import { tryImportAndInstallIfNecessary } from "./tryImportAndInstallIfNecessary.js";

export async function tryImportTemplatePreset(
from: string,
from: string | undefined,
requestedPreset?: string,
) {
if (!from) {
return new Error("Please specify a package to create from.");
}

const template = await tryImportWithPredicate(
tryImportAndInstallIfNecessary,
from,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CreationOptions } from "../../modes/types.js";
export interface CreationOptions {
owner: string;
repository: string;
}

export function assertOptionsForInitialize(
options: object,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Octokit } from "octokit";

import { RepositoryTemplate } from "../types/bases.js";
import { CreationOptions } from "./types.js";
import { RepositoryTemplate } from "../../types/bases.js";
import { CreationOptions } from "./assertOptionsForInitialize.js";

export async function createRepositoryOnGitHub(
{ owner, repository }: CreationOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ describe("createTrackingBranches", () => {
[
"git remote add origin https://github.com/TestOwner/test-repository",
],
[
"git add -A",
],
[
"git commit --message "feat:\\ initialized\\ repo\\ ✨" --no-gpg-sign",
],
[
"git push -u origin main --force",
],
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SystemRunner } from "../types/system.js";
import { CreationOptions } from "./types.js";
import { SystemRunner } from "../../types/system.js";
import { CreationOptions } from "./assertOptionsForInitialize.js";

export async function createTrackingBranches(
{ owner, repository }: CreationOptions,
Expand All @@ -8,9 +8,6 @@ export async function createTrackingBranches(
for (const command of [
`git init`,
`git remote add origin https://github.com/${owner}/${repository}`,
`git add -A`,
`git commit --message "feat:\\ initialized\\ repo\\ ✨" --no-gpg-sign`,
`git push -u origin main --force`,
]) {
await runner(command);
}
Expand Down
Loading
Loading