Skip to content

Commit

Permalink
feat(create-turbo): better error when GH is down
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Sep 7, 2023
1 parent c7eb255 commit b5f5d68
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 21 deletions.
70 changes: 64 additions & 6 deletions packages/create-turbo/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import path from "path";
import path from "node:path";
import childProcess from "node:child_process";
import chalk from "chalk";
import childProcess from "child_process";
import { setupTestFixtures, spyConsole } from "@turbo/test-utils";
import { create } from "../src/commands/create";
import type { CreateCommandArgument } from "../src/commands/create/types";
import { setupTestFixtures, spyConsole, spyExit } from "@turbo/test-utils";
import { logger } from "@turbo/utils";
import type { PackageManager } from "@turbo/workspaces";

// imports for mocks
import * as turboWorkspaces from "@turbo/workspaces";
import * as turboUtils from "@turbo/utils";
import type { CreateCommandArgument } from "../src/commands/create/types";
import { create } from "../src/commands/create";
import { getWorkspaceDetailsMockReturnValue } from "./test-utils";

jest.mock("@turbo/workspaces", () => ({
Expand All @@ -24,6 +23,7 @@ describe("create-turbo", () => {
});

const mockConsole = spyConsole();
const mockExit = spyExit();

test.each<{ packageManager: PackageManager }>([
{ packageManager: "yarn" },
Expand Down Expand Up @@ -97,4 +97,62 @@ describe("create-turbo", () => {
mockExecSync.mockRestore();
}
);

test.only("throws correct error message when a download error is encountered", async () => {
const { root } = useFixture({ fixture: `create-turbo` });
const packageManager = "pnpm";
const mockAvailablePackageManagers = jest
.spyOn(turboUtils, "getAvailablePackageManagers")
.mockResolvedValue({
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
});

const mockCreateProject = jest
.spyOn(turboUtils, "createProject")
.mockRejectedValue(new turboUtils.DownloadError("Could not connect"));

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

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

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

expect(mockConsole.error).toHaveBeenCalledTimes(2);
expect(mockConsole.error).toHaveBeenNthCalledWith(
1,
logger.turboRed.bold(">>>"),
chalk.red("Unable to download template from Github")
);
expect(mockConsole.error).toHaveBeenNthCalledWith(
2,
logger.turboRed.bold(">>>"),
chalk.red("Could not connect")
);
expect(mockExit.exit).toHaveBeenCalledWith(1);

mockAvailablePackageManagers.mockRestore();
mockCreateProject.mockRestore();
mockGetWorkspaceDetails.mockRestore();
mockExecSync.mockRestore();
});
});
2 changes: 1 addition & 1 deletion packages/create-turbo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"jest": "^27.4.3",
"ts-jest": "^27.1.1",
"tsup": "^6.7.0",
"typescript": "^4.5.5"
"typescript": "^5.2.2"
},
"files": [
"dist"
Expand Down
32 changes: 24 additions & 8 deletions packages/create-turbo/src/commands/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
getAvailablePackageManagers,
createProject,
DownloadError,
logger,
} from "@turbo/utils";
import { tryGitCommit, tryGitInit } from "../../utils/git";
Expand All @@ -33,8 +34,15 @@ function handleErrors(err: unknown) {
} else if (err instanceof ConvertError && err.type !== "unknown") {
error(chalk.red(err.message));
process.exit(1);
// handle unknown errors (no special handling, just re-throw to catch at root)
} else {
// handle download errors from @turbo/utils
} else if (err instanceof DownloadError) {
error(chalk.red("Unable to download template from Github"));
error(chalk.red(err.message));
process.exit(1);
}

// handle unknown errors (no special handling, just re-throw to catch at root)
else {
throw err;
}
}
Expand Down Expand Up @@ -85,12 +93,20 @@ export async function create(

const { example, examplePath } = opts;
const exampleName = example && example !== "default" ? example : "basic";
const { hasPackageJson, availableScripts, repoInfo } = await createProject({
appPath: root,
example: exampleName,
isDefaultExample: isDefaultExample(exampleName),
examplePath,
});

let projectData = {} as Awaited<ReturnType<typeof createProject>>;
try {
projectData = await createProject({
appPath: root,
example: exampleName,
isDefaultExample: isDefaultExample(exampleName),
examplePath,
});
} catch (err) {
handleErrors(err);
}

const { hasPackageJson, availableScripts, repoInfo } = projectData;

// create a new git repo after creating the project
tryGitInit(root, `feat(create-turbo): create ${exampleName}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export {
downloadAndExtractExample,
} from "./examples";
export { isWriteable } from "./isWriteable";
export { createProject } from "./createProject";
export { createProject, DownloadError } from "./createProject";
export { convertCase } from "./convertCase";

export * as logger from "./logger";
Expand Down
88 changes: 83 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5f5d68

Please sign in to comment.