bun-workspaces 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@ A CLI and API to enhance your monorepo development with Bun's [native workspaces
10
10
 
11
11
  - Works right away, with no boilerplate required 🍔🍴
12
12
  - Get metadata about your monorepo 🤖
13
- - Run package.json scripts across workspaces 📋
13
+ - Orchestrate your workspaces' `package.json` scripts 📋
14
14
  - Run inline [Bun Shell](https://bun.com/docs/runtime/shell) scripts in workspaces 🐚
15
15
 
16
16
  This is a tool to help manage a Bun monorepo, offering features beyond what [Bun's --filter feature](https://bun.com/docs/pm/filter) can do. It can be used to get a variety of metadata about your project and run scripts across your workspaces with advanced control.
@@ -84,6 +84,7 @@ bw run "bun build" --inline # Run an inline command via the Bun shell
84
84
  bw run lint --parallel=false # Run in series
85
85
  bw run lint --parallel=2 # Run in parallel with a max of 2 concurrent scripts
86
86
  bw run lint --parallel=auto # Default, based on number of available logical CPUs
87
+ bw run lint --parallel=50% # Run in parallel with a max of 50% of the "auto" limit
87
88
 
88
89
  # Use the grouped output style (default when on a TTY)
89
90
  bw run my-script --output-style=grouped
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-workspaces",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "A monorepo management tool for Bun, with a CLI and API to enhance Bun's native workspaces.",
5
5
  "license": "MIT",
6
6
  "main": "src/index.mjs",
@@ -93,7 +93,6 @@ const createCli = ({ defaultCwd = process.cwd(), defaultMiddleware } = {}) => {
93
93
  const { project, projectError } = initializeWithGlobalOptions(
94
94
  program,
95
95
  args,
96
- defaultCwd,
97
96
  middleware,
98
97
  );
99
98
  middleware.findProject({
@@ -3,7 +3,6 @@ import type { CliMiddleware } from "../middleware";
3
3
  export declare const initializeWithGlobalOptions: (
4
4
  program: Command,
5
5
  args: string[],
6
- defaultCwd: string,
7
6
  middleware: CliMiddleware,
8
7
  ) => {
9
8
  project: import("../../internal/core").Simplify<{
@@ -19,6 +19,8 @@ import { getCliGlobalOptionConfig } from "./globalOptionsConfig.mjs"; // CONCATE
19
19
  const ERRORS = defineErrors(
20
20
  "WorkingDirectoryNotFound",
21
21
  "WorkingDirectoryNotADirectory",
22
+ "NoCwdAndWorkspaceRoot",
23
+ "ProjectRootNotFound",
22
24
  );
23
25
  const addGlobalOption = (program, optionName, defaultOverride) => {
24
26
  const { mainOption, shortOption, description, param, values, defaultValue } =
@@ -42,13 +44,54 @@ const addGlobalOption = (program, optionName, defaultOverride) => {
42
44
  );
43
45
  }
44
46
  };
45
- const getWorkingDirectoryFromArgs = (program, args, defaultCwd) => {
46
- addGlobalOption(program, "cwd", defaultCwd);
47
+ const getWorkingDirectoryFromArgs = (program, args) => {
48
+ addGlobalOption(program, "cwd");
49
+ addGlobalOption(program, "workspaceRoot");
47
50
  program.parseOptions(args);
48
- return program.opts().cwd;
51
+ const { cwd, workspaceRoot } = program.opts();
52
+ if (cwd && workspaceRoot) {
53
+ throw new ERRORS.NoCwdAndWorkspaceRoot(
54
+ `Cannot use both ${getCliGlobalOptionConfig("cwd").mainOption} (${getCliGlobalOptionConfig("cwd").shortOption}) and ${getCliGlobalOptionConfig("workspaceRoot").mainOption} (${getCliGlobalOptionConfig("workspaceRoot").shortOption}) options together`,
55
+ );
56
+ }
57
+ return {
58
+ cwdOption: cwd,
59
+ workspaceRootOption: workspaceRoot,
60
+ };
49
61
  };
50
- const defineGlobalOptions = (program, args, defaultCwd, middleware) => {
51
- const cwd = getWorkingDirectoryFromArgs(program, args, defaultCwd);
62
+ const findRootFromCwd = () => {
63
+ let currentDirectory = process.cwd();
64
+ while (true) {
65
+ const packageJsonPath = path.join(currentDirectory, "package.json");
66
+ if (fs.existsSync(packageJsonPath)) {
67
+ try {
68
+ const packageJsonContent = JSON.parse(
69
+ fs.readFileSync(packageJsonPath, "utf8"),
70
+ );
71
+ if (packageJsonContent.workspaces) {
72
+ return currentDirectory;
73
+ }
74
+ } catch {
75
+ continue;
76
+ }
77
+ }
78
+ const parentDirectory = path.dirname(currentDirectory);
79
+ if (parentDirectory === currentDirectory) {
80
+ break;
81
+ }
82
+ currentDirectory = parentDirectory;
83
+ }
84
+ throw new ERRORS.ProjectRootNotFound(
85
+ `${getCliGlobalOptionConfig("workspaceRoot").shortOption}|${getCliGlobalOptionConfig("workspaceRoot").mainOption} option: Project root not found from current working directory "${process.cwd()}"`,
86
+ );
87
+ };
88
+ const defineGlobalOptions = (program, args, middleware) => {
89
+ const { cwdOption, workspaceRootOption } = getWorkingDirectoryFromArgs(
90
+ program,
91
+ args,
92
+ );
93
+ const cwd =
94
+ cwdOption || (workspaceRootOption ? findRootFromCwd() : process.cwd());
52
95
  const exists = fs.existsSync(cwd);
53
96
  const isDirectory = exists ? fs.statSync(cwd).isDirectory() : false;
54
97
  middleware.processWorkingDirectory({
@@ -98,9 +141,9 @@ const applyGlobalOptions = (options) => {
98
141
  projectError: error,
99
142
  };
100
143
  };
101
- const initializeWithGlobalOptions = (program, args, defaultCwd, middleware) => {
144
+ const initializeWithGlobalOptions = (program, args, middleware) => {
102
145
  program.allowUnknownOption(true);
103
- const { cwd } = defineGlobalOptions(program, args, defaultCwd, middleware);
146
+ const { cwd } = defineGlobalOptions(program, args, middleware);
104
147
  program.parseOptions(args);
105
148
  program.allowUnknownOption(false);
106
149
  const options = program.opts();
@@ -3,6 +3,7 @@ export interface CliGlobalOptions {
3
3
  logLevel: LogLevelSetting;
4
4
  cwd: string;
5
5
  includeRoot: boolean;
6
+ workspaceRoot: boolean;
6
7
  }
7
8
  export interface CliGlobalOptionConfig {
8
9
  mainOption: string;
@@ -28,7 +29,7 @@ export declare const getCliGlobalOptionConfig: (
28
29
  readonly mainOption: "--cwd";
29
30
  readonly shortOption: "-d";
30
31
  readonly description: "Working directory";
31
- readonly defaultValue: ".";
32
+ readonly defaultValue: "";
32
33
  readonly values: null;
33
34
  readonly param: "path";
34
35
  }
@@ -39,5 +40,13 @@ export declare const getCliGlobalOptionConfig: (
39
40
  readonly defaultValue: "";
40
41
  readonly values: null;
41
42
  readonly param: "";
43
+ }
44
+ | {
45
+ readonly mainOption: "--workspace-root";
46
+ readonly shortOption: "-w";
47
+ readonly description: "Run from the project root above the current working directory";
48
+ readonly defaultValue: "";
49
+ readonly values: null;
50
+ readonly param: "";
42
51
  };
43
52
  export declare const getCliGlobalOptionNames: () => CliGlobalOptionName[];
@@ -14,7 +14,7 @@ const CLI_GLOBAL_OPTIONS_CONFIG = {
14
14
  mainOption: "--cwd",
15
15
  shortOption: "-d",
16
16
  description: "Working directory",
17
- defaultValue: ".",
17
+ defaultValue: "",
18
18
  values: null,
19
19
  param: "path",
20
20
  },
@@ -26,6 +26,15 @@ const CLI_GLOBAL_OPTIONS_CONFIG = {
26
26
  values: null,
27
27
  param: "",
28
28
  },
29
+ workspaceRoot: {
30
+ mainOption: "--workspace-root",
31
+ shortOption: "-w",
32
+ description:
33
+ "Run from the project root above the current working directory",
34
+ defaultValue: "",
35
+ values: null,
36
+ param: "",
37
+ },
29
38
  };
30
39
  const getCliGlobalOptionConfig = (optionName) =>
31
40
  CLI_GLOBAL_OPTIONS_CONFIG[optionName];
@@ -1,4 +1,4 @@
1
1
  export declare const IS_WINDOWS: boolean;
2
2
  export declare const IS_MACOS: boolean;
3
3
  export declare const IS_LINUX: boolean;
4
- export declare const IS_UNIX: boolean;
4
+ export declare const IS_POSIX: boolean;
@@ -2,6 +2,6 @@
2
2
  const IS_WINDOWS = process.platform === "win32";
3
3
  const IS_MACOS = process.platform === "darwin";
4
4
  const IS_LINUX = process.platform === "linux";
5
- const IS_UNIX = IS_MACOS || IS_LINUX;
5
+ const IS_POSIX = IS_MACOS || IS_LINUX;
6
6
 
7
- export { IS_LINUX, IS_MACOS, IS_UNIX, IS_WINDOWS };
7
+ export { IS_LINUX, IS_MACOS, IS_POSIX, IS_WINDOWS };