Git Product home page Git Product logo

Comments (4)

spion avatar spion commented on August 20, 2024 1

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.

spion avatar spion commented on August 20, 2024

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.

idan-at avatar idan-at commented on August 20, 2024

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.

idan-at avatar idan-at commented on August 20, 2024

I finally got it.

it works perfectly. Thanks.

closing :)

from wsrun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.