Run arbitrary commands?
Closed this issue · 4 comments
I'd appreciate a way to run an arbitrary shell command via wsrun, for example:
wsrun -p @my/package --raw "ls -la"
Hopefully I didn't miss a way to do this currently, neither of these worked for me:
wsrun -p @my/package --bin "ls -la"
wsrun -p @my/package --bin ls -c "-la"
(The second is weird anyway.)
The default runner is yarn
which conforms to the rules described here: https://yarnpkg.com/lang/en/docs/cli/run/ - they do allow arbitrary commands just not with system binaries.
I think this is a good default, because broadly speaking the only binaries you should rely on are those distributed with the installed packages. ls
for example is not present on Windows systems
For more constrained scenarios, feel free to write your own runner, e.g.
#!/bin/bash
export PATH=$PWD/node_modules/.bin:$PATH
VERBOSE='1'
if [ "$1" == '-s' ]; then VERBOSE=''; shift; fi
if [ "$1" == 'run' ]; then shift; fi
# RUNCMD=$(jq -r .scripts.$1 package.json)
RUNCMD=$(node -e "console.log(JSON.parse(fs.readFileSync('package.json', 'utf8')).scripts['$1'])")
if [ "xundefined" = "x$RUNCMD" ]
then
RUNCMD=$1
fi
BASHCMD="$RUNCMD ${@:2}"
if [ $VERBOSE ]; then echo '$' $BASHCMD; fi;
exec bash -c "$BASHCMD"
My use case is this:
From the monorepo root, I can do something like this:
wsrun -p @my/package -c build
Now let's say I'd like to explore whether my build script correctly built the dist
folder and it would be great if I could just press ↑
, delete the end of the previous command and type ls dist
, like this:
wsrun -p @my/package --raw ls dist
I'd find it much more convenient than opening a new shell, cd
ing into the correct folder etc.
In the monorepo package.json you can put a script:
{
"scripts": {
"wsrun-raw":"wsrun --bin runner.sh"
}
}
then use yarn wsrun-raw -p @my/package -c ls dist
The raw bash runner written above works with:
- package commands
- local npm binaries
- system binaries
- any command understood by bash
(but it doesn't work on Windows - so I recommend against using it in libraries you intend for wider consumption and OSS development)
Thanks for the example, it would be great if wsrun could do this natively.