add option into setting

This commit is contained in:
monoid 2021-01-09 20:29:01 +09:00
parent 15b6d0c2c2
commit f70cfd041a

View File

@ -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){