mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53924 Fix failing `yarn build` runs following D83070252. Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D83161092 fbshipit-source-id: 3e9e984699750add82794281a387eac0534da60d
129 lines
3.0 KiB
JavaScript
129 lines
3.0 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
* @format
|
|
* @oncall react_native
|
|
*/
|
|
|
|
// $FlowFixMe[unclear-type] We have no Flow types for the Electron API.
|
|
const {app} = require('electron') as any;
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
type Options = $ReadOnly<{
|
|
name?: string,
|
|
defaults?: Object,
|
|
}>;
|
|
|
|
/**
|
|
* A data persistence layer for storing application settings, modelled after
|
|
* [`electron-store`](https://www.npmjs.com/package/electron-store).
|
|
*
|
|
* Values are saved in a `config.json` file in `app.getPath('userData')`.
|
|
*
|
|
* Compatibility:
|
|
* - Maintains API and file format compatibility with `electron-store@8.2.0`.
|
|
* - Supports the Electron main process only.
|
|
*/
|
|
export default class SettingsStore {
|
|
#defaultValues: Object = {};
|
|
path: string;
|
|
|
|
constructor(options: Options = {}) {
|
|
options = {
|
|
name: 'config',
|
|
...options,
|
|
};
|
|
this.#defaultValues = {
|
|
...this.#defaultValues,
|
|
...options.defaults,
|
|
};
|
|
this.path = path.resolve(
|
|
app.getPath('userData'),
|
|
`${options.name ?? 'config'}.json`,
|
|
);
|
|
}
|
|
|
|
get(key: string, defaultValue?: any): any {
|
|
const store = this.store;
|
|
return store[key] !== undefined ? store[key] : defaultValue;
|
|
}
|
|
|
|
set(key: string, value: any): void {
|
|
const {store} = this;
|
|
if (typeof key === 'object') {
|
|
const object = key;
|
|
for (const [k, v] of Object.entries(object)) {
|
|
store[k] = v;
|
|
}
|
|
} else {
|
|
store[key] = value;
|
|
}
|
|
this.store = store;
|
|
}
|
|
|
|
has(key: string): boolean {
|
|
return key in this.store;
|
|
}
|
|
|
|
reset(...keys: Array<string>): void {
|
|
for (const key of keys) {
|
|
if (this.#defaultValues[key] != null) {
|
|
this.set(key, this.#defaultValues[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
delete(key: string): void {
|
|
const {store} = this;
|
|
delete store[key];
|
|
this.store = store;
|
|
}
|
|
|
|
clear(): void {
|
|
this.store = {};
|
|
for (const key of Object.keys(this.#defaultValues)) {
|
|
this.reset(key);
|
|
}
|
|
}
|
|
|
|
get store(): {[string]: mixed} {
|
|
try {
|
|
const data = fs.readFileSync(this.path, 'utf8');
|
|
const deserializedData = this._deserialize(data);
|
|
return {
|
|
...((deserializedData: any): {[string]: mixed}),
|
|
};
|
|
} catch (error) {
|
|
if (error?.code === 'ENOENT') {
|
|
this._ensureDirectory();
|
|
return {};
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
set store(value: mixed): void {
|
|
this._ensureDirectory();
|
|
this._write(value);
|
|
}
|
|
|
|
_deserialize = (value: string): mixed => JSON.parse(value);
|
|
_serialize = (value: mixed): string =>
|
|
JSON.stringify(value, undefined, '\t') ?? '';
|
|
|
|
_ensureDirectory(): void {
|
|
fs.mkdirSync(path.dirname(this.path), {recursive: true});
|
|
}
|
|
|
|
_write(value: mixed): void {
|
|
const data = this._serialize(value);
|
|
|
|
fs.writeFileSync(this.path, data, {mode: 0o666});
|
|
}
|
|
}
|