How to merge multiple (npm) package.json files into one with Gulp?

Lets assume I have a mainFolder and 3 subfolders (subFolderA, subfolderB, subfolderC).

And these subfolders all contain a package.json with dependencies and devDependencies. (mainFolder/subFolderA/package.json)

I want to combine them into a single package.json in mainFolder (mainFolder/package.json)

Is there an existing gulp package (or any other solution outside Gulp) to combine and merge package.json dependencies and devDependencies?

(Let's assume there will be no version conflict. If there is a solution also covers that case, thats great!)

Can you post an example gulpfile.js or explain other ways/tools.

Thank you


You could try using this node package to merge package.json files: https://www.npmjs.com/package/package-merge

Edit following comment by @dur

Example:

var merge = require('package-merge')
var fs = require('fs');

var dst = fs.readFileSync('package.a.json');
var src = fs.readFileSync('package.b.json');
fs.writeFile("/tmp/package.merged.json", merge(dst,src));

Here are two options:

json-merge

json-merge package.json --parse="dependencies" package2.json --parse="devDependencies"

npm-deps scans subdirectories for nested package.json files, and merges all the dependencies together in a single package.json file that is outputted to stdout.

A base template passed through standard input is used to produce the root package.json file. This allows package.json to be ignored by version control systems, which conflict with auto-generated files. Base package attributes, like name and version, can be stored in a separate file such as package-base.json, and kept in version control.

$ cd my_cool_project $ npm-deps < package-base.json > package.json

链接地址: http://www.djcxy.com/p/92082.html

上一篇: k查询位串

下一篇: 如何将多个(npm)package.json文件合并为一个Gulp文件?