From f70cfd041adcb43d727965e9fb3cfd4066c93f20 Mon Sep 17 00:00:00 2001 From: monoid Date: Sat, 9 Jan 2021 20:29:01 +0900 Subject: [PATCH] add option into setting --- src/setting.ts | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/setting.ts b/src/setting.ts index f11d26e..f8536e7 100644 --- a/src/setting.ts +++ b/src/setting.ts @@ -1,11 +1,38 @@ -import { readFileSync } from 'fs'; +import { Settings } from '@material-ui/icons'; +import { readFileSync, writeFileSync } from 'fs'; export type Setting = { - path: string[] + path: string[], + initial_admin_password:string, + localmode: boolean, + guest: boolean, +} +const default_setting:Setting = { + path:[], + initial_admin_password:"admin", + localmode: true, + guest:false, } let setting: null|Setting = null; +const setEmptyToDefault = (target:any,default_table:any)=>{ + let diff_occur = false; + for(const key in default_table){ + if(key === undefined || key in target){ + continue; + } + target[key] = default_table[key]; + diff_occur = true; + } + return diff_occur; +} + export const read_setting_from_file = ()=>{ - return JSON.parse(readFileSync("settings.json",{encoding:"utf8"})) as Setting; + let ret = JSON.parse(readFileSync("settings.json",{encoding:"utf8"})) as Setting; + const diff_occur = setEmptyToDefault(ret,default_setting); + if(diff_occur){ + writeFileSync("settings.json",JSON.stringify(ret)); + } + return ret; } export function get_setting():Setting{ if(setting === null){