copykitku/src/utils.ts
2020-08-21 11:53:55 +02:00

36 lines
1.1 KiB
TypeScript

/*
* Copycat. Copyright (C) 2020 selfisekai <laura@selfisekai.rocks> and other contributors.
*
* This is free software, and you are welcome to redistribute it
* under the GNU General Public License 3.0 or later; see the LICENSE file for details,
* or, if the file is unavailable, visit <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*/
import { readFileSync, writeFileSync } from 'fs-extra';
import toml from '@iarna/toml';
import appdataPath from 'appdata-path';
import { CopycatConfig, CopycatProfile } from './types';
export const DEFAULT_CONFIG: CopycatConfig = {
vendorConfigs: [],
};
export const getConfigPath = () => appdataPath('copycat');
export const getConfig = () => {
try {
const file = readFileSync(getConfigPath());
return (toml.parse(file.toString('utf-8')) as unknown) as CopycatConfig;
} catch (err) {
if (err.code === 'ENOENT') {
setConfig(DEFAULT_CONFIG);
}
return DEFAULT_CONFIG;
}
};
export const setConfig = (config: CopycatConfig) =>
writeFileSync(getConfigPath(), toml.stringify({ ...DEFAULT_CONFIG, ...config } as any), {
encoding: 'utf-8',
});