IT박스

package.json 버전을 자동으로 업데이트

itboxs 2020. 6. 13. 19:37
반응형

package.json 버전을 자동으로 업데이트


작은 릴리스를 수행하고 태그를 지정하기 전에 새 버전의 프로그램을 반영하도록 package.json을 업데이트하고 싶습니다.

파일을 package.json자동으로 편집하는 방법이 있습니까?

사용겠습니까 git pre-release hook도움을?


npm version아마 정답 일 것입니다. 대안을 제공하기 위해 grunt-bump 권장 합니다. 그것은 angular.js의 사람 중 하나에 의해 유지됩니다.

용법:

grunt bump
>> Version bumped to 0.0.2

grunt bump:patch
>> Version bumped to 0.0.3

grunt bump:minor
>> Version bumped to 0.1.0

grunt bump
>> Version bumped to 0.1.1

grunt bump:major
>> Version bumped to 1.0.0

어쨌든 grunt를 사용하는 경우 가장 간단한 해결책 일 수 있습니다.


정답

그렇게하려면 npm version patch=)

나의 옛 대답

pre-release원래 에는 후크 가 없습니다 git. 적어도 man githooks표시하지 않습니다.

당신이 사용하는 경우 git-extra( https://github.com/visionmedia/git-extras ), 예를 들어, 당신은 사용할 수 있습니다 pre-release당신은에서 볼 수 있듯이, 그것에 의해 구현되는 후크를 https://github.com/visionmedia/ git-extras / blob / master / bin / git-release . .git/hook/pre-release.sh파일을 편집 하는 실행 파일 만 필요 package.json합니다. 커밋, 푸시 및 태깅은 git release명령에 의해 수행됩니다 .

에 대한 확장명을 사용하지 않는 경우 git쉘 스크립트를 작성하고 (이름 git-release.sh을 지정할 것임) git release다음과 같이 별명을 지정할 수 있습니다 .

git config --global alias.release '!sh path/to/pre-release.sh $1'

git release 0.4를 실행할 것보다을 사용할 수 있습니다 path/to/pre-release.sh 0.4. 스크립트는 package.json태그를 편집 하여 서버로 푸시 할 수 있습니다.


이것이 일반적으로 프로젝트에서 수행하는 작업입니다.

npm version patch
git add *;
git commit -m "Commit message"
git push
npm publish

첫 번째 줄 npm version patch은에서 패치 버전을 1 (xx1 ~ xx2) 증가시킵니다 package.json. 그런 다음 package.json수정 된 시점을 포함하여 모든 파일을 추가합니다 . 그런 다음 평소 git commitgit push, 마지막 npm publish으로 모듈을 게시합니다.

이것이 의미가 있기를 바랍니다 ...

머서.


보다 최신의 접근 방식을 제공합니다.

package.json

  "scripts": {
    "eslint": "eslint index.js",
    "pretest": "npm install",
    "test": "npm run eslint",
    "preversion": "npm run test",
    "version": "",
    "postversion": "git push && git push --tags && npm publish"
  }

그런 다음 실행하십시오.

npm version minor --force -m "Some message to commit"

어느 것 :

  1. ... 테스트 실행 ...

  2. package.json다음 부 버전으로 변경하십시오 (예 : 1.8.1-1.9.0)

  3. 당신의 변화를 밀어

  4. 새로운 자식 태그 릴리스를 만들고

  5. npm 패키지를 게시하십시오.

--force누가 보스인지 보여줘야합니다! 농담 옆으로 https://github.com/npm/npm/issues/8620 참조


또한 버전 범프를 원하지만 태그 또는 새로운 커밋을 원하지 않는 경우 플래그를 npm version사용할 수 있습니다 --no-git-tag-version.

npm --no-git-tag-version version patch

https://docs.npmjs.com/cli/version


원사를 사용하는 경우 사용할 수 있습니다

yarn version --patch

This will increment package.json version by patch (0.0.x), commit, and tag it with format v0.0.0

Likewise you can bump minor or major version by using --minor or --major

When pushing to git ensure you also push the tags with --follow-tags

git push --follow-tags

You can also create a script for it

    "release-it": "yarn version --patch && git push --follow-tags"

Simply run it by typing yarn release-it


I am using husky and git-branch-is:

As of husky v1+:

// package.json
{
  "husky": {
    "hooks": {
      "post-merge": "(git-branch-is master && npm version minor || 
  (git-branch-is dev && npm --no-git-tag-version version patch)",
    }
  }
}

Prior to husky V1:

"scripts": {
  ...
  "postmerge": "(git-branch-is master && npm version minor || 
  (git-branch-is dev && npm --no-git-tag-version version patch)",
  ...
},

Read more about npm version

Webpack or Vue.js

If you are using webpack or Vue.js, you can display this in the UI using Auto inject version - Webpack plugin

NUXT

In nuxt.config.js:

var WebpackAutoInject = require('webpack-auto-inject-version');

module.exports = {
  build: {
    plugins: [
      new WebpackAutoInject({
        // options
        // example:
        components: {
          InjectAsComment: false
        },
      }),
    ]
  },
}

Inside your template for example in the footer:

<p> All rights reserved © 2018 [v[AIV]{version}[/AIV]]</p>

I want to add some clarity to the answers this question got.

Even thought there are some answers here that are tackling properly the problem and providing a solution, they are not the correct ones. The correct answer to this question is to use npm version

Is there a way to edit the file package.json automatically?

Yes, what you can do to make this happen is to run the npm version command when needed, you can read more about it here npm version, but the base usage would be npm version patch and it would add the 3rd digit order on your package.json version (1.0.X)

Would using a git pre-release hook help?

You could configure to run the npm version command on the pre-release hook, as you need, but that depends if that is what you need or not in your CD/CI pipe, but without the npm version command a git pre-release hook can't do anything "easily" with the package.json

The reason why npm version is the correct answer is the following:

  1. If the user is using a folder structure in which he has a package.json he is using npm if he is using npm he has access to the npm scripts.
  2. If he has access to npm scripts he has access to the npm version command.
  3. Using this command he doesn't need to install anything more in his computer or CD/CI pipe which on the long term will reduce the maintainability effort for the project, and will help with the setup

The other answers in which other tools are proposed are incorrect.

gulp-bump works but requires another extra package which could create issues in the long term (point 3 of my answer)

grunt-bump works but requires another extra package which could create issues in the long term (point 3 of my answer)


First, you need to understand the rules for upgrading the versioning number. You can read more about the semantic version here.

Each version will have x.y.z version where it defines for different purpose as shown below.

  1. x - major, up this when you have major changes and it is huge discrepancy of changes occurred.
  2. y - minor, up this when you have new functionality or enhancement occurred.
  3. z - patch, up this when you have bugs fixed or revert changes on earlier version.

To run the scripts, you can define it in your package.json.

"script": {
    "buildmajor": "npm version major && ng build --prod",
    "buildminor": "npm version minor && ng build --prod",
    "buildpatch": "npm version patch && ng build --prod"
}

In your terminal, you just need to npm run accordingly to your needs like

npm run buildpatch

If run it in git repo, the default git-tag-version is true and if you do not wish to do so, you can add below command into your scripts:

--no-git-tag-version

for eg: "npm --no-git-tag-version version major && ng build --prod"

참고URL : https://stackoverflow.com/questions/13059991/update-package-json-version-automatically

반응형