Skip to content

Commit

Permalink
fix: Move counter settings to tosu/settings folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Oct 11, 2024
1 parent 3021fd5 commit adf0765
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/common/utils/directories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ export function getProgramPath() {
if ('pkg' in process) return path.dirname(process.execPath);
return process.cwd();
}

export function getSettingsPath(folderName: string) {
if (!folderName) return '';

const settingsFolderPath = path.join(getProgramPath(), 'settings');
if (!fs.existsSync(settingsFolderPath))
fs.mkdirSync(settingsFolderPath, { recursive: true });

const folderPath = path.join(
settingsFolderPath,
`${folderName}.values.json`
);
return folderPath;
}
20 changes: 18 additions & 2 deletions packages/server/utils/parseSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { JsonSafeParse, getStaticPath, wLogger } from '@tosu/common';
import {
JsonSafeParse,
getSettingsPath,
getStaticPath,
wLogger
} from '@tosu/common';
import fs from 'fs';
import path from 'path';

Expand All @@ -15,13 +20,24 @@ export function parseCounterSettings(
decodeURI(folderName),
'settings.json'
);
const settingsValuesPath = path.join(
const settingsValuesPath = getSettingsPath(decodeURI(folderName));
const legacySettingsValuesPath = path.join(
staticPath,
decodeURI(folderName),
'settings.values.json'
);

try {
// copy legacy settings instead of moving/renaming, because some could have their static folder on other drive
if (fs.existsSync(legacySettingsValuesPath)) {
// wLogger.warn('counter-settings', 'copied legacy settings to new folder', decodeURI(folderName));
fs.copyFileSync(legacySettingsValuesPath, settingsValuesPath);
fs.renameSync(
legacySettingsValuesPath,
legacySettingsValuesPath.replace('values', 'old-values')
);
}

if (!fs.existsSync(settingsPath))
fs.writeFileSync(settingsPath, JSON.stringify([]), 'utf8');

Expand Down

0 comments on commit adf0765

Please sign in to comment.