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

时间:2023-01-24 11:37:59

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. (mainFolder/subFolderA/package.json)

这些子文件夹都包含一个带有依赖项和devDependencies的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

谢谢

2 个解决方案

#1


5  

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));

#2


1  

Here are two options:

这有两个选择:

json-merge

JSON合并

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

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. 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.

通过标准输入传递的基本模板用于生成根package.json文件。这允许版本控制系统忽略package.json,这与自动生成的文件冲突。基本包属性(如名称和版本)可以存储在单独的文件中,例如package-base.json,并保存在版本控制中。

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

$ cd my_cool_project $ npm-deps package.json

#1


5  

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));

#2


1  

Here are two options:

这有两个选择:

json-merge

JSON合并

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

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. 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.

通过标准输入传递的基本模板用于生成根package.json文件。这允许版本控制系统忽略package.json,这与自动生成的文件冲突。基本包属性(如名称和版本)可以存储在单独的文件中,例如package-base.json,并保存在版本控制中。

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

$ cd my_cool_project $ npm-deps package.json