Fix not filtering .json files for config

This commit is contained in:
ajnart
2023-01-08 13:09:54 +09:00
parent c2a9ff44fd
commit 6f6888f6a7
4 changed files with 13 additions and 6 deletions

View File

@@ -3,8 +3,8 @@ import fs from 'fs';
import { backendMigrateConfig } from '../../tools/config/backendMigrateConfig'; import { backendMigrateConfig } from '../../tools/config/backendMigrateConfig';
export default async (req: NextApiRequest, res: NextApiResponse) => { export default async (req: NextApiRequest, res: NextApiResponse) => {
// Get all the configs in the /data/configs folder // Gets all the config files
const configs = fs.readdirSync('./data/configs'); const configs = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json'));
// If there is no config, redirect to the index // If there is no config, redirect to the index
configs.every((config) => { configs.every((config) => {
const configData = JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8')); const configData = JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8'));

View File

@@ -17,9 +17,10 @@ export async function getServerSideProps({
res, res,
locale, locale,
}: GetServerSidePropsContext): Promise<{ props: DashboardServerSideProps }> { }: GetServerSidePropsContext): Promise<{ props: DashboardServerSideProps }> {
// Check that all the json files in the /data/configs folder are migrated // Get all the configs in the /data/configs folder
// If not, redirect to the migrate page // All the files that end in ".json"
const configs = await fs.readdirSync('./data/configs'); const configs = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json'));
if ( if (
!configs.every( !configs.every(
(config) => JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8')).schemaVersion (config) => JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8')).schemaVersion

View File

@@ -246,7 +246,9 @@ function SwitchToggle() {
export async function getServerSideProps({ req, res, locale }: GetServerSidePropsContext) { export async function getServerSideProps({ req, res, locale }: GetServerSidePropsContext) {
// Get all the configs in the /data/configs folder // 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) { if (configs.length === 0) {
res.writeHead(302, { res.writeHead(302, {
Location: '/', Location: '/',

View File

@@ -6,6 +6,10 @@ import { migrateConfig } from './migrateConfig';
export function backendMigrateConfig(config: Config, name: string): ConfigType { export function backendMigrateConfig(config: Config, name: string): ConfigType {
const migratedConfig = migrateConfig(config); 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 // Overrite the file ./data/configs/${name}.json
// with the new config format // with the new config format
fs.writeFileSync(`./data/configs/${name}.json`, JSON.stringify(migratedConfig, null, 2)); fs.writeFileSync(`./data/configs/${name}.json`, JSON.stringify(migratedConfig, null, 2));