Created
April 1, 2020 05:37
-
-
Save Mativve/6f33cd280e3387891e0d9cc3576b8488 to your computer and use it in GitHub Desktop.
It's part of filesystem in Electron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> | |
</head> | |
<body> | |
<script> | |
const {remote} = require('electron'); | |
let fs = require('fs'), localData, fileName = "appData.json"; | |
function getAppDataPath() { | |
switch (process.platform) { | |
case "darwin": { | |
return path.join(process.env.HOME, "Library", "Application Support", "MyApp"); | |
} | |
case "win32": { | |
return path.join(process.env.APPDATA, "MyApp"); | |
} | |
case "linux": { | |
return path.join(process.env.HOME, ".MyApp"); | |
} | |
default: { | |
console.log("Unsupported platform!"); | |
process.exit(1); | |
} | |
} | |
} | |
function loadAppData() { | |
const appDataFilePath = path.join(appDatatDirPath, fileName); | |
fs.readFile(appDataFilePath, (err, data) => { | |
if (err) { | |
console.log("There was a problem loading data!"); | |
} else { | |
console.log("Data loaded correctly!"); | |
localData = JSON.parse(data); | |
console.log(localData); | |
} | |
}); | |
} | |
function saveAppData(content) { | |
const appDatatDirPath = getAppDataPath(); | |
// Create appDataDir if not exist | |
if (!fs.existsSync(appDatatDirPath)) { | |
fs.mkdirSync(appDatatDirPath); | |
} | |
const appDataFilePath = path.join(appDatatDirPath, fileName); | |
content = JSON.stringify(content); | |
fs.writeFile(appDataFilePath, content, (err) => { | |
if (err) { | |
console.log("There was a problem saving data!"); | |
console.log(err); | |
} else { | |
console.log("Data saved correctly!"); | |
loadAppData(); | |
} | |
}); | |
} | |
function initappData() { | |
if (fs.existsSync(fileName)) { | |
console.log("File founded! Loading..."); | |
loadAppData(); | |
} else { | |
let defData = { | |
"patients": [], | |
"actions": [], | |
"visits": [] | |
}; | |
console.log("File not exist! Creating..."); | |
saveAppData(defData); | |
} | |
} | |
initappData(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment