v1
This commit is contained in:
66
src/main/ConfigManager.js
Normal file
66
src/main/ConfigManager.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const fs = require("fs");
|
||||
const { dialog } = require("electron");
|
||||
|
||||
const ConfigManager = {
|
||||
save: (obj_window, obj_config) => {
|
||||
return dialog.showSaveDialog(obj_window, {
|
||||
title: "Sauvegarder la configuration",
|
||||
defaultPath: "render_config.json",
|
||||
filters: [
|
||||
{ name: "Configuration JSON", extensions: ["json"] },
|
||||
],
|
||||
})
|
||||
.then((obj_result) => {
|
||||
if (obj_result.canceled || !obj_result.filePath) {
|
||||
return { is_success: false };
|
||||
}
|
||||
|
||||
let str_json = JSON.stringify(obj_config, null, 4);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(obj_result.filePath, str_json, "utf8", (obj_err) => {
|
||||
if (obj_err) {
|
||||
reject(new Error("Impossible de sauvegarder : " + obj_err.message));
|
||||
return;
|
||||
}
|
||||
resolve({ is_success: true, str_path: obj_result.filePath });
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
load: (obj_window) => {
|
||||
return dialog.showOpenDialog(obj_window, {
|
||||
title: "Charger une configuration",
|
||||
filters: [
|
||||
{ name: "Configuration JSON", extensions: ["json"] },
|
||||
],
|
||||
properties: ["openFile"],
|
||||
})
|
||||
.then((obj_result) => {
|
||||
if (obj_result.canceled || obj_result.filePaths.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let str_file_path = obj_result.filePaths[0];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(str_file_path, "utf8", (obj_err, str_data) => {
|
||||
if (obj_err) {
|
||||
reject(new Error("Impossible de lire : " + obj_err.message));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let obj_config = JSON.parse(str_data);
|
||||
resolve(obj_config);
|
||||
} catch (obj_parse_err) {
|
||||
reject(new Error("Fichier corrompu : " + obj_parse_err.message));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = ConfigManager;
|
||||
Reference in New Issue
Block a user