diff --git a/src/utils/__tests__/packageMetadata.node.js b/src/utils/__tests__/packageMetadata.node.js new file mode 100644 index 000000000..205b52e8c --- /dev/null +++ b/src/utils/__tests__/packageMetadata.node.js @@ -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(); +}); diff --git a/src/utils/packageMetadata.js b/src/utils/packageMetadata.js new file mode 100644 index 000000000..5803a5083 --- /dev/null +++ b/src/utils/packageMetadata.js @@ -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 = lodash.memoize( + async () => { + const json = await getPackageJSON(); + return json.revision; + }, +);