[英]Use enviroment variables in yarn package.json
I want to pull from a private package hosted on bitbucket. 我想从bitbucket上托管的私有包中提取。 Since SSH is not an option for my deploy setup, I want to access the repo using the Application Password.
由于SSH不是我的部署设置的选项,我想使用应用程序密码访问存储库。
So my entry in the package JSON looks like this: 所以我在包JSON中的条目如下所示:
"dependencies": {
"@companyName/repository": "git+https://${$BITBUCKET_USER}:${BITBUCKET_APP_PASSWORD}@bitbucket.org/company name/repository.git",
Coding username and password hard into the repo URL works fine but when I perform yarn install
as above, the environment variables are not replaced by its values. 将用户名和密码编码到repo URL中工作正常但是当我按上述方式执行
yarn install
,环境变量不会被其值替换。
Is there any way to use environment variables like this? 有没有办法像这样使用环境变量?
You can write a preinstall
hook that updates package.json
with values from the environment. 您可以编写一个
preinstall
挂钩,使用环境中的值更新package.json
。 Luckily the order of lifecycle hooks work as prescribed using yarn
. 幸运的是,生命周期钩子的顺序按照规定使用
yarn
。
{
"name": "njs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preinstall": "node preinstall.js"
},
"dependencies": {
"@companyName/repository": "git+https://${$BITBUCKET_USER}:${BITBUCKET_APP_PASSWORD}@bitbucket.org/companyName/repository.git"
},
"author": "",
"license": "ISC"
}
preinstall.js example: preinstall.js示例:
const package = require('./package.json');
const fs = require('fs');
const {BITBUCKET_USER = 'test', BITBUCKET_APP_PASSWORD='test'} = process.env;
package.dependencies["@companyName/repository"] = package.dependencies["@companyName/repository"]
.replace("${$BITBUCKET_USER}", BITBUCKET_USER)
.replace("${BITBUCKET_APP_PASSWORD}", BITBUCKET_APP_PASSWORD);
fs.writeFileSync('package.json', JSON.stringify(package, null, 4));
Bonus: 奖金:
How you choose to replace environment variables in preinstall.js
is left to your good judgment. 你如何选择在
preinstall.js
替换环境变量,这preinstall.js
你的判断。 Yes, you can totally use ES6 template tags. 是的,您可以完全使用ES6模板标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.