这是indexloc提供的服务,不要输入任何密码
Skip to content
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
39 changes: 39 additions & 0 deletions packages/create-turbo/__tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path";
import childProcess from "node:child_process";
import fs from "node:fs";
import { setupTestFixtures } from "@turbo/test-utils";
import { describe, it, expect, jest } from "@jest/globals";
import {
Expand All @@ -9,6 +10,7 @@ import {
isInGitRepository,
isInMercurialRepository,
tryGitInit,
removeGitDirectory,
} from "../src/utils/git";

describe("git", () => {
Expand Down Expand Up @@ -243,4 +245,41 @@ describe("git", () => {
mockExecSync.mockRestore();
});
});

describe("removeGitDirectory", () => {
const { useFixture } = setupTestFixtures({
directory: path.join(__dirname, "../"),
options: { emptyFixture: true },
});

it("attempts to remove .git directory", async () => {
const { root } = useFixture({ fixture: `remove-git` });
const mockRmSync = jest.spyOn(fs, "rmSync").mockImplementation(() => {});

const result = removeGitDirectory(root);
expect(result).toBe(true);

expect(mockRmSync).toHaveBeenCalledWith(path.join(root, ".git"), {
recursive: true,
force: true,
});
mockRmSync.mockRestore();
});

it("returns false on error", async () => {
const { root } = useFixture({ fixture: `remove-git-error` });
const mockRmSync = jest.spyOn(fs, "rmSync").mockImplementation(() => {
throw new Error("Permission denied");
});

const result = removeGitDirectory(root);
expect(result).toBe(false);

expect(mockRmSync).toHaveBeenCalledWith(path.join(root, ".git"), {
recursive: true,
force: true,
});
mockRmSync.mockRestore();
});
});
});
115 changes: 115 additions & 0 deletions packages/create-turbo/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CreateTurboTelemetry, TelemetryConfig } from "@turbo/telemetry";
import * as turboUtils from "@turbo/utils";
import { describe, it, expect, jest } from "@jest/globals";
import type { CreateCommandArgument } from "../src/commands/create/types";
import * as gitUtils from "../src/utils/git";
import { create } from "../src/commands/create";
import { getWorkspaceDetailsMockReturnValue } from "./test-utils";

Expand Down Expand Up @@ -275,4 +276,118 @@ describe("create-turbo", () => {
mockGetWorkspaceDetails.mockRestore();
mockExecSync.mockRestore();
});

it("removes .git directory when --no-git flag is used", async () => {
const { root } = useFixture({ fixture: "create-turbo-no-git" });
const packageManager = "npm";

const mockAvailablePackageManagers = jest
.spyOn(turboUtils, "getAvailablePackageManagers")
.mockResolvedValue({
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
bun: "1.0.1",
});

const mockCreateProject = jest
.spyOn(turboUtils, "createProject")
.mockResolvedValue({
cdPath: "",
hasPackageJson: true,
availableScripts: ["build", "test", "dev"],
});

const mockGetWorkspaceDetails = jest
.spyOn(turboWorkspaces, "getWorkspaceDetails")
.mockResolvedValue(
getWorkspaceDetailsMockReturnValue({
root,
packageManager,
})
);

const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockImplementation(() => {
return "success";
});

const mockRemoveGitDirectory = jest
.spyOn(gitUtils, "removeGitDirectory")
.mockReturnValue(true);

await create(root as CreateCommandArgument, {
packageManager,
skipInstall: true,
example: "default",
noGit: true,
telemetry,
});

expect(mockRemoveGitDirectory).toHaveBeenCalledWith(root);

mockAvailablePackageManagers.mockRestore();
mockCreateProject.mockRestore();
mockGetWorkspaceDetails.mockRestore();
mockExecSync.mockRestore();
mockRemoveGitDirectory.mockRestore();
});

it("does not remove .git directory when --no-git flag is not used", async () => {
const { root } = useFixture({ fixture: "create-turbo-with-git" });
const packageManager = "npm";

const mockAvailablePackageManagers = jest
.spyOn(turboUtils, "getAvailablePackageManagers")
.mockResolvedValue({
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
bun: "1.0.1",
});

const mockCreateProject = jest
.spyOn(turboUtils, "createProject")
.mockResolvedValue({
cdPath: "",
hasPackageJson: true,
availableScripts: ["build", "test", "dev"],
});

const mockGetWorkspaceDetails = jest
.spyOn(turboWorkspaces, "getWorkspaceDetails")
.mockResolvedValue(
getWorkspaceDetailsMockReturnValue({
root,
packageManager,
})
);

const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockImplementation(() => {
return "success";
});

const mockRemoveGitDirectory = jest
.spyOn(gitUtils, "removeGitDirectory")
.mockReturnValue(true);

await create(root as CreateCommandArgument, {
packageManager,
skipInstall: true,
example: "default",
noGit: false,
telemetry,
});

expect(mockRemoveGitDirectory).not.toHaveBeenCalled();

mockAvailablePackageManagers.mockRestore();
mockCreateProject.mockRestore();
mockGetWorkspaceDetails.mockRestore();
mockExecSync.mockRestore();
mockRemoveGitDirectory.mockRestore();
});
});
5 changes: 5 additions & 0 deletions packages/create-turbo/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ createTurboCli
--example-path foo/bar
`
)
.option(
"--no-git",
"Remove the .git directory after creating the project",
false
)
.version(cliPkg.version, "-v, --version", "Output the current version")
.helpOption("-h, --help", "Display help for command")
.action(create);
Expand Down
16 changes: 14 additions & 2 deletions packages/create-turbo/src/commands/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import {
DownloadError,
logger,
} from "@turbo/utils";
import { tryGitCommit, tryGitInit, tryGitAdd } from "../../utils/git";
import {
tryGitCommit,
tryGitInit,
tryGitAdd,
removeGitDirectory,
} from "../../utils/git";
import { isOnline } from "../../utils/isOnline";
import { transforms } from "../../transforms";
import { TransformError } from "../../transforms/errors";
Expand Down Expand Up @@ -78,7 +83,7 @@ export async function create(

let isMaintainedByCoreTeam = false;

const { packageManager, skipInstall, skipTransforms } = opts;
const { packageManager, skipInstall, skipTransforms, noGit } = opts;

const [online, availablePackageManagers] = await Promise.all([
isOnline(),
Expand Down Expand Up @@ -307,5 +312,12 @@ export async function create(
logger.log("- Run a command twice to hit cache");
}

// remove .git directory if --no-git flag is used
if (noGit) {
if (!removeGitDirectory(root)) {
logger.warn("Failed to remove '.git' directory")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.warn("Failed to remove '.git' directory")
logger.warn("Failed to remove '.git' directory");

Missing semicolon after the logger.warn statement, which is inconsistent with the coding style used throughout the file.

View Details

Analysis

The logger.warn("Failed to remove '.git' directory") statement on line 318 is missing a semicolon at the end. This is inconsistent with the coding style used throughout the rest of the file, where all logger statements end with semicolons (as seen on lines 189, 300, 301, 302, etc.). While this may not cause a runtime error due to JavaScript's automatic semicolon insertion, it creates a style inconsistency that should be fixed for code maintainability and consistency.

The line should be:

logger.warn("Failed to remove '.git' directory");

}
}

opts.telemetry?.trackCommandStatus({ command: "create", status: "end" });
}
1 change: 1 addition & 0 deletions packages/create-turbo/src/commands/create/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export interface CreateCommandOptions {
turboVersion?: string;
example?: string;
examplePath?: string;
noGit?: boolean;
telemetry: CreateTurboTelemetry | undefined;
}
9 changes: 9 additions & 0 deletions packages/create-turbo/src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ function gitCommit(message: string) {
}
);
}

export function removeGitDirectory(root: string): boolean {
try {
rmSync(path.join(root, ".git"), { recursive: true, force: true });
return true;
} catch (_) {
return false;
}
}
Loading