Fix Flipper lints #9

Summary:
This introduces a few more lints in fact because I renamed the icon functions
to make clear that they use sync methods under the hood.

Reviewed By: timur-valiev

Differential Revision: D31964701

fbshipit-source-id: d0beb58b5b301f5428fdbfe8c65784df0d86eaad
This commit is contained in:
Pascal Hartig
2021-10-28 05:45:38 -07:00
committed by Facebook GitHub Bot
parent f8117240af
commit 2525a5efd4
12 changed files with 45 additions and 24 deletions

View File

@@ -9,7 +9,7 @@
import React from 'react'; import React from 'react';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import {getIconURL} from '../../utils/icons'; import {getIconURLSync} from '../../utils/icons';
export type IconSize = 8 | 10 | 12 | 16 | 18 | 20 | 24 | 32; export type IconSize = 8 | 10 | 12 | 16 | 18 | 20 | 24 | 32;
@@ -121,7 +121,7 @@ export default class Glyph extends React.PureComponent<{
color={color} color={color}
size={size} size={size}
title={title} title={title}
src={getIconURL( src={getIconURLSync(
variant === 'outline' ? `${name}-outline` : name, variant === 'outline' ? `${name}-outline` : name,
size, size,
typeof window !== 'undefined' ? window.devicePixelRatio : 1, typeof window !== 'undefined' ? window.devicePixelRatio : 1,

View File

@@ -7,7 +7,7 @@
* @format * @format
*/ */
import {buildLocalIconPath, buildIconURL} from '../icons'; import {buildLocalIconPath, buildIconURLSync} from '../icons';
import * as path from 'path'; import * as path from 'path';
test('filled icons get correct local path', () => { test('filled icons get correct local path', () => {
@@ -21,14 +21,14 @@ test('outline icons get correct local path', () => {
}); });
test('filled icons get correct URL', () => { test('filled icons get correct URL', () => {
const iconUrl = buildIconURL('star', 12, 2); const iconUrl = buildIconURLSync('star', 12, 2);
expect(iconUrl).toBe( expect(iconUrl).toBe(
'https://facebook.com/assets/?name=star&variant=filled&size=12&set=facebook_icons&density=2x', 'https://facebook.com/assets/?name=star&variant=filled&size=12&set=facebook_icons&density=2x',
); );
}); });
test('outline icons get correct URL', () => { test('outline icons get correct URL', () => {
const iconUrl = buildIconURL('star-outline', 12, 2); const iconUrl = buildIconURLSync('star-outline', 12, 2);
expect(iconUrl).toBe( expect(iconUrl).toBe(
'https://facebook.com/assets/?name=star&variant=outline&size=12&set=facebook_icons&density=2x', 'https://facebook.com/assets/?name=star&variant=outline&size=12&set=facebook_icons&density=2x',
); );

View File

@@ -589,7 +589,10 @@ export function importDataToStore(source: string, data: string, store: Store) {
export const importFileToStore = (file: string, store: Store) => { export const importFileToStore = (file: string, store: Store) => {
fs.readFile(file, 'utf8', (err, data) => { fs.readFile(file, 'utf8', (err, data) => {
if (err) { if (err) {
console.error(`[exportData] Failed to write to file ${file}: `, err); console.error(
`[exportData] importFileToStore for file ${file} failed:`,
err,
);
return; return;
} }
importDataToStore(file, data, store); importDataToStore(file, data, store);

View File

@@ -7,6 +7,10 @@
* @format * @format
*/ */
// We should get rid of sync use entirely but until then the
// methods are marked as such.
/* eslint-disable node/no-sync */
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
// eslint-disable-next-line flipper/no-electron-remote-imports // eslint-disable-next-line flipper/no-electron-remote-imports
@@ -26,7 +30,7 @@ export type Icons = {
let _icons: Icons | undefined; let _icons: Icons | undefined;
export function getIcons(): Icons { export function getIconsSync(): Icons {
return ( return (
_icons! ?? _icons! ??
(_icons = JSON.parse(fs.readFileSync(getIconsPath(), {encoding: 'utf8'}))) (_icons = JSON.parse(fs.readFileSync(getIconsPath(), {encoding: 'utf8'})))
@@ -67,7 +71,7 @@ export function buildLocalIconURL(name: string, size: number, density: number) {
return `icons/${getIconFileName(icon, size, density)}`; return `icons/${getIconFileName(icon, size, density)}`;
} }
export function buildIconURL(name: string, size: number, density: number) { export function buildIconURLSync(name: string, size: number, density: number) {
const icon = getIconPartsFromName(name); const icon = getIconPartsFromName(name);
// eslint-disable-next-line prettier/prettier // eslint-disable-next-line prettier/prettier
const url = `https://facebook.com/assets/?name=${ const url = `https://facebook.com/assets/?name=${
@@ -77,7 +81,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' &&
(!getIcons()[name] || !getIcons()[name].includes(size)) (!getIconsSync()[name] || !getIconsSync()[name].includes(size))
) { ) {
// From utils/isProduction // From utils/isProduction
const isProduction = !/node_modules[\\/]electron[\\/]/.test( const isProduction = !/node_modules[\\/]electron[\\/]/.test(
@@ -85,7 +89,7 @@ export function buildIconURL(name: string, size: number, density: number) {
); );
if (!isProduction) { if (!isProduction) {
const existing = getIcons()[name] || (getIcons()[name] = []); const existing = getIconsSync()[name] || (getIconsSync()[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)
@@ -96,7 +100,7 @@ export function buildIconURL(name: string, size: number, density: number) {
existing.sort(); existing.sort();
fs.writeFileSync( fs.writeFileSync(
getIconsPath(), getIconsPath(),
JSON.stringify(getIcons(), null, 2), JSON.stringify(getIconsSync(), null, 2),
'utf8', 'utf8',
); );
console.warn( console.warn(
@@ -122,7 +126,7 @@ export function buildIconURL(name: string, size: number, density: number) {
return url; return url;
} }
export function getIconURL(name: string, size: number, density: number) { export function getIconURLSync(name: string, size: number, density: number) {
if (name.indexOf('/') > -1) { if (name.indexOf('/') > -1) {
return name; return name;
} }
@@ -168,5 +172,5 @@ export function getIconURL(name: string, size: number, density: number) {
) { ) {
return buildLocalIconURL(name, size, density); return buildLocalIconURL(name, size, density);
} }
return buildIconURL(name, requestedSize, density); return buildIconURLSync(name, requestedSize, density);
} }

View File

@@ -7,6 +7,9 @@
* @format * @format
*/ */
// Use of sync methods is cached.
/* eslint-disable node/no-sync */
import os from 'os'; import os from 'os';
import isProduction from './isProduction'; import isProduction from './isProduction';
import fs from 'fs-extra'; import fs from 'fs-extra';

View File

@@ -29,11 +29,12 @@ export default class JsonFileStorage {
return readFile(this.filepath) return readFile(this.filepath)
.then((buffer) => buffer.toString()) .then((buffer) => buffer.toString())
.then(this.deserializeValue) .then(this.deserializeValue)
.catch((e) => { .catch(async (e) => {
console.warn( console.warn(
`Failed to read settings file: "${this.filepath}". ${e}. Replacing file with default settings.`, `Failed to read settings file: "${this.filepath}". ${e}. Replacing file with default settings.`,
); );
return this.writeContents(prettyStringify({})).then(() => ({})); await this.writeContents(prettyStringify({}));
return {};
}); });
} }

View File

@@ -7,7 +7,7 @@
* @format * @format
*/ */
import fs from 'fs'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import TOML, {JsonMap} from '@iarna/toml'; import TOML, {JsonMap} from '@iarna/toml';
import {Storage} from 'redux-persist/es/types'; import {Storage} from 'redux-persist/es/types';
@@ -36,7 +36,7 @@ export default class LauncherSettingsStorage implements Storage {
private async parseFile(): Promise<LauncherSettings> { private async parseFile(): Promise<LauncherSettings> {
try { try {
const content = fs.readFileSync(this.filepath).toString(); const content = fs.readFile(this.filepath).toString();
return deserialize(content); return deserialize(content);
} catch (e) { } catch (e) {
console.warn( console.warn(
@@ -50,12 +50,15 @@ export default class LauncherSettingsStorage implements Storage {
private async writeFile(value: LauncherSettings): Promise<void> { private async writeFile(value: LauncherSettings): Promise<void> {
this.ensureDirExists(); this.ensureDirExists();
const content = serialize(value); const content = serialize(value);
fs.writeFileSync(this.filepath, content); return fs.writeFile(this.filepath, content);
} }
private ensureDirExists(): void { private async ensureDirExists(): Promise<void> {
const dir = path.dirname(this.filepath); const dir = path.dirname(this.filepath);
fs.existsSync(dir) || fs.mkdirSync(dir, {recursive: true}); const exists = await fs.pathExists(dir);
if (!exists) {
await fs.mkdir(dir, {recursive: true});
}
} }
} }

View File

@@ -7,6 +7,7 @@
* @format * @format
*/ */
// eslint-disable-next-line flipper/no-electron-remote-imports
import {remote} from 'electron'; import {remote} from 'electron';
export type ProcessConfig = { export type ProcessConfig = {

View File

@@ -7,6 +7,7 @@
* @format * @format
*/ */
// eslint-disable-next-line flipper/no-electron-remote-imports
import {remote} from 'electron'; import {remote} from 'electron';
import isProduction from './isProduction'; import isProduction from './isProduction';

View File

@@ -12,6 +12,7 @@ import path from 'path';
import BaseDevice from '../devices/BaseDevice'; import BaseDevice from '../devices/BaseDevice';
import {reportPlatformFailures} from 'flipper-common'; import {reportPlatformFailures} from 'flipper-common';
import expandTilde from 'expand-tilde'; import expandTilde from 'expand-tilde';
// eslint-disable-next-line flipper/no-electron-remote-imports
import {remote} from 'electron'; import {remote} from 'electron';
import config from '../utils/processConfig'; import config from '../utils/processConfig';

View File

@@ -13,7 +13,7 @@ import {getPreferredEditorUriScheme} from '../fb-stubs/user';
let preferredEditorUriScheme: string | undefined = undefined; let preferredEditorUriScheme: string | undefined = undefined;
export function callVSCode(plugin: string, command: string, params?: string) { export function callVSCode(plugin: string, command: string, params?: string) {
getVSCodeUrl(plugin, command, params).then((url) => return getVSCodeUrl(plugin, command, params).then((url) =>
getFlipperLib().openLink(url), getFlipperLib().openLink(url),
); );
} }

View File

@@ -29,7 +29,11 @@ import {
moveSourceMaps, moveSourceMaps,
} from './build-utils'; } from './build-utils';
import fetch from '@adobe/node-fetch-retry'; import fetch from '@adobe/node-fetch-retry';
import {getIcons, buildLocalIconPath, getIconURL} from '../app/src/utils/icons'; import {
getIconsSync,
buildLocalIconPath,
getIconURLSync,
} from '../app/src/utils/icons';
import isFB from './isFB'; import isFB from './isFB';
import copyPackageWithDependencies from './copy-package-with-dependencies'; import copyPackageWithDependencies from './copy-package-with-dependencies';
import {staticDir, distDir} from './paths'; import {staticDir, distDir} from './paths';
@@ -305,7 +309,7 @@ async function copyStaticFolder(buildFolder: string) {
} }
function downloadIcons(buildFolder: string) { function downloadIcons(buildFolder: string) {
const iconURLs = Object.entries(getIcons()).reduce< const iconURLs = Object.entries(getIconsSync()).reduce<
{ {
name: string; name: string;
size: number; size: number;
@@ -322,7 +326,7 @@ function downloadIcons(buildFolder: string) {
return Promise.all( return Promise.all(
iconURLs.map(({name, size, density}) => { iconURLs.map(({name, size, density}) => {
const url = getIconURL(name, size, density); const url = getIconURLSync(name, size, density);
return fetch(url, { return fetch(url, {
retryOptions: { retryOptions: {
// Be default, only 5xx are retried but we're getting the odd 404 // Be default, only 5xx are retried but we're getting the odd 404