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
This commit is contained in:
Michel Weststrate
2020-03-13 04:36:57 -07:00
committed by Facebook GitHub Bot
parent 72a74c9a1d
commit 1d04fc3e34
3 changed files with 47 additions and 14 deletions

View File

@@ -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;

View File

@@ -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(

29
src/utils/pathUtils.tsx Normal file
View File

@@ -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;
}