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:
committed by
Facebook Github Bot
parent
aad970defd
commit
e97dddfda6
13
src/utils/__tests__/packageMetadata.node.js
Normal file
13
src/utils/__tests__/packageMetadata.node.js
Normal 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();
|
||||
});
|
||||
29
src/utils/packageMetadata.js
Normal file
29
src/utils/packageMetadata.js
Normal 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;
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user