Skip to content

Commit

Permalink
feat(create-turbo): support bun (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Sep 13, 2023
1 parent ff636cd commit df28580
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 60 deletions.
5 changes: 4 additions & 1 deletion packages/create-turbo/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("create-turbo", () => {
{ packageManager: "yarn" },
{ packageManager: "npm" },
{ packageManager: "pnpm" },
{ packageManager: "bun" },
])(
"outputs expected console messages when using $packageManager",
async ({ packageManager }) => {
Expand All @@ -42,6 +43,7 @@ describe("create-turbo", () => {
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
bun: "1.0.1",
});

const mockCreateProject = jest
Expand Down Expand Up @@ -98,7 +100,7 @@ describe("create-turbo", () => {
}
);

test.only("throws correct error message when a download error is encountered", async () => {
test("throws correct error message when a download error is encountered", async () => {
const { root } = useFixture({ fixture: `create-turbo` });
const packageManager = "pnpm";
const mockAvailablePackageManagers = jest
Expand All @@ -107,6 +109,7 @@ describe("create-turbo", () => {
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
bun: "1.0.1",
});

const mockCreateProject = jest
Expand Down
13 changes: 9 additions & 4 deletions packages/create-turbo/src/commands/create/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ export async function packageManager({
// prompt for package manager if it wasn't provided as an argument, or if it was
// provided, but isn't available (always allow npm)
!manager || !availablePackageManagers[manager as PackageManager],
choices: ["npm", "pnpm", "yarn"].map((p) => ({
name: p,
value: p,
disabled: availablePackageManagers[p as PackageManager]
choices: [
{ pm: "npm", label: "npm workspaces" },
{ pm: "pnpm", label: "pnpm workspaces" },
{ pm: "yarn", label: "yarn workspaces" },
{ pm: "bun", label: "bun workspaces (beta)" },
].map(({ pm, label }) => ({
name: label,
value: pm,
disabled: availablePackageManagers[pm as PackageManager]
? false
: `not installed`,
})),
Expand Down
1 change: 0 additions & 1 deletion packages/turbo-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export { convertCase } from "./convertCase";
export * as logger from "./logger";

// types
export type { PackageManagerAvailable } from "./managers";
export type { RepoInfo } from "./examples";
export type {
TurboConfig,
Expand Down
15 changes: 7 additions & 8 deletions packages/turbo-utils/src/managers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import os from "node:os";
import type { Options } from "execa";
import execa from "execa";

export type PackageManager = "npm" | "yarn" | "pnpm";
export interface PackageManagerAvailable {
available: boolean;
version?: string;
}
import type { PackageManager } from "./types";

async function exec(command: string, args: Array<string> = [], opts?: Options) {
// run the check from tmpdir to avoid corepack conflicting -
Expand All @@ -28,31 +23,35 @@ async function exec(command: string, args: Array<string> = [], opts?: Options) {
export async function getAvailablePackageManagers(): Promise<
Record<PackageManager, string | undefined>
> {
const [yarn, npm, pnpm] = await Promise.all([
const [yarn, npm, pnpm, bun] = await Promise.all([
exec("yarnpkg", ["--version"]),
exec("npm", ["--version"]),
exec("pnpm", ["--version"]),
exec("bun", ["--version"]),
]);

return {
yarn,
pnpm,
npm,
bun,
};
}

export async function getPackageManagersBinPaths(): Promise<
Record<PackageManager, string | undefined>
> {
const [yarn, npm, pnpm] = await Promise.all([
const [yarn, npm, pnpm, bun] = await Promise.all([
exec("yarnpkg", ["global", "bin"]),
exec("npm", ["config", "get", "prefix"]),
exec("pnpm", ["bin", "--global"]),
exec("bun", ["pm", "--g", "bin"]),
]);

return {
yarn,
pnpm,
npm,
bun,
};
}
2 changes: 2 additions & 0 deletions packages/turbo-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Schema } from "@turbo/types";

export type PackageManager = "npm" | "yarn" | "pnpm" | "bun";

export type DependencyList = Record<string, string>;

export interface DependencyGroups {
Expand Down
1 change: 1 addition & 0 deletions packages/turbo-workspaces/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("Node entrypoint", () => {
npm: "8.19.2",
yarn: "1.22.19",
pnpm: "7.29.1",
bun: "1.0.1",
});

const { root } = useFixture({
Expand Down
1 change: 1 addition & 0 deletions packages/turbo-workspaces/__tests__/managers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ describe("managers", () => {

await MANAGERS[toManager].convertLock({
project,
to: { name: toManager, version: "1.2.3" },
logger: new Logger(),
options: {
interactive,
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-workspaces/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PackageManager } from "../src/types";
import type { PackageManager } from "@turbo/utils";

const PACKAGE_MANAGERS: Array<PackageManager> = ["pnpm", "npm", "yarn"];
const REPO_TYPES: Array<"monorepo" | "non-monorepo"> = [
Expand Down
16 changes: 10 additions & 6 deletions packages/turbo-workspaces/src/commands/convert/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import inquirer from "inquirer";
import chalk from "chalk";
import { getAvailablePackageManagers } from "@turbo/utils";
import { getAvailablePackageManagers, type PackageManager } from "@turbo/utils";
import { Logger } from "../../logger";
import { directoryInfo } from "../../utils";
import { getWorkspaceDetails } from "../../getWorkspaceDetails";
import type { PackageManager } from "../../types";
import { convertProject } from "../../convert";
import type { ConvertCommandArgument, ConvertCommandOptions } from "./types";

Expand Down Expand Up @@ -80,11 +79,16 @@ export async function convertCommand(
when:
!packageManager ||
!Object.keys(availablePackageManagers).includes(packageManager),
choices: ["npm", "pnpm", "yarn"].map((p) => ({
name: `${p} workspaces`,
value: p,
choices: [
{ pm: "npm", label: "npm workspaces" },
{ pm: "pnpm", label: "pnpm workspaces" },
{ pm: "yarn", label: "yarn workspaces" },
{ pm: "bun", label: "bun workspaces (beta)" },
].map(({ pm, label }) => ({
name: label,
value: pm,
disabled: isPackageManagerDisabled({
packageManager: p as PackageManager,
packageManager: pm as PackageManager,
currentWorkspaceManger: project.packageManager,
availablePackageManagers,
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-workspaces/src/commands/summary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export async function summaryCommand(directory: SummaryCommandArgument) {
Object.keys(workspacesByDirectory).forEach((dir, idx) => {
renderDirectory({
number: idx + 1,
workspaces: workspacesByDirectory[directory],
workspaces: workspacesByDirectory[dir],
dir,
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-workspaces/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function convertProject({

logger.mainStep("Installing dependencies");
if (!options?.skipInstall) {
await MANAGERS[to.name].convertLock({ project, logger, options });
await MANAGERS[to.name].convertLock({ project, to, logger, options });
await install({ project, to, logger, options });
} else {
logger.subStep(chalk.yellow("Skipping install"));
Expand Down
10 changes: 2 additions & 8 deletions packages/turbo-workspaces/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { getAvailablePackageManagers } from "@turbo/utils";
import { getAvailablePackageManagers, type PackageManager } from "@turbo/utils";
import { getWorkspaceDetails } from "./getWorkspaceDetails";
import { convertProject } from "./convert";
import { Logger } from "./logger";
import { install, getPackageManagerMeta } from "./install";
import { ConvertError } from "./errors";
import { MANAGERS } from "./managers";
import type {
PackageManager,
Options,
InstallArgs,
Workspace,
Project,
} from "./types";
import type { Options, InstallArgs, Workspace, Project } from "./types";
import type { ConvertErrorType } from "./errors";

async function convert({
Expand Down
14 changes: 13 additions & 1 deletion packages/turbo-workspaces/src/install.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { PackageManager } from "@turbo/utils";
import execa from "execa";
import ora from "ora";
import { satisfies } from "semver";
import { ConvertError } from "./errors";
import { Logger } from "./logger";
import type {
PackageManager,
RequestedPackageManagerDetails,
PackageManagerInstallDetails,
InstallArgs,
Expand Down Expand Up @@ -68,6 +68,18 @@ export const PACKAGE_MANAGERS: Record<
semver: ">=2",
},
],
bun: [
{
name: "bun1",
template: "bun",
command: "bun",
installArgs: ["install"],
version: "1.x",
executable: "bunx",
semver: "<2",
default: true,
},
],
};

export function getPackageManagerMeta(
Expand Down
Loading

0 comments on commit df28580

Please sign in to comment.