Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ export const USER_DIRECTORY = os.homedir();
export const CONFIG_DIRECTORY = path.join(USER_DIRECTORY, '.config');
export const CONFIG_FILE_NAME = 'foji.json';
export const CONFIG_FILE_PATH = path.join(CONFIG_DIRECTORY, CONFIG_FILE_NAME);
export const HAS_CONFIGURATION = fs.existsSync(
path.join(CONFIG_DIRECTORY, 'foji.json')
);
export const HAS_CONFIGURATION =
fs.existsSync(path.join(CONFIG_DIRECTORY, 'foji.json')) &&
typeof (JSON.parse(fs.readFileSync(CONFIG_FILE_PATH, 'utf-8')) as Config)
.commands === 'object';

export function createConfig(
newConfig: Config = { commands: {} },
useLettersSaved = false
): Config {
if (!HAS_CONFIGURATION) fs.mkdirSync(CONFIG_DIRECTORY, { recursive: true });

if (!useLettersSaved) newConfig.lettersSaved = getConfig().lettersSaved;
if (!useLettersSaved)
newConfig.lettersSaved = HAS_CONFIGURATION ? getConfig().lettersSaved : 0;

fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(newConfig, null, 2));
return newConfig;
Expand Down
Loading