Add readCurrentRevision helper

Summary: To read the current revision written into the manifest at build-time.

Reviewed By: danielbuechele

Differential Revision: D14454983

fbshipit-source-id: adad7d85dbf410701d2f8601bfccbcfbc0f30dff
This commit is contained in:
Pascal Hartig
2019-03-14 04:44:52 -07:00
committed by Facebook Github Bot
parent aad970defd
commit e97dddfda6
2 changed files with 42 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {readCurrentRevision} from '../packageMetadata.js';
test('readCurrentRevision does not return something meaningful in dev mode', async () => {
const ret = await readCurrentRevision();
expect(ret).toBeUndefined();
});

View File

@@ -0,0 +1,29 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import electron from 'electron';
import lodash from 'lodash';
import isProduction from './isProduction';
import path from 'path';
import fs from 'fs';
import {promisify} from 'util';
const getPackageJSON = async () => {
const base =
isProduction() && electron.remote
? electron.remote.app.getAppPath()
: process.cwd();
const content = await promisify(fs.readFile)(path.join(base, 'package.json'));
return JSON.parse(content);
};
export const readCurrentRevision: () => Promise<?string> = lodash.memoize(
async () => {
const json = await getPackageJSON();
return json.revision;
},
);