diff --git a/src/pages/api/migrate.ts b/src/pages/api/migrate.ts index c2aaba65b..379b7c724 100644 --- a/src/pages/api/migrate.ts +++ b/src/pages/api/migrate.ts @@ -3,8 +3,8 @@ import fs from 'fs'; import { backendMigrateConfig } from '../../tools/config/backendMigrateConfig'; export default async (req: NextApiRequest, res: NextApiResponse) => { - // Get all the configs in the /data/configs folder - const configs = fs.readdirSync('./data/configs'); + // Gets all the config files + const configs = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json')); // If there is no config, redirect to the index configs.every((config) => { const configData = JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8')); diff --git a/src/pages/index.tsx b/src/pages/index.tsx index d68cf541c..961e6f9e7 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -17,9 +17,10 @@ export async function getServerSideProps({ res, locale, }: GetServerSidePropsContext): Promise<{ props: DashboardServerSideProps }> { - // Check that all the json files in the /data/configs folder are migrated - // If not, redirect to the migrate page - const configs = await fs.readdirSync('./data/configs'); + // Get all the configs in the /data/configs folder + // All the files that end in ".json" + const configs = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json')); + if ( !configs.every( (config) => JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8')).schemaVersion diff --git a/src/pages/migrate.tsx b/src/pages/migrate.tsx index 6fa046e9b..f38a8dd3c 100644 --- a/src/pages/migrate.tsx +++ b/src/pages/migrate.tsx @@ -246,7 +246,9 @@ function SwitchToggle() { export async function getServerSideProps({ req, res, locale }: GetServerSidePropsContext) { // Get all the configs in the /data/configs folder - const configs = fs.readdirSync('./data/configs'); + // All the files that end in ".json" + const configs = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json')); + if (configs.length === 0) { res.writeHead(302, { Location: '/', diff --git a/src/tools/config/backendMigrateConfig.ts b/src/tools/config/backendMigrateConfig.ts index 54bb55693..dc311e5e3 100644 --- a/src/tools/config/backendMigrateConfig.ts +++ b/src/tools/config/backendMigrateConfig.ts @@ -6,6 +6,10 @@ import { migrateConfig } from './migrateConfig'; export function backendMigrateConfig(config: Config, name: string): ConfigType { const migratedConfig = migrateConfig(config); + // Make a backup of the old file ./data/configs/${name}.json + // New name is ./data/configs/${name}.bak + fs.copyFileSync(`./data/configs/${name}.json`, `./data/configs/${name}.json.bak`); + // Overrite the file ./data/configs/${name}.json // with the new config format fs.writeFileSync(`./data/configs/${name}.json`, JSON.stringify(migratedConfig, null, 2));