简体   繁体   English

package package.json 应用程序不同分支中的不同节点 package 版本?

[英]Different node package versions in different branches of app in package.json?

The scenario: We have several similar but customized Node applications, all of which include our own common library/package which we include as a dependency in package.json , all on BitBucket, all under development.场景:我们有几个相似但自定义的 Node 应用程序,所有这些应用程序都包含我们自己的公共库/包,我们将其作为依赖项包含在package.json中,所有这些都在 BitBucket 上,都在开发中。

Each application has 3 branches, 'develop', 'staging', 'production'每个应用程序有 3 个分支,'develop', 'staging', 'production'

The library also has 3 branches, 'develop', 'staging' & 'production'该库还有 3 个分支,“开发”、“暂存”和“生产”

We want to use the 'develop' branch of the lib for develop version of our apps, same for staging & production.我们想使用 lib 的“开发”分支来开发我们应用程序的版本,同样用于暂存和生产。

We can do that in package.json with:我们可以在 package.json 中使用:

ourapp dev branch package.json我们的应用开发分支 package.json

"dependencies": {
    "ourlib": "git+https://ouruser@bitbucket.org/our-org/ourlib#develop",
    "ts-jest": "^27.1.2",
    ...etc.
}

ourapp staging branch package.json:我们的应用程序暂存分支 package.json:

"dependencies": {
    "ourlib": "git+https://ouruser@bitbucket.org/our-org/ourlib#staging",
    "ts-jest": "^27.1.2",
    ...etc.
}

The issue/concern is promoting/merging from dev->staging->prod - to make it foolproof.问题/关注点是从 dev->staging->prod 促进/合并 - 以使其万无一失。 When we are satisfied with ourapp dev branch, we merge it into staging & deploy.当我们对我们的应用程序开发分支感到满意时,我们将其合并到暂存和部署中。

But of course then after the merge, the ourapp staging package.json will then get over-written with the dev package.json :但是当然在合并之后,ourapp staging package.json将被dev package.json 覆盖

"dependencies": {
    "ourlib": "git+https://ouruser@bitbucket.org/our-org/ourlib#develop",
    "ts-jest": "^27.1.2",
    ...etc.
}

...instead of the staging branch of ourlib - one solution could be to edit the staging ourapp package.json immediately after the merge to restore the staging branch for the lib - but that's way too clumsy and error prone. ...而不是 ourlib 的暂存分支 - 一种解决方案可能是在合并后立即编辑暂存 ourapp package.json 以恢复 lib 的暂存分支 - 但这太笨拙且容易出错。

I would imagine this must be a common scenario, but none of us have that level of NPM expertise.我想这一定是一个常见的场景,但我们都没有 NPM 的专业知识水平。

I'm wondering if there is any way to do it with environment variables in.npmrc or somewhere, or anything else - so something like:我想知道是否有任何方法可以使用 .npmrc 或某处的环境变量或其他任何方式来做到这一点 - 比如:

"dependencies": {
    "ourlib": "git+https://ouruser@bitbucket.org/our-org/ourlib#$NODE_ENV"
    or
    "ourlib": "git+https://ouruser@bitbucket.org/our-org/ourlib#${$npm_package_env}"
    or???
}

Or is there any way to include another package.json within the main - which would be in.gitignore, like:或者有没有办法在 main 中包含另一个 package.json - 这将是 in.gitignore,例如:

{
  "include" : "env.package.json",
  ...
}
  • Or is there some other, obvious solution/approach/technique I'm missing?还是我缺少其他一些明显的解决方案/方法/技术?

Thanks for any tips...感谢您的任何提示...

I finally figured out how to do what I wanted to do - install different versions of a library in different branches of an application, without over-writing the branch specific versions when merging (thanks to assembling several different threads - thanks to all.).我终于想出了如何做我想做的事——在应用程序的不同分支中安装不同版本的库,而不会在合并时覆盖分支特定版本(感谢组装几个不同的线程——感谢所有人。)。

It's a combination of.git & npm / package.json -它是 .git 和 npm / package.json 的组合 -

In package.json, under dependencies, instead of the library or package or repo, use a file dependency:在 package.json 中,在 dependencies 下,而不是 library 或 package 或 repo,使用一个文件依赖:

"versionlib": "file:./versionlib",

Create the subdirectory./versionlib, and in there create a package.json with the particular branch/version of the library you want installed in your application branch:创建子目录./versionlib,并在其中创建一个 package.json,其中包含您要在应用程序分支中安装的库的特定分支/版本:

{
  "name": "develop-lib-branch",
  "version": "0.0.0",
  "description": "Installs the develop branch of Library",
  "dependencies": {
    "mylibrary": "git+https://<credentials>@bitbucket.org/<repo>/mylibrary.git#develop"
    }
}

GIT: First, configure git (locally or globally) to enable a merge driver: GIT:首先,配置git(本地或全局)以启用合并驱动程序:

git config merge.ours.driver true

Then create a.gitattributes file with:然后创建一个 .gitattributes 文件:

mylibrary/package.json merge=ours

Create the mylibrary/package.json for each of the branches of your app that you want to load different library versions -- in production,为您要加载不同库版本的应用程序的每个分支创建 mylibrary/package.json——在生产中,

{
  "name": "prod-lib-branch",
  "version": "0.0.0",
  "description": "Installs the prod branch of Library",
  "dependencies": {
    "mylibrary": "git+https://<credentials>@bitbucket.org/<repo>/mylibrary.git#prod"
    }
}

Then you can merge your app's develop branch to prod branch, but still install the prod branch of the library when you npm install on prod.然后你可以将你的应用程序的开发分支合并到生产分支,但是当你在生产上安装 npm 时,仍然安装库的生产分支。

And then when you are ready, you can merge the develop branch of your library to the prod branch of the library, and when you install your app on prod, you will install the prod branch of the library, not the develop.然后当你准备好时,你可以将你的库的 develop 分支合并到库的 prod 分支,当你在 prod 上安装你的应用程序时,你将安装库的 prod 分支,而不是 develop。

I have no idea why I keep coming up with use-cases/questions that seem very obvious/important to me, but apparently of interest to almost no-one else - but just in case anyone else is searching for a similar solution, I thought I would share mine.我不知道为什么我不断提出对我来说非常明显/重要的用例/问题,但显然几乎没有其他人感兴趣 - 但以防万一其他人正在寻找类似的解决方案,我想我会分享我的。

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

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