Create-react-app: Is there a way to automatically increment the build number?

Created on 31 Mar 2017  路  4Comments  路  Source: facebook/create-react-app

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!

Most helpful comment

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JimmyLv picture JimmyLv  路  3Comments

alleroux picture alleroux  路  3Comments

adrice727 picture adrice727  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

Aranir picture Aranir  路  3Comments