问题
There is a very handy npm version command. Besides arguments like major
, minor
and patch
it accepts arguments like prerelease
, prepatch
, etc.
It says in the docs that the commands work in accordance with the semver.inc function.
These pre
commands I have a question about.
Say I'm currently at version v1.0.0
.
If I run npm version prerelease
it will bump version to v1.0.1-0
.
Is it possible to provide an extra agrument for a prerelease identifier according to https://github.com/npm/node-semver#prerelease-identifiers?
I wish something like npm version prerelease alpha
would bump version to v1.0.1-alpha.0
but that doesn't work.
回答1:
Starting with npm 6.4.0 you can use the --preid
option of npm version
like this:
$ npm version prerelease --preid=alpha
v0.1.1-alpha.0
$ npm version prerelease --preid=alpha
v0.1.1-alpha.1
$ npm version prerelease --preid=alpha
v0.1.1-alpha.2
回答2:
I have been looking at this recently to see if there were any updates on the matter... but it seems things are still the same.
No, there are no npm version prerelease identifiers supported by the npm version command. You can see the reasoning by the team here: https://github.com/npm/npm/pull/12936#issuecomment-248153743
semver (https://www.npmjs.com/package/semver) does support what you are trying to do, so what you can do is obtain the version with a command just like this:
semver <current version> -i prerelease --preid <prelease identifier>
for example:
semver 1.0.1 -i prerelease --preid alpha
will produce:
1.0.2-alpha.0
With that result you can pass it to npm version (say for example in a CI build), like this:
npm version <resulting version from semver command>
Another alternative is to use semantic-release (an independent project): https://github.com/semantic-release/semantic-release
That will automate semantic versioning based on commit messages, but I think it only works with github repo hosted modules, not sure about that.
回答3:
Like the other answer mentioned this is not supported by npm because of the reason mentioned in this comment
But you can achieve the same using semver package and npm scripts by
adding something like the following to the package.json
"scripts": {
"beta-version-patch": "npm version $(semver $npm_package_version -i prerelease --preid beta)",
"beta-version-minor": "npm version $(semver $npm_package_version -i preminor --preid beta)",
"beta-version-major": "npm version $(semver $npm_package_version -i premajor --preid beta)",
"rc-version": "npm version $(semver $npm_package_version -i prerelease --preid rc)",
"final-release": "npm version $(semver $npm_package_version -i)"
}
and run npm run beta-version-patch
To be more generic you can use the following:
"scripts": {
"semver": "npm version $(semver $npm_package_version -i $release --preid $preid)"
}
and run commands like:
release=prerelease preid=alpha npm run semver
release=prerelease preid=beta npm run semver
release=premajor preid=alpha npm run semver
来源:https://stackoverflow.com/questions/44567010/are-there-npm-version-prerelease-identifiers