2021-01-09 20:29:01 +09:00
|
|
|
import { Settings } from '@material-ui/icons';
|
2021-01-09 23:23:37 +09:00
|
|
|
import { randomBytes } from 'crypto';
|
2021-01-10 03:04:30 +09:00
|
|
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
2020-12-31 03:06:16 +09:00
|
|
|
|
|
|
|
export type Setting = {
|
2021-01-09 20:29:01 +09:00
|
|
|
path: string[],
|
|
|
|
localmode: boolean,
|
|
|
|
guest: boolean,
|
2021-01-09 23:38:44 +09:00
|
|
|
jwt_secretkey: string,
|
|
|
|
port:number,
|
2021-01-10 03:04:30 +09:00
|
|
|
mode:"development"|"production",
|
|
|
|
cli:boolean,
|
2021-01-09 20:29:01 +09:00
|
|
|
}
|
|
|
|
const default_setting:Setting = {
|
|
|
|
path:[],
|
|
|
|
localmode: true,
|
|
|
|
guest:false,
|
2021-01-09 23:23:37 +09:00
|
|
|
jwt_secretkey:"itsRandom",
|
2021-01-09 23:38:44 +09:00
|
|
|
port:8080,
|
2021-01-10 03:04:30 +09:00
|
|
|
mode:"production",
|
|
|
|
cli:false
|
2020-12-31 03:06:16 +09:00
|
|
|
}
|
|
|
|
let setting: null|Setting = null;
|
2021-01-09 23:23:37 +09:00
|
|
|
|
|
|
|
const setEmptyToDefault = (target:any,default_table:Setting)=>{
|
2021-01-09 20:29:01 +09:00
|
|
|
let diff_occur = false;
|
|
|
|
for(const key in default_table){
|
|
|
|
if(key === undefined || key in target){
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-09 23:23:37 +09:00
|
|
|
target[key] = default_table[key as keyof Setting];
|
2021-01-09 20:29:01 +09:00
|
|
|
diff_occur = true;
|
|
|
|
}
|
|
|
|
return diff_occur;
|
|
|
|
}
|
|
|
|
|
2020-12-31 03:06:16 +09:00
|
|
|
export const read_setting_from_file = ()=>{
|
2021-01-10 03:04:30 +09:00
|
|
|
let ret = existsSync("settings.json") ? JSON.parse(readFileSync("settings.json",{encoding:"utf8"})) : {};
|
2021-01-09 23:23:37 +09:00
|
|
|
const partial_occur = setEmptyToDefault(ret,default_setting);
|
|
|
|
if(partial_occur){
|
2021-01-09 20:29:01 +09:00
|
|
|
writeFileSync("settings.json",JSON.stringify(ret));
|
|
|
|
}
|
2021-01-10 03:04:30 +09:00
|
|
|
return ret as Setting;
|
2020-12-31 03:06:16 +09:00
|
|
|
}
|
|
|
|
export function get_setting():Setting{
|
|
|
|
if(setting === null){
|
|
|
|
setting = read_setting_from_file();
|
2021-01-10 03:04:30 +09:00
|
|
|
const env = process.env.NODE_ENV || 'development';
|
|
|
|
if(env != "production" && env != "development"){
|
|
|
|
throw new Error("process unknown value in NODE_ENV: must be either \"development\" or \"production\"");
|
|
|
|
}
|
|
|
|
setting.mode = env || setting.mode;
|
2020-12-31 03:06:16 +09:00
|
|
|
}
|
|
|
|
return setting;
|
|
|
|
}
|