bun icon indicating copy to clipboard operation
bun copied to clipboard

`bun shell` command for quick, cross platform `package.json` scripts

Open mangs opened this issue 11 months ago • 2 comments

What is the problem this feature would solve?

Having access to the Bun shell would be really nice to have when writing out package.json scripts, especially when considering its cross-platform benefits.

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

bun shell <command>

Extracting values, response codes, etc from template strings can potentially be ignored in this use case. Looking over the Bun shell examples, there are quite a few that are simple strings that would fit this use case.

Some examples from the documentation that would work:

bun shell 'echo bun! > greeting.txt'
bun shell 'bun run index.ts 2> errors.txt'
bun shell 'echo "Hello World!" | wc -w'

Some other examples that may be useful

bun shell 'rm -rf dist/*' # clean the build directory
bun shell 'cd node_modules && du -sh -- * | sort -h' # audit node_modules packages sizes
bun shell "cd dist && rm -f index.zip && zip index.zip *.{map,mjs} && echo -n 'Zip file size: ' && stat --format=%s index.zip | numfmt --to=iec" # zip a lambda then report its zipped size

What alternatives have you considered?

Populating a scripts/ directory with Bun shell code, but that requires more setup than a simple inline script

mangs avatar Mar 07 '24 17:03 mangs

I discovered that the bun exec command does this already, but there may be confusion between bun run and bun exec for some people due to the similarities in colloquial use of terms exec and run; they're often used interchangeably. This suggestion would make the intent more clear in my opinion.

mangs avatar Apr 01 '24 19:04 mangs

This took entirely too long to figure out. Well, once I came across this post only a few minutes. Here's an example that works:

package.json

{
  "scripts": {
    "clean": "bun exec 'rm -rf ./dist'"
  }
}

then in your terminal

bun run clean

xerullian avatar Apr 07 '24 22:04 xerullian