28 lines
782 B
TypeScript
28 lines
782 B
TypeScript
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',
|
|
});
|