Comments (4)
You don't need to add a npm script to every individual module.
Instead, you can make a module containing tool scripts, and make sure that they are specified in "bin"
field of package.json
. That will make them automatically available without having script entries in individual package.jsons
You can also use existing commands like that. For example, yarn wsrun tsc
will run the typescript compiler command on every package, yarn wsrun mocha
will run the mocha command in every package. As long as the script is found in node_modules/.bin
, yarn will use it.
If you are not happy with yarn's behavior you can go a step further and use a different runner. To save on yarn memory usage we use a bash based runner in our monorepo:
#!/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=$(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"
and at the monorepo root we have a script alias called
"scripts":{
"wsrun-custom":"wsrun --bin=runner.sh"
}
In addition to
- package.json scripts (
yarn wsrun-custom build
), - locally installed binaries (
yarn wsrun-custom mocha
),
this runner also supports
- anything you have in your PATH (
yarn wsrun-custom git diff master package.json
) - even built in bash commands (e.g.
yarn wsrun-custom echo 'Hello world'
),
for ultimate flexibility
from wsrun.
Can you give a more specific example of what you would like to accomplish?
Our typical pattern is to have a "tool" package in the monorepo that has a few "bin" fields pointing to commonly used scripts. Then after a yarn install we're able to run, for example:
wsrun -c bin-script-to-update-config --key key --value value
note: you might need a yarn install --force
This will work even if the individual packages don't have a script - yarn falls back to trying to run the command.
from wsrun.
I'll try to give an example of what I mean.
with the current capabilities, I can run a command on all modules.
but let's say I want to do something a bit more complex:
Let's say I keep a list.json
file of all my shared dependencies (names and versions), and I wish to update a major version of one of my dependencies.
Instead of doing it manually (editing each package.json
), I want to run a script that does exactly that.
The problem is that this script doesn't know what modules I have in my workspace.
If I will be able to do something like this:
const fs = require("fs");
const wsrun = require("wsrun");
const allModules = require("./list.json");
wsrun.packages().forEach(pkg => {
const {name, path} = pkg;
console.log(`update ${name}`);
const packageJson = JSON.parse(fs.readFileSync(path));
packageJson.dependencies = { ...packageJson.dependencies, ...allModules };
fs.writeFileSync(path, JSON.stringify(packageJson), "utf-8");
})
It will be useful for those complex scenarios.
just to clarify, this is possible today by passing arguments to a script, but then I need to add an npm script inside each and every one of my modules, which kind of misses the point IMO.
from wsrun.
I finally got it.
it works perfectly. Thanks.
closing :)
from wsrun.
Related Issues (20)
- wsrun --bin spawns incorrectly on non-win32 platforms when command includes parameters
- Bug: wsrun does not exit if a spawed command is killed with OOM
- Bug: filter-changed-packages wrong result
- Compatibility with npm 7? HOT 1
- -x doesn't work with globs HOT 1
- Report output is interlaced with package outputs if a command fails with `collect-logs` flag HOT 1
- Run wsrun command from node script
- Warning for command matching no packages?
- collect-logs with fast-exit can hide logs HOT 1
- Install instructions? HOT 2
- Handling input for multiple projects
- Build only if package has changed HOT 1
- Release wsrun? HOT 3
- Security warning for Mem via the Yargs dependency
- ca in nmprc -> TypeError: Cannot create property '' on string HOT 3
- Excessive error messages when using `--if` argument. HOT 2
- Error messages from a failing submodule are hidden HOT 1
- The path rewriter is missing paths HOT 1
- Feature: run a certain command if any step fails HOT 1
- Feature: --checkSum <checkSumFile> for storing information about package was changed from last command run, and do not run command again if the changes not affect package. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wsrun.