Skip to content

Commit

Permalink
Review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Sep 14, 2023
1 parent 76430c6 commit 618f867
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 81 deletions.
4 changes: 2 additions & 2 deletions packages/turbo-workspaces/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Project } from "../src/types";
import { isCompatibleWithBunWorkspace } from "../src/utils";
import { isCompatibleWithBunWorkspaces } from "../src/utils";

describe("utils", () => {
describe("isCompatibleWithBunWorkspace", () => {
Expand All @@ -12,7 +12,7 @@ describe("utils", () => {
{ globs: ["apps/*", "packages/*/utils/*"], expected: false },
{ globs: ["internal-*/*"], expected: false },
])("should return $result when given %globs", ({ globs, expected }) => {
const result = isCompatibleWithBunWorkspace({
const result = isCompatibleWithBunWorkspaces({
project: {
workspaceData: { globs },
} as Project,
Expand Down
6 changes: 3 additions & 3 deletions packages/turbo-workspaces/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export const PACKAGE_MANAGERS: Record<
],
bun: [
{
name: "bun1",
name: "bun",
template: "bun",
command: "bun",
installArgs: ["install"],
version: "1.x",
version: "latest",
executable: "bunx",
semver: "<2",
semver: "^1.0.1",
default: true,
},
],
Expand Down
55 changes: 42 additions & 13 deletions packages/turbo-workspaces/src/managers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
CleanArgs,
Project,
ManagerHandler,
Manager,
} from "../types";
import {
getMainStep,
Expand All @@ -20,9 +21,15 @@ import {
expandWorkspaces,
getWorkspacePackageManager,
parseWorkspacePackages,
isCompatibleWithBunWorkspace,
isCompatibleWithBunWorkspaces,
removeLockFile,
} from "../utils";

const PACKAGE_MANAGER_DETAILS: Manager = {
name: "bun",
lock: "bun.lockb",
};

/**
* Check if a given project is using bun workspaces
* Verify by checking for the existence of:
Expand All @@ -31,11 +38,13 @@ import {
*/
// eslint-disable-next-line @typescript-eslint/require-await -- must match the detect type signature
async function detect(args: DetectArgs): Promise<boolean> {
const lockFile = path.join(args.workspaceRoot, "bun.lockb");
const lockFile = path.join(args.workspaceRoot, PACKAGE_MANAGER_DETAILS.lock);
const packageManager = getWorkspacePackageManager({
workspaceRoot: args.workspaceRoot,
});
return existsSync(lockFile) || packageManager === "bun";
return (
existsSync(lockFile) || packageManager === PACKAGE_MANAGER_DETAILS.name
);
}

/**
Expand All @@ -57,10 +66,10 @@ async function read(args: ReadArgs): Promise<Project> {
return {
name,
description,
packageManager: "bun",
packageManager: PACKAGE_MANAGER_DETAILS.name,
paths: expandPaths({
root: args.workspaceRoot,
lockFile: "bun.lockb",
lockFile: PACKAGE_MANAGER_DETAILS.lock,
}),
workspaceData: {
globs: workspaceGlobs,
Expand All @@ -86,7 +95,7 @@ async function create(args: CreateArgs): Promise<void> {
const { project, to, logger, options } = args;
const hasWorkspaces = project.workspaceData.globs.length > 0;

if (!isCompatibleWithBunWorkspace({ project })) {
if (!isCompatibleWithBunWorkspaces({ project })) {
throw new ConvertError(
"Unable to convert project to bun - workspace globs unsupported",
{
Expand All @@ -96,7 +105,11 @@ async function create(args: CreateArgs): Promise<void> {
}

logger.mainStep(
getMainStep({ packageManager: "bun", action: "create", project })
getMainStep({
packageManager: PACKAGE_MANAGER_DETAILS.name,
action: "create",
project,
})
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });
logger.rootHeader();
Expand Down Expand Up @@ -156,7 +169,11 @@ async function remove(args: RemoveArgs): Promise<void> {
const hasWorkspaces = project.workspaceData.globs.length > 0;

logger.mainStep(
getMainStep({ packageManager: "bun", action: "remove", project })
getMainStep({
packageManager: PACKAGE_MANAGER_DETAILS.name,
action: "remove",
project,
})
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });

Expand Down Expand Up @@ -219,11 +236,23 @@ async function clean(args: CleanArgs): Promise<void> {
async function convertLock(args: ConvertArgs): Promise<void> {
const { project, options } = args;

if (project.packageManager !== "bun") {
// remove the lockfile
if (!options?.dry) {
rmSync(project.paths.lockfile, { force: true });
}
// handle moving lockfile from `packageManager` to npm
switch (project.packageManager) {
case "pnpm":
// can't convert from pnpm to bun - just remove the lock
removeLockFile({ project, options });
break;
case "bun":
// we're already using bun, so we don't need to convert
break;
case "npm":
// can't convert from npm to bun - just remove the lock
removeLockFile({ project, options });
break;
case "yarn":
// can't convert from yarn to bun - just remove the lock
removeLockFile({ project, options });
break;
}
}

Expand Down
51 changes: 40 additions & 11 deletions packages/turbo-workspaces/src/managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
Project,
ConvertArgs,
ManagerHandler,
Manager,
} from "../types";
import {
getMainStep,
Expand All @@ -20,8 +21,14 @@ import {
getWorkspacePackageManager,
expandPaths,
parseWorkspacePackages,
removeLockFile,
} from "../utils";

const PACKAGE_MANAGER_DETAILS: Manager = {
name: "npm",
lock: "package-lock.json",
};

/**
* Check if a given project is using npm workspaces
* Verify by checking for the existence of:
Expand All @@ -30,11 +37,13 @@ import {
*/
// eslint-disable-next-line @typescript-eslint/require-await -- must match the detect type signature
async function detect(args: DetectArgs): Promise<boolean> {
const lockFile = path.join(args.workspaceRoot, "package-lock.json");
const lockFile = path.join(args.workspaceRoot, PACKAGE_MANAGER_DETAILS.lock);
const packageManager = getWorkspacePackageManager({
workspaceRoot: args.workspaceRoot,
});
return existsSync(lockFile) || packageManager === "npm";
return (
existsSync(lockFile) || packageManager === PACKAGE_MANAGER_DETAILS.name
);
}

/**
Expand All @@ -56,10 +65,10 @@ async function read(args: ReadArgs): Promise<Project> {
return {
name,
description,
packageManager: "npm",
packageManager: PACKAGE_MANAGER_DETAILS.name,
paths: expandPaths({
root: args.workspaceRoot,
lockFile: "package-lock.json",
lockFile: PACKAGE_MANAGER_DETAILS.lock,
}),
workspaceData: {
globs: workspaceGlobs,
Expand All @@ -85,7 +94,11 @@ async function create(args: CreateArgs): Promise<void> {
const hasWorkspaces = project.workspaceData.globs.length > 0;

logger.mainStep(
getMainStep({ packageManager: "npm", action: "create", project })
getMainStep({
packageManager: PACKAGE_MANAGER_DETAILS.name,
action: "create",
project,
})
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });
logger.rootHeader();
Expand Down Expand Up @@ -144,7 +157,11 @@ async function remove(args: RemoveArgs): Promise<void> {
const hasWorkspaces = project.workspaceData.globs.length > 0;

logger.mainStep(
getMainStep({ packageManager: "npm", action: "remove", project })
getMainStep({
packageManager: PACKAGE_MANAGER_DETAILS.name,
action: "remove",
project,
})
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });

Expand Down Expand Up @@ -207,11 +224,23 @@ async function clean(args: CleanArgs): Promise<void> {
async function convertLock(args: ConvertArgs): Promise<void> {
const { project, options } = args;

if (project.packageManager !== "npm") {
// remove the lockfile
if (!options?.dry) {
rmSync(project.paths.lockfile, { force: true });
}
// handle moving lockfile from `packageManager` to npm
switch (project.packageManager) {
case "pnpm":
// can't convert from pnpm to npm - just remove the lock
removeLockFile({ project, options });
break;
case "bun":
// can't convert from bun to npm - just remove the lock
removeLockFile({ project, options });
break;
case "npm":
// we're already using npm, so we don't need to convert
break;
case "yarn":
// can't convert from yarn to npm - just remove the lock
removeLockFile({ project, options });
break;
}
}

Expand Down
Loading

0 comments on commit 618f867

Please sign in to comment.