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:
committed by
Facebook GitHub Bot
parent
72a74c9a1d
commit
1d04fc3e34
@@ -21,7 +21,7 @@ import {
|
|||||||
genMercurialRevision,
|
genMercurialRevision,
|
||||||
} from './build-utils';
|
} from './build-utils';
|
||||||
import fetch from 'node-fetch';
|
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) {
|
function generateManifest(versionNumber: string) {
|
||||||
const filePath = path.join(__dirname, '..', 'dist');
|
const filePath = path.join(__dirname, '..', 'dist');
|
||||||
@@ -129,7 +129,7 @@ function copyStaticFolder(buildFolder: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function downloadIcons(buildFolder: string) {
|
function downloadIcons(buildFolder: string) {
|
||||||
const iconURLs = Object.entries(ICONS).reduce<
|
const iconURLs = Object.entries(getIcons()).reduce<
|
||||||
{
|
{
|
||||||
name: string;
|
name: string;
|
||||||
size: number;
|
size: number;
|
||||||
|
|||||||
@@ -10,23 +10,27 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {remote} from 'electron';
|
import {remote} from 'electron';
|
||||||
|
import {getStaticPath} from './pathUtils';
|
||||||
|
|
||||||
const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32];
|
const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32];
|
||||||
const DENSITIES = [1, 1.5, 2, 3, 4];
|
const DENSITIES = [1, 1.5, 2, 3, 4];
|
||||||
|
|
||||||
const staticPath = path.resolve(__dirname, '..', '..', 'static');
|
function getIconsPath() {
|
||||||
const appPath = remote ? remote.app.getAppPath() : staticPath;
|
return path.resolve(getStaticPath(), 'icons.json');
|
||||||
const iconsPath = fs.existsSync(path.resolve(appPath, 'icons.json'))
|
}
|
||||||
? path.resolve(appPath, 'icons.json')
|
|
||||||
: path.resolve(staticPath, 'icons.json');
|
|
||||||
|
|
||||||
export type Icons = {
|
export type Icons = {
|
||||||
[key: string]: number[];
|
[key: string]: number[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ICONS: Icons = JSON.parse(
|
let _icons: Icons | undefined;
|
||||||
fs.readFileSync(iconsPath, {encoding: 'utf8'}),
|
|
||||||
|
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
|
// Takes a string like 'star', or 'star-outline', and converts it to
|
||||||
// {trimmedName: 'star', variant: 'filled'} or {trimmedName: 'star', variant: 'outline'}
|
// {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`;
|
}&size=${size}&set=facebook_icons&density=${density}x`;
|
||||||
if (
|
if (
|
||||||
typeof window !== 'undefined' &&
|
typeof window !== 'undefined' &&
|
||||||
(!ICONS[name] || !ICONS[name].includes(size))
|
(!getIcons()[name] || !getIcons()[name].includes(size))
|
||||||
) {
|
) {
|
||||||
// From utils/isProduction
|
// From utils/isProduction
|
||||||
const isProduction = !/node_modules[\\/]electron[\\/]/.test(
|
const isProduction = !/node_modules[\\/]electron[\\/]/.test(
|
||||||
@@ -82,7 +86,7 @@ export function buildIconURL(name: string, size: number, density: number) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!isProduction) {
|
if (!isProduction) {
|
||||||
const existing = ICONS[name] || (ICONS[name] = []);
|
const existing = getIcons()[name] || (getIcons()[name] = []);
|
||||||
if (!existing.includes(size)) {
|
if (!existing.includes(size)) {
|
||||||
// Check if that icon actually exists!
|
// Check if that icon actually exists!
|
||||||
fetch(url)
|
fetch(url)
|
||||||
@@ -92,8 +96,8 @@ export function buildIconURL(name: string, size: number, density: number) {
|
|||||||
existing.push(size);
|
existing.push(size);
|
||||||
existing.sort();
|
existing.sort();
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
iconsPath,
|
getIconsPath(),
|
||||||
JSON.stringify(ICONS, null, 2),
|
JSON.stringify(getIcons(), null, 2),
|
||||||
'utf8',
|
'utf8',
|
||||||
);
|
);
|
||||||
console.warn(
|
console.warn(
|
||||||
|
|||||||
29
src/utils/pathUtils.tsx
Normal file
29
src/utils/pathUtils.tsx
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user