Show date time of the latest publish on Expo

Viewed 221

I'm trying to get the date time when I publish the app as the app version so I don't have to update the version manually. I'm defining the information in the 'extra' object

app.config.js

export default ({ config }) => {
    config.extra = {
        buildDate: new Date(),
    };
    return config;
};

and getting it via expo-constants

App.js

import Constants from 'expo-constants';
...
<Text muted>Build date time: {moment.utc(Constants.manifest.extra.buildDate).format('DD/MM/YYYY HH:mm UTC')}</Text>
...

However, when I open the app the value changes - turns out it runs again every time and is not related to publishing the app.

How to get this properly configured?

1 Answers

What I ended up doing was the following

  1. I added a .scripts folder to the root of the project
  2. I added a timestamp.js script in the folder timestamp.js

const fs = require("fs");
const t = new Date();
const timestamp = 
    t.getFullYear() +
    ("0" + (t.getMonth() + 1)).slice(-2) +
    ("0" + t.getDate()).slice(-2);

const statement = `export const timestamp = "${timestamp}"`;

fs.writeFile("./src/util/VersionTimestamp.js", statement, (err) => {
  if (err) {
    console.error(err);
    return;
  }
  //file written successfully
});

  1. Added an empty file somewhere called VersionTimestamp.js
  2. Added an npm script "publish": "node ./.scripts/timestamp.js && expo publish"
  3. When you publish use yarn run publish ... instead of expo publish ...
  4. Then I would import the version timestamp and would display it wherever I want it in the app.

P.S.: Obviously update the path from the script to your path.

Related