在我做一个小的发布和标记它之前,我想更新这个包。Json来反映程序的新版本。

是否有编辑文件包的方法。json自动吗?

使用git预发布钩子会有帮助吗?


当前回答

NPM版本可能是正确的答案。为了给大家一个替代的选择,我建议你用咕噜撞。它是由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,这可能是最简单的解决方案。

其他回答

提供一个最新的方法。

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"

将:

... 进行测试… 更换你的包裹。Json到下一个小版本(例如:1.8.1到1.9.0) 推动您的更改 创建一个新的git标签版本和 发布你的NPM包。

——武力就是要显示谁是老大!撇开笑话不谈,见https://github.com/npm/npm/issues/8620

作为npm版本的一个补充,如果你想要一个版本碰撞但没有标记或一个新的提交,你可以使用——no-git-tag-version标记:

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

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

首先,您需要了解升级版本号的规则。你可以在这里阅读更多关于语义版本的信息。

每个版本都有x.y.z版本,它为不同的目的定义,如下所示。

X大调,当你有很大的变化时 发生了不一致的变化。 Y小调,当你有 出现了新的功能或增强。 Z - patch, up this when 您修复了错误或恢复了早期版本上的更改。

要运行脚本,可以在package.json中定义它。

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

在你的终端中,你只需要根据你的需要运行npm

npm run buildpatch

如果在git repo中运行它,默认的git-tag-version是true,如果你不希望这样做,你可以在你的脚本中添加以下命令:

--no-git-tag-version

例如:"npm -no-git-tag-version version major && ng build -prod"

我的构建和发布脚本运行在一个无法访问git的docker映像中,所以我想要一些不修改我的存储库,但能够发布自动递增版本的方法。所以我在预发布脚本中添加了这个:

npm view `sed -nr 's/"name": "([^"]+).*/\1/p' package.json` version | awk -F'.' '{ cmd = "npm version v" $1 "." $2 "." $3+1 " --force --no-git-tag-version"; system(cmd)}'

它的作用:

用sed读取包名 要求最新发布的版本与NPM查看版本 调用NPM version——force——no-git-tag-version,其中version取自(2)并加1

有很多方法可以改进它,但至少它是有效的,而且是普遍的。

以防万一,如果你想用npm package semver链接来做这件事

let fs = require('fs');
let semver = require('semver');

if (fs.existsSync('./package.json')) {
    var package = require('./package.json');
    let currentVersion = package.version;
    let type = process.argv[2];
    if (!['major', 'minor', 'patch'].includes(type)) {
        type = 'patch';
    }

    let newVersion = semver.inc(package.version, type);
    package.version = newVersion;
    fs.writeFileSync('./package.json', JSON.stringify(package, null, 2));

    console.log('Version updated', currentVersion, '=>', newVersion);
}

包中。Json应该是这样的,

{
  "name": "versioning",
  "version": "0.0.0",
  "description": "Update version in package.json using npm script",
  "main": "version.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "version": "node version.js"
  },
  "author": "Bhadresh Arya",
  "license": "ISC",
  "dependencies": {
    "semver": "^7.3.2"
  }
}

只需要将major, minor, patch参数传递给NPM运行版本。默认为补丁。

例子: NPM运行版本或NPM运行版本补丁或NPM运行版本小或NPM运行版本大

去回购