Fix missing whatsapp-sessions.json file, auto create if not exists

This commit is contained in:
Nur Muhammad
2021-01-22 06:45:39 +08:00
parent f8d4bde68d
commit 9bea507a05

View File

@@ -26,6 +26,19 @@ app.get('/', (req, res) => {
const sessions = []; const sessions = [];
const SESSIONS_FILE = './whatsapp-sessions.json'; const SESSIONS_FILE = './whatsapp-sessions.json';
const createSessionsFileIfNotExists = function() {
if (!fs.existsSync(SESSIONS_FILE)) {
try {
fs.writeFileSync(SESSIONS_FILE, JSON.stringify([]));
console.log('Sessions file created successfully.');
} catch(err) {
console.log('Failed to create sessions file: ', err);
}
}
}
createSessionsFileIfNotExists();
const setSessionsFile = function(sessions) { const setSessionsFile = function(sessions) {
fs.writeFile(SESSIONS_FILE, JSON.stringify(sessions), function(err) { fs.writeFile(SESSIONS_FILE, JSON.stringify(sessions), function(err) {
if (err) { if (err) {