Created
February 10, 2017 22:19
-
-
Save goldhand/7d94db602a65b50cf21b2863fe3b7613 to your computer and use it in GitHub Desktop.
Get a bunch of dependencies from multiple package.json formated for npm install command
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
| #!/usr/bin/env node | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const args = process.argv.slice(2); | |
| const targetDependencies = args.filter(arg => arg.includes('ependencies')).length | |
| ? args.filter(arg => arg.includes('ependencies')) | |
| : ['peerDependencies', 'devDependencies', 'dependencies']; | |
| const packageFiles = args.filter(arg => arg.includes('json')).length | |
| ? args.filter(arg => arg.includes('json')) | |
| : ['package.json']; | |
| const dependenciesObj = packageFiles | |
| .map(packageFile => JSON.parse(fs.readFileSync(path.resolve(process.cwd(), packageFile), 'utf8'))) | |
| .map(packageJSON => targetDependencies.map(dependencyType => packageJSON[dependencyType])) | |
| .reduce((a, b) => a.concat(b)) | |
| .filter(dependencyObj => dependencyObj) | |
| .reduce((a, cv) => (Object.assign(a, cv)), {}); | |
| const dependenciesStr = Object.keys(dependenciesObj) | |
| .map(dependency => `${dependency}@"${dependenciesObj[dependency]}"`) | |
| .join(' '); | |
| process.stdout.write(dependenciesStr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment