Skip to content

Instantly share code, notes, and snippets.

@alfonmga
Created December 1, 2018 19:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alfonmga/f09a050ae1df1c06a437adef2ccdf25d to your computer and use it in GitHub Desktop.
We use this to install all dependencies when we have a huge monorepo aka multiple directories with his own package.json
// @flow
const { readdirSync, statSync } = require('fs');
const { join } = require('path');
const { spawn } = require('child_process');
const getDirs = p =>
readdirSync(p).filter(f => statSync(join(p, f)).isDirectory());
// Get a list of all non-build directories in the root folder
const rootDirs = getDirs(join(__dirname, '..')).filter(
dir => dir.indexOf('build') === -1
);
// Filter them by directories that have a package.json
const workerDirs = rootDirs.filter(dir => {
let result = false;
readdirSync(dir).forEach(file => {
if (file === 'package.json') {
result = true;
}
});
return result;
});
const installDeps = (dir, callback) => {
const stream = spawn(
process.platform === 'win32' ? 'yarn.cmd' : 'yarn',
['install', '--no-progress', '--non-interactive'],
{ cwd: join(__dirname, '..', dir), stdio: 'inherit' }
);
stream.on('close', code => {
callback();
});
};
const installWorkerDeps = index => {
const dir = workerDirs[index];
if (!dir) return process.exit(0);
installDeps(dir, () => {
installWorkerDeps(index + 1);
});
};
// Install the dependencies in the root folder first
// then recursilvey install them for all the workers
installDeps('/', () => {
installWorkerDeps(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment