Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit bb23b36

Browse files
mweststratefacebook-github-bot
authored andcommitted
Fixed flipper settings not being loaded / saved on fresh install
Summary: I was running flipper-server on a fresh machine without Flipper installed and discovered that reading / writing settings failed since `~/.flipper` wasn't existing, due to using `access` instead of `pathExists`. Added a warning about needing to restart the server after making changes, since that is tricky to do from the UI. Fixed an issue in the settings screen, which would fs.stat as part of rendering, causing the Settings UI not to load. Reviewed By: timur-valiev, aigoncharov Differential Revision: D32984538 fbshipit-source-id: 2b2011ad9d84c72ac824d92a8c96f636237b8771
1 parent 4222aca commit bb23b36

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

desktop/flipper-server-core/src/utils/settings.tsx

+15-6
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,39 @@ import os from 'os';
1111
import {resolve} from 'path';
1212
import xdg from 'xdg-basedir';
1313
import {Settings, Tristate} from 'flipper-common';
14-
import {readFile, writeFile, access} from 'fs-extra';
14+
import {readFile, writeFile, pathExists, mkdirp} from 'fs-extra';
1515

1616
export async function loadSettings(): Promise<Settings> {
17-
if (!access(getSettingsFile())) {
17+
if (!pathExists(getSettingsFile())) {
18+
return getDefaultSettings();
19+
}
20+
try {
21+
const json = await readFile(getSettingsFile(), {encoding: 'utf8'});
22+
return JSON.parse(json);
23+
} catch (e) {
24+
console.warn('Failed to load settings file', e);
1825
return getDefaultSettings();
1926
}
20-
const json = await readFile(getSettingsFile(), {encoding: 'utf8'});
21-
return JSON.parse(json);
2227
}
2328

2429
export async function saveSettings(settings: Settings): Promise<void> {
30+
await mkdirp(getSettingsDir());
2531
await writeFile(getSettingsFile(), JSON.stringify(settings, null, 2), {
2632
encoding: 'utf8',
2733
});
2834
}
2935

30-
function getSettingsFile() {
36+
function getSettingsDir() {
3137
return resolve(
3238
...(xdg.config ? [xdg.config] : [os.homedir(), '.config']),
3339
'flipper',
34-
'settings.json',
3540
);
3641
}
3742

43+
function getSettingsFile() {
44+
return resolve(getSettingsDir(), 'settings.json');
45+
}
46+
3847
export const DEFAULT_ANDROID_SDK_PATH = getDefaultAndroidSdkPath();
3948

4049
function getDefaultSettings(): Settings {

desktop/flipper-ui-browser/src/initializeRenderHost.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ export function initializeRenderHost(
5252
);
5353
},
5454
restartFlipper() {
55-
// TODO: restart server as well
56-
window.location.reload();
55+
window.flipperShowError!(
56+
'Flipper settings have changed, please restart flipper server for the changes to take effect',
57+
);
5758
},
5859
loadDefaultPlugins: getDefaultPluginsIndex,
5960
serverConfig: flipperServerConfig,

desktop/flipper-ui-core/src/chrome/settings/configFields.tsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import React, {useState} from 'react';
2020
import {promises as fs} from 'fs';
2121
import {theme} from 'flipper-plugin';
2222
import {getRenderHostInstance} from '../../RenderHost';
23+
import {useEffect} from 'react';
2324

2425
export const ConfigFieldContainer = styled(FlexRow)({
2526
paddingLeft: 10,
@@ -70,14 +71,17 @@ export function FilePathConfigField(props: {
7071
const renderHost = getRenderHostInstance();
7172
const [value, setValue] = useState(props.defaultValue);
7273
const [isValid, setIsValid] = useState(true);
73-
fs.stat(value)
74-
.then((stat) => props.isRegularFile !== stat.isDirectory())
75-
.then((valid) => {
76-
if (valid !== isValid) {
77-
setIsValid(valid);
74+
75+
useEffect(() => {
76+
(async function () {
77+
try {
78+
const stat = await fs.stat(value);
79+
setIsValid(props.isRegularFile !== stat.isDirectory());
80+
} catch (_) {
81+
setIsValid(false);
7882
}
79-
})
80-
.catch((_) => setIsValid(false));
83+
})();
84+
}, [props.isRegularFile, value]);
8185

8286
return (
8387
<ConfigFieldContainer>

0 commit comments

Comments
 (0)