Lightcord/src/common/paths.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-05-16 23:24:51 +02:00
import * as fs from "fs"
import * as path from "path"
import rimraf from "rimraf"
import { app } from "electron"
// Determines environment-specific paths based on info provided
import originalFs from 'fs';
let userDataPath = null;
let resourcesPath = null;
function determineAppUserDataRoot() {
2020-05-22 18:42:30 +02:00
return app.getPath('appData')
2020-05-16 23:24:51 +02:00
}
function determineUserData(userDataRoot, buildInfo) {
return path.join(userDataRoot, 'Lightcord');
}
// cleans old version data in the background
export function cleanOldVersions(buildInfo) {
const entries = fs.readdirSync(userDataPath) || [];
entries.forEach(entry => {
const fullPath = path.join(userDataPath, entry);
if (fs.statSync(fullPath).isDirectory() && entry.indexOf(buildInfo.version) === -1) {
if (entry.match('^[0-9]+.[0-9]+.[0-9]+') != null) {
console.log('Removing old directory ', entry);
rimraf(fullPath, originalFs, error => {
if (error) {
console.warn('...failed with error: ', error);
}
});
}
}
});
}
export function init(buildInfo) {
resourcesPath = path.join(require.main.filename, '..', '..', '..');
const userDataRoot = determineAppUserDataRoot();
userDataPath = determineUserData(userDataRoot, buildInfo);
const { app } = require('electron');
app.setPath('userData', userDataPath);
console.log(userDataPath, buildInfo.version)
}
export function getUserData() {
return userDataPath;
}
export function getResources() {
return resourcesPath;
}