|
|
|
@ -8,6 +8,8 @@ const dataContext=createContext(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const filePath= 'param.json'; |
|
|
|
const filePath= 'param.json'; |
|
|
|
|
|
|
|
const GlobalSettingsPath='./GlobalSettings.json'; |
|
|
|
|
|
|
|
const PrivateSettingsPath='./PrivateSettings.json'; |
|
|
|
|
|
|
|
|
|
|
|
export function DataProvider({children}) { |
|
|
|
export function DataProvider({children}) { |
|
|
|
|
|
|
|
|
|
|
|
@ -17,16 +19,36 @@ export function DataProvider({children}) { |
|
|
|
|
|
|
|
|
|
|
|
try{ |
|
|
|
try{ |
|
|
|
|
|
|
|
|
|
|
|
const folder=await path.appDataDir(); |
|
|
|
// check exists |
|
|
|
if (!(await exists(folder))) { |
|
|
|
const globalExists=await exists(GlobalSettingsPath, { baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
const privateExists=await exists(PrivateSettingsPath, { baseDir: BaseDirectory.Resource }); |
|
|
|
console.log('Creating folder:', folder); |
|
|
|
console.log("GlobalSettings exists:", globalExists); |
|
|
|
await mkdir(folder); |
|
|
|
console.log("PrivateSettings exists:", privateExists); |
|
|
|
|
|
|
|
if(!globalExists){ |
|
|
|
|
|
|
|
// copy from public/default.json |
|
|
|
|
|
|
|
const res=await fetch('default.json'); |
|
|
|
|
|
|
|
const defaultData=await res.json(); |
|
|
|
|
|
|
|
await writeTextFile(GlobalSettingsPath, JSON.stringify(defaultData), |
|
|
|
|
|
|
|
{ baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
console.log("Default global settings written to", |
|
|
|
|
|
|
|
path(GlobalSettingsPath, { baseDir: BaseDirectory.Resource })); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(!privateExists){ |
|
|
|
|
|
|
|
await writeTextFile(PrivateSettingsPath, JSON.stringify({}), |
|
|
|
|
|
|
|
{ baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
console.log("Empty private settings written to", |
|
|
|
|
|
|
|
path(PrivateSettingsPath, { baseDir: BaseDirectory.Resource })); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const contents=await readTextFile(filePath, { baseDir: BaseDirectory.AppData }); |
|
|
|
|
|
|
|
let output=await JSON.parse(contents); |
|
|
|
// const contents=await readTextFile(filePath, { baseDir: BaseDirectory.AppData }); |
|
|
|
|
|
|
|
const contents=await readTextFile(GlobalSettingsPath, { baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
let output=await JSON.parse(contents); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const private_contents=await readTextFile(PrivateSettingsPath, { baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
const private_output=await JSON.parse(private_contents); |
|
|
|
|
|
|
|
output={...output, ...private_output}; |
|
|
|
|
|
|
|
|
|
|
|
// check if all keys in ParamKeys are present in output |
|
|
|
// check if all keys in ParamKeys are present in output |
|
|
|
const missingKeys = ParamKeys.filter(key => !output.hasOwnProperty(key)); |
|
|
|
const missingKeys = ParamKeys.filter(key => !output.hasOwnProperty(key)); |
|
|
|
@ -58,10 +80,21 @@ export function DataProvider({children}) { |
|
|
|
setData(towrite); |
|
|
|
setData(towrite); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(towrite.id){ |
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
await writeTextFile(PrivateSettingsPath, JSON.stringify({ |
|
|
|
|
|
|
|
id: towrite.id |
|
|
|
|
|
|
|
}), { baseDir: BaseDirectory.Resource }); |
|
|
|
|
|
|
|
console.log("Private settings written successfully"); |
|
|
|
|
|
|
|
}catch(error){ |
|
|
|
|
|
|
|
console.error("Error writing private settings:", error); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{ |
|
|
|
try{ |
|
|
|
const folder = await path.appDataDir(); |
|
|
|
const res_write=await writeTextFile(GlobalSettingsPath, JSON.stringify(towrite), { baseDir: BaseDirectory.Resource }); |
|
|
|
const res_write=await writeTextFile(`${folder}\\${filePath}`, JSON.stringify(towrite)) |
|
|
|
console.log("File written successfully", GlobalSettingsPath, towrite); |
|
|
|
console.log("File written successfully", `${folder}\\${filePath}`); |
|
|
|
|
|
|
|
}catch(error){ |
|
|
|
}catch(error){ |
|
|
|
console.error("Error writing file:", error); |
|
|
|
console.error("Error writing file:", error); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -87,9 +120,9 @@ export function DataProvider({children}) { |
|
|
|
read, |
|
|
|
read, |
|
|
|
write, |
|
|
|
write, |
|
|
|
openFile: async () => { |
|
|
|
openFile: async () => { |
|
|
|
const folder = await path.appDataDir(); |
|
|
|
const folder = await path.resourceDir(); |
|
|
|
const file = await path.join(folder, filePath); |
|
|
|
// const file = await path.join(folder, GlobalSettingsPath); |
|
|
|
await openPath(file); |
|
|
|
await openPath(folder); |
|
|
|
}, |
|
|
|
}, |
|
|
|
}}> |
|
|
|
}}> |
|
|
|
{children} |
|
|
|
{children} |
|
|
|
|