Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] npm_version_specifier as the way to know how npm version command was called #8098

Open
ThePlenkov opened this issue Feb 11, 2025 · 4 comments

Comments

@ThePlenkov
Copy link

Context

I'd like to develop an extension for npm version command

In a script I'd like to know how the version was called, like patch, minor or any else

Problem

Unfortunately currently it is not possible to know what is a version specifier

Solution

We already have set of predefined variables, like npm_package_version, but it's not helping here to know what was the instruction.

So having a variable like npm_version_specifier would help a lot.

Thanks!

@milaninfy
Copy link
Contributor

@ThePlenkov You might want to check this https://docs.npmjs.com/cli/v11/using-npm/scripts#npm-version
Whenever version command is called there are some lifecycle scripts that executes with it and you can tap into it with some env vars that will be pre set like old and new version values to see if that fits your need

await runScript({
...opts,
pkg,
stdio: 'inherit',
event: 'preversion',
env: {
npm_old_version: current,
npm_new_version: newV,
},
})
}

@ThePlenkov
Copy link
Author

That's ok, but that's what I want to say is that I can only have exact versions, old and new. But not the specifier like patch or minor.

@milaninfy
Copy link
Contributor

you can derive/calculate this using something like semver package

~/workarea/test-repo $ cat test.js 
const semver = require('semver')

console.log(semver.diff('1.1.0', '1.1.1'))
console.log(semver.diff('1.0.0', '1.1.0'))
console.log(semver.diff('1.1.0', '2.0.0'))


~/workarea/test-repo $ node test.js
patch
minor
major

@ThePlenkov
Copy link
Author

Of course. But point is - we are already calling a command with the specifier. So would be nice to have this argument propagated as a variable. But surely thanks for the recommendation- that's a working code indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants