Created
April 13, 2021 12:40
-
-
Save sators/74f45c2d1843c9b9bdb9525548c3fe04 to your computer and use it in GitHub Desktop.
NPM Install Recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const fs = require('fs') | |
const child_process = require('child_process') | |
const root = process.cwd() | |
npm_install_recursive(root) | |
// Since this script is intended to be run as a "preinstall" command, | |
// it will do `npm install` automatically inside the root folder in the end. | |
console.log('===================================================================') | |
console.log(`Performing "npm install" inside root folder`) | |
console.log('===================================================================') | |
// Recurses into a folder | |
function npm_install_recursive(folder) { | |
const has_package_json = fs.existsSync(path.join(folder, 'package.json')) | |
// If there is `package.json` in this folder then perform `npm install`. | |
// | |
// Since this script is intended to be run as a "preinstall" command, | |
// skip the root folder, because it will be `npm install`ed in the end. | |
// Hence the `folder !== root` condition. | |
// | |
if (has_package_json && folder !== root) { | |
console.log('===================================================================') | |
console.log(`Performing "npm install" inside ${folder === root ? 'root folder' : './' + path.relative(root, folder)}`) | |
console.log('===================================================================') | |
npm_install(folder) | |
} | |
// Recurse into subfolders | |
for (let subfolder of subfolders(folder)) { | |
npm_install_recursive(subfolder) | |
} | |
} | |
// Performs `npm install` | |
function npm_install(where) { | |
child_process.execSync('npm install', { cwd: where, env: process.env, stdio: 'inherit' }) | |
} | |
// Lists subfolders in a folder | |
function subfolders(folder) { | |
return fs.readdirSync(folder) | |
.filter(subfolder => fs.statSync(path.join(folder, subfolder)).isDirectory()) | |
.filter(subfolder => subfolder !== 'node_modules' && subfolder[0] !== '.') | |
.map(subfolder => path.join(folder, subfolder)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "my-nested-npm-project", | |
"version": "1.0.0", | |
"description": "", | |
"scripts": { | |
"preinstall": "node npm-install-recursive.js", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment