Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bun run --workspaces #5207

Closed
Tracked by #442 ...
colinhacks opened this issue Sep 13, 2023 · 8 comments · Fixed by #8185
Closed
Tracked by #442 ...

Support bun run --workspaces #5207

colinhacks opened this issue Sep 13, 2023 · 8 comments · Fixed by #8185
Labels
bun install Something that relates to the npm-compatible client enhancement New feature or request

Comments

@colinhacks
Copy link
Contributor

What is the problem this feature would solve?

Run a package.json script in all workspaces (if it is defined)

What is the feature you are proposing to solve the problem?


What alternatives have you considered?

No response

@colinhacks colinhacks added the enhancement New feature or request label Sep 13, 2023
@Alia5
Copy link

Alia5 commented Sep 15, 2023

pnpm has a --filter param for pnpm run

I suggest doing something similar (if not the same) for bun

@paulleonartcalvo
Copy link

Would love to see this for the sake of monorepo support

@Electroid Electroid added the bun install Something that relates to the npm-compatible client label Oct 27, 2023
@dangenendt
Copy link

dangenendt commented Oct 28, 2023

+

Without this, a monorepo is quite hard to manage when it comes to slim and performant docker images made from monorepo parts

@paulleonartcalvo
Copy link

paulleonartcalvo commented Oct 29, 2023

Seems like a nice QOL issue which could increase early adoption

@hyoretsu
Copy link

Bump, please add support for this, even if it's a simple cd in and cd out.

@SahilVasava
Copy link

Yes please. Much needed feature for monorepos.

@cyrsis
Copy link

cyrsis commented Jan 13, 2024

Yes Please , Great help to make everything works the same

@o-az
Copy link
Contributor

o-az commented Feb 22, 2024

You can create your own workspace filter while this is under development:

in package.json:

{
  "scripts": {
    "workspace": "bun ./scripts/workspace.ts"
  }
}

./scripts/workspace.ts

File content
// ./scripts/workspace.ts

import { file } from 'bun';
import { join } from 'node:path';

const [, , workspace, script] = process.argv;

const runScriptForWorkspace = (w: string) =>
  new Promise<number>((resolve, reject) => {
    console.info(`Running script "${script}" in workspace "${w}"`);
    Bun.spawn({
      cmd: ['bun', 'run', script],
      cwd: `./${w}`,
      stdio: ['inherit', 'inherit', 'inherit'],
      onExit(info) {
        if (info.exitCode !== 0) {
          console.log('The script failed', info.exitCode);
          reject(info.exitCode);
        } else {
          console.log('The script ran successfully');
          resolve(info.exitCode);
        }
      },
    });
  });

if (workspace.includes(',')) {
  const workspaces = workspace.split(',');
  for (const w of workspaces) {
    runScriptForWorkspace(w);
  }
} else if (workspace !== 'all' && !workspace.startsWith('-'))
  runScriptForWorkspace(workspace);
else {
  const excluded = workspace.startsWith('-') && workspace.slice(1);

  const packageJson = JSON.parse(
    await file(join(import.meta.dir, '../package.json')).text(),
  );

  const workspaces = packageJson.workspaces as string[];
  for (const w of workspaces) {
    if (w === excluded) continue;
    await runScriptForWorkspace(w);
  }
}

Then you can run workspaces with bun run workspace <workspace-script>.

Here's an example of a project using workspaces with bun:

https://github.com/rehype-pretty/rehype-pretty-code/blob/4a41af0c8fb706e26bdd00aec54b9bc384fc318a/package.json#L28
https://github.com/rehype-pretty/rehype-pretty-code/blob/master/scripts/workspace.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun install Something that relates to the npm-compatible client enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants