简体   繁体   English

如何将多个(npm)package.json文件合并到一个Gulp?

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

Lets assume I have a mainFolder and 3 subfolders (subFolderA, subfolderB, subfolderC). 让我们假设我有一个mainFolder和3个子文件夹(subFolderA,subfolderB,subfolderC)。

And these subfolders all contain a package.json with dependencies and devDependencies. 这些子文件夹都包含一个带有依赖项和devDependencies的package.json。 (mainFolder/subFolderA/package.json) (mainFolder / subFolderA /的package.json)

I want to combine them into a single package.json in mainFolder (mainFolder/package.json) 我想在mainFolder(mainFolder / package.json)中将它们组合成一个package.json

Is there an existing gulp package (or any other solution outside Gulp) to combine and merge package.json dependencies and devDependencies? 是否存在一个现有的gulp包(或Gulp之外的任何其他解决方案)来组合和合并package.json依赖项和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. 你可以发布一个例子gulpfile.js或解释其他方式/工具。

Thank you 谢谢

You could try using this node package to merge package.json files: https://www.npmjs.com/package/package-merge 您可以尝试使用此节点包来合并package.json文件: https//www.npmjs.com/package/package-merge

Edit following comment by @dur 编辑@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合并

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. npm-deps扫描子目录中的嵌套package.json文件,并将所有依赖项合并到一个输出到stdout的package.json文件中。

A base template passed through standard input is used to produce the root package.json file. 通过标准输入传递的基本模板用于生成根package.json文件。 This allows package.json to be ignored by version control systems, which conflict with auto-generated files. 这允许版本控制系统忽略package.json,这与自动生成的文件冲突。 Base package attributes, like name and version, can be stored in a separate file such as package-base.json, and kept in version control. 基本包属性(如名称和版本)可以存储在单独的文件中,例如package-base.json,并保存在版本控制中。

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

You can try package-json-merge npm module. 您可以尝试package-json-merge npm模块。 To install it, use this command - 要安装它,请使用此命令 -

npm install -g package-json-merge

Then simply use below command to combine multiple package.json into a single one - 然后只需使用下面的命令将多个package.json组合成一个 -

package-json-merge package1.json package2.json .... packageN.json > package.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM