From 1d04fc3e344c1859acc964bfdc73298e723ca11b Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Fri, 13 Mar 2020 04:36:57 -0700 Subject: [PATCH] extract utility for determing static path [2/n] Summary: Make the logic to find the static file path reusable Reviewed By: passy Differential Revision: D20219731 fbshipit-source-id: a4f1fa021ea958681a078cb103621322bbfc1e48 --- scripts/build-release.ts | 4 ++-- src/utils/icons.ts | 28 ++++++++++++++++------------ src/utils/pathUtils.tsx | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/utils/pathUtils.tsx diff --git a/scripts/build-release.ts b/scripts/build-release.ts index b170452de..580fe0f16 100755 --- a/scripts/build-release.ts +++ b/scripts/build-release.ts @@ -21,7 +21,7 @@ import { genMercurialRevision, } from './build-utils'; import fetch from 'node-fetch'; -import {ICONS, buildLocalIconPath, getIconURL} from '../src/utils/icons'; +import {getIcons, buildLocalIconPath, getIconURL} from '../src/utils/icons'; function generateManifest(versionNumber: string) { const filePath = path.join(__dirname, '..', 'dist'); @@ -129,7 +129,7 @@ function copyStaticFolder(buildFolder: string) { } function downloadIcons(buildFolder: string) { - const iconURLs = Object.entries(ICONS).reduce< + const iconURLs = Object.entries(getIcons()).reduce< { name: string; size: number; diff --git a/src/utils/icons.ts b/src/utils/icons.ts index 2e2b3998e..1734455ab 100644 --- a/src/utils/icons.ts +++ b/src/utils/icons.ts @@ -10,23 +10,27 @@ import fs from 'fs'; import path from 'path'; import {remote} from 'electron'; +import {getStaticPath} from './pathUtils'; const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32]; const DENSITIES = [1, 1.5, 2, 3, 4]; -const staticPath = path.resolve(__dirname, '..', '..', 'static'); -const appPath = remote ? remote.app.getAppPath() : staticPath; -const iconsPath = fs.existsSync(path.resolve(appPath, 'icons.json')) - ? path.resolve(appPath, 'icons.json') - : path.resolve(staticPath, 'icons.json'); +function getIconsPath() { + return path.resolve(getStaticPath(), 'icons.json'); +} export type Icons = { [key: string]: number[]; }; -export const ICONS: Icons = JSON.parse( - fs.readFileSync(iconsPath, {encoding: 'utf8'}), -); +let _icons: Icons | undefined; + +export function getIcons(): Icons { + return ( + _icons! ?? + (_icons = JSON.parse(fs.readFileSync(getIconsPath(), {encoding: 'utf8'}))) + ); +} // Takes a string like 'star', or 'star-outline', and converts it to // {trimmedName: 'star', variant: 'filled'} or {trimmedName: 'star', variant: 'outline'} @@ -73,7 +77,7 @@ export function buildIconURL(name: string, size: number, density: number) { }&size=${size}&set=facebook_icons&density=${density}x`; if ( typeof window !== 'undefined' && - (!ICONS[name] || !ICONS[name].includes(size)) + (!getIcons()[name] || !getIcons()[name].includes(size)) ) { // From utils/isProduction const isProduction = !/node_modules[\\/]electron[\\/]/.test( @@ -82,7 +86,7 @@ export function buildIconURL(name: string, size: number, density: number) { ); if (!isProduction) { - const existing = ICONS[name] || (ICONS[name] = []); + const existing = getIcons()[name] || (getIcons()[name] = []); if (!existing.includes(size)) { // Check if that icon actually exists! fetch(url) @@ -92,8 +96,8 @@ export function buildIconURL(name: string, size: number, density: number) { existing.push(size); existing.sort(); fs.writeFileSync( - iconsPath, - JSON.stringify(ICONS, null, 2), + getIconsPath(), + JSON.stringify(getIcons(), null, 2), 'utf8', ); console.warn( diff --git a/src/utils/pathUtils.tsx b/src/utils/pathUtils.tsx new file mode 100644 index 000000000..8c82ffc8b --- /dev/null +++ b/src/utils/pathUtils.tsx @@ -0,0 +1,29 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import path from 'path'; +import fs from 'fs'; +import {remote} from 'electron'; + +let _staticPath = ''; + +export function getStaticPath() { + if (_staticPath) { + return _staticPath; + } + if (remote && fs.existsSync(remote.app.getAppPath())) { + _staticPath = path.join(remote.app.getAppPath(), 'static'); + } else { + _staticPath = path.resolve(__dirname, '..', '..', 'static'); + } + if (!fs.existsSync(_staticPath)) { + throw new Error('Static path does not exist: ' + _staticPath); + } + return _staticPath; +}