1

When using yarn workspaces is it possible to have nested package.json files to resolve dependancies for a subset of packages? Avoiding repetition?

where cra are create-react-apps and pkg are packages that these sites consume. e.g.

| /
| - package.json
|---- /sites
|-------- /cra-1
|         - package.json
|-------- /cra-2
|         - package.json
|---- /packages
|     - package.json   // <<---- Extra level here
|-------- /pkg-1
|         - package.json
|-------- /pkg-1
|         - package.json
|-------- /pkg-2
|         - package.json

this would allow us to share common dependancies for our packages... keep them all in sync and up-to-date

tk421
  • 5,557
  • 6
  • 23
  • 33
AndrewMcLagan
  • 12,718
  • 21
  • 82
  • 153

1 Answers1

0

This could work with a bash script as well, but try this one out:

var fs = require('fs')
var join = require('path').join
var os = require('os')
var cp = require('child_process')
var resolve = require('path').resolve

// library path
var lib = resolve(__dirname, '../lib/')

fs.readdirSync(lib)
  .forEach(function (mod) {
    var modPath = join(lib, mod)
// ensure path has package.json
if (!fs.existsSync(join(modPath, 'package.json'))) return

// npm binary based on OS
var npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm'

// install folder
cp.spawn(npmCmd, ['i'], { env: process.env, cwd: modPath, stdio: 'inherit' })
})

You can run a script via npm and main package.json in your root directory. The script will visit every subdirectory and perform npm install.

Aditya Prakash
  • 1,367
  • 2
  • 12
  • 31