Is there a way to automatically increment the build number and have that build number available inside my components?
My client is asking me to include it somewhere so they can make sure they are seeing the latest build available (see as reference issue #1910 )
Thank you!
You can have a module that exports an id (e.g. module.exports = 1;). To ensure it's incremented on every build, you can regenerate this file before react-scripts:
// in package.json
"build": "node generate-build-id.js && react-scripts build"
where generate-build-id.js is your Node script that would read a file (e.g. src/buildId.js), parse a number out of it, increment that number, and rewrite src/buildId.js.
Maybe it would be easier to use a json file, as you can require that in your app too (and would make updating it a ton easier).
Yea.
In case anyone is interested, this is the script I ended up using as generate-build.js
var fs = require('fs');
console.log("Incrementing build number...");
fs.readFile('src/metadata.json',function(err,content){
if(err) throw err;
var metadata = JSON.parse(content);
metadata.build = metadata.build + 1;
fs.writeFile('src/metadata.json',JSON.stringify(metadata),function(err){
if(err) throw err;
console.log("Current build number: " + metadata.build);
})
});
Thanks @gaearon for the tip on where to run the node script and @Timer for the idea of using a JSON file.
Most helpful comment
In case anyone is interested, this is the script I ended up using as generate-build.js
Thanks @gaearon for the tip on where to run the node script and @Timer for the idea of using a JSON file.