情况
lerna
我有一个包含 40-50 个项目的 monorepo 。每个都有package.json
这样的。
{
"name": "@base-repo/add-class-methods",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
文件夹结构是这样的,
packages
├── absolute-url
│ ├── index.js
│ └── package.json
├── add-class-methods
│ ├── index.js
│ └── package.json
├── check-set-relative
│ ├── index.js
│ └── package.json
├── crypto
│ ├── index.js
│ └── package.json
如果我将它推送到 github,它将只有一个 github url,但是我看到babel
有 142 个包,其中每个包repository
在package.json
.
"repository": "https://github.com/babel/babel/tree/master/packages/babel-types"
我希望他们不会为 142 个包手动设置此值。我的 40 个小包裹也一样。
我知道在我写这个问题时,我可以在 3-4 分钟内手动设置它们。但是,当我尝试对 150 包 monorepo 或将来做同样的事情时,这将变得不堪重负。
问题
如何在不package.json
手动打开 40 个包的每个文件的情况下设置/更新存储库字段?
我试过的
尽可能手动设置每个,但考虑到我是一名程序员,事情很快就变得无聊和重复。然后我用谷歌搜索了大约一个小时的解决方案。最后我写了以下脚本,
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const gitUrl = 'https://github.com/user';
const author = `Mr. Github User <user@example.com> (${gitUrl})`;
const basePath = '/utility-scripts/tree/master';
const baseRepo = gitUrl + basePath;
glob('packages/*/package.json', (err, files) => {
for (const filePath of files) {
const [parent, pkg] = filePath.split('/');
const newData = {
author,
license: 'MIT',
repository: `${baseRepo}/${parent}/${pkg}`,
};
const data = Object.assign(
{},
JSON.parse(fs.readFileSync(path.resolve(filePath), 'utf-8')),
newData,
);
fs.writeFileSync(path.resolve(filePath), JSON.stringify(data, true, 2));
}
});
有没有简单的方法来处理这个问题?使用任何类型的,shell
或命令?git
yarn
npm