Lightcord/modules/discord_desktop_core/core/common/paths.js

111 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-12-12 11:56:28 +01:00
"use strict";
2020-05-16 23:24:51 +02:00
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cleanOldVersions = cleanOldVersions;
exports.init = init;
exports.getUserData = getUserData;
exports.getUserDataVersioned = getUserDataVersioned;
exports.getResources = getResources;
2020-12-12 11:56:28 +01:00
exports.getModuleDataPath = getModuleDataPath;
exports.getInstallPath = getInstallPath;
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
var _fs = _interopRequireDefault(require("fs"));
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
var _mkdirp = _interopRequireDefault(require("mkdirp"));
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
var _path = _interopRequireDefault(require("path"));
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
var _rimraf = _interopRequireDefault(require("rimraf"));
2020-05-16 23:24:51 +02:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Determines environment-specific paths based on info provided
const originalFs = require('original-fs');
let userDataPath = null;
let userDataVersionedPath = null;
let resourcesPath = null;
2020-12-12 11:56:28 +01:00
let moduleDataPath = null;
let installPath = null;
2020-05-16 23:24:51 +02:00
function determineAppUserDataRoot() {
2020-12-12 11:56:28 +01:00
const {
app
} = require('electron');
2020-05-16 23:24:51 +02:00
return app.getPath('appData');
}
function determineUserData(userDataRoot, buildInfo) {
2020-12-12 11:56:28 +01:00
return _path.default.join(userDataRoot, 'discord' + (buildInfo.releaseChannel == 'stable' ? '' : buildInfo.releaseChannel));
} // cleans old version data in the background
2020-05-16 23:24:51 +02:00
function cleanOldVersions(buildInfo) {
2020-12-12 11:56:28 +01:00
const entries = _fs.default.readdirSync(userDataPath) || [];
2020-05-16 23:24:51 +02:00
entries.forEach(entry => {
2020-12-12 11:56:28 +01:00
const fullPath = _path.default.join(userDataPath, entry);
if (_fs.default.lstatSync(fullPath).isDirectory() && entry.indexOf(buildInfo.version) === -1) {
2020-05-16 23:24:51 +02:00
if (entry.match('^[0-9]+.[0-9]+.[0-9]+') != null) {
console.log('Removing old directory ', entry);
2020-12-12 11:56:28 +01:00
(0, _rimraf.default)(fullPath, originalFs, error => {
2020-05-16 23:24:51 +02:00
if (error) {
console.warn('...failed with error: ', error);
}
});
}
}
});
}
function init(buildInfo) {
2020-12-12 11:56:28 +01:00
resourcesPath = _path.default.join(require.main.filename, '..', '..', '..');
2020-05-16 23:24:51 +02:00
const userDataRoot = determineAppUserDataRoot();
userDataPath = determineUserData(userDataRoot, buildInfo);
2020-12-12 11:56:28 +01:00
const {
app
} = require('electron');
2020-05-16 23:24:51 +02:00
app.setPath('userData', userDataPath);
2020-12-12 11:56:28 +01:00
userDataVersionedPath = _path.default.join(userDataPath, buildInfo.version);
_mkdirp.default.sync(userDataVersionedPath);
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
if (buildInfo.localModulesRoot != null) {
moduleDataPath = buildInfo.localModulesRoot;
} else if (buildInfo.newUpdater) {
moduleDataPath = _path.default.join(userDataPath, 'module_data');
} else {
moduleDataPath = _path.default.join(userDataVersionedPath, 'modules');
}
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
const exeDir = _path.default.dirname(app.getPath('exe'));
if (/^app-[0-9]+\.[0-9]+\.[0-9]+/.test(_path.default.basename(exeDir))) {
installPath = _path.default.join(exeDir, '..');
}
2020-05-16 23:24:51 +02:00
}
function getUserData() {
return userDataPath;
}
function getUserDataVersioned() {
return userDataVersionedPath;
}
function getResources() {
return resourcesPath;
}
2020-12-12 11:56:28 +01:00
function getModuleDataPath() {
return moduleDataPath;
}
function getInstallPath() {
return installPath;
2020-05-16 23:24:51 +02:00
}