-
-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
NPM/Yarn-only way of versioning packages #232
Comments
I am having this issue with Create React App, I have tried using the Ideally we could inject the version number into |
@eberkund Have you tried the dotnet |
Hi @AArnott, I was trying to use the The issue I was having was that I could not find a way to assign an environment variable in my NPM script that would work on Windows. Something like this I found would work if you only use a *nix environment: "build": "VERSION=$(nbgv get-version -v Version) react-scripts build" But does not work on Windows. If an environment variable can be set this way then webpack will inject the value so that it will be accessible as |
Actually I went back and took another look at it and I think the ideal solution would be to create a package so that users could do something like this: {
"dependencies": {
"nbgv": "^1.0"
},
"scripts": {
"build": "nbgv-helper react-scripts build"
}
} "nbgv-helper" would wrap anything you pass it, similar to how cross-env works and be distributed as an NPM package. I got something partially working (by hard coding my particular use case) but my lack of experience creating node tools slowed me down. {
"scripts": {
"build": "node ./build.js"
}
} const child_process = require("child_process");
const version = child_process.execSync('nbgv get-version -v NpmPackageVersion');
process.env.REACT_APP_VERSION = version;
child_process.execSync('react-scripts start', {
stdio: 'inherit'
}); Any thoughts on this approach @AArnott? |
That's an interesting way to go. I've been looking for ways to create NPM packages that are stamped with the nbgv computed version that didn't require changing sources and it looks like your approach may work. But what is |
react-scripts provides wrappers for webpack that preconfigure it for working with React. It is the recommended scaffolding for creating new React projects. I know Vue.js is very similar with their wrapper scripts being called "vue-cli-service". Even if you are not using one of these tools I think it would work the same way if you were calling webpack directly from your NPM script.
The env var is important because it makes the version number accessible to from the app code. For example, I am using it to display the version number in the footer, so I can just reference The default create-react-app template has some scripts like this: {
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"start": "react-scripts start"
}
} Ideally the installation instructions would just be to prepend the nbgv-helper which would set the env var to the version number: {
"scripts": {
"build": "nbgv-helper react-scripts build",
"test": "nbgv-helper react-scripts test",
"start": "nbgv-helper react-scripts start"
}
} And then in your app code you would have access to the version through the |
It sounds like the nbgv-helper that you're describing wouldn't stamp the version anywhere, but it would allow the app's status bar to show the version. Is that right? |
Ah, you are correct. I hadn't thought of that as that wasn't a requirement for my scenario. Maybe instead of setting an env var, const child_process = require("child_process");
const version = child_process.execSync('nbgv get-version -v NpmPackageVersion');
// Sets the version in package.json
child_process.execSync(`npm version ${version}`); {
"scripts": {
"build": "nbgv-helper && react-scripts build",
}
} |
You have seen my nerdbank-gitversioning NPM package, which gets versions and stamps package.json, right? |
I saw that but I gave up on trying to use it because I wasn't using gulp or a similar tool. What if nerdbank-gitversioning exposed a CLI so that it could be called directly? And also this line: added the Then it could be used directly from NPM scripts. {
"scripts": {
"build": "nbgv set-version && react-scripts build"
}
} |
The
I didn't realize it failed in that case. Can you send a PR to fix that? |
What I'm doing current is I created a script called "scripts": {
"build": "node ./version.js && react-scripts build"
}
Sure. |
Bumps [Microsoft.SourceLink.GitHub](https://github.com/dotnet/sourcelink) from 1.1.1 to 8.0.0. - [Release notes](https://github.com/dotnet/sourcelink/releases) - [Commits](dotnet/sourcelink@1.1.1...8.0.0) --- updated-dependencies: - dependency-name: Microsoft.SourceLink.GitHub dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
In some factions of NPM development, gulp and other build orchestrators are being avoided in favor of simply using
yarn build
,yarn pack
, etc. This makes our gulp-based doc less helpful.How can NB.GV help versioning for these folks? Preferably
yarn pack
automatically builds a package with the right version, and without dirtying the source tree. Is that possible? If not, what's the closest we can get?The text was updated successfully, but these errors were encountered: