UNPKG

kbc-paths

Version:

Qt standard paths - translate & resolve in node - only partial support

132 lines (111 loc) 4.59 kB
'use strict'; // Path type macOS Windows Linux // HomeLocation "~" "C:/Users/<USER>" "~" // DesktopLocation "~/Desktop" "C:/Users/<USER>/Desktop" "~/Desktop" // DocumentsLocation "~/Documents" "C:/Users/<USER>/Documents" "~/Documents" // MusicLocation "~/Music" "C:/Users/<USER>/Music" "~/Music" // MoviesLocation "~/Movies" "C:/Users/<USER>/Videos" "~/Videos" // PicturesLocation "~/Pictures" "C:/Users/<USER>/Pictures" "~/Pictures" // DownloadLocation "~/Downloads" "C:/Users/<USER>/Downloads" "~/Downloads" const kc = require('kbc-configuration'); const os = require('os'); const path = require('path'); const processEnvSystemDrive = process.env.SystemDrive || 'C:'; class QtPaths { template(user = os.userInfo().username) { const full = (user) => { return { 'DesktopLocation': { 'linux': ['/home', user, 'Desktop'], 'win32': [processEnvSystemDrive, 'Users', user, 'Desktop'], 'darwin': ['/Users', user, 'Desktop'] }, 'DocumentsLocation': { 'linux': ['/home', user, 'Documents'], 'win32': [processEnvSystemDrive, 'Users', user, 'Documents'], 'darwin': ['/Users', user, 'Documents'] }, 'MusicLocation': { 'linux': ['/home', user, 'Music'], 'win32': [processEnvSystemDrive, 'Users', user, 'Music'], 'darwin': ['/Users', user, 'Music'] }, 'MoviesLocation': { 'linux': ['/home', user, 'Videos'], 'win32': [processEnvSystemDrive, 'Users', user, 'Videos'], 'darwin': ['/Users', user, 'Movies'] }, 'PicturesLocation': { 'linux': ['/home', user, 'Pictures'], 'win32': [processEnvSystemDrive, 'Users', user, 'Pictures'], 'darwin': ['/Users', user, 'Pictures'] }, 'DownloadLocation': { 'linux': ['/home', user, 'Downloads'], 'win32': [processEnvSystemDrive, 'Users', user, 'Downloads'], 'darwin': ['/Users', user, 'Downloads'] }, 'HomeLocation': { 'linux': ['/home', user], 'win32': [processEnvSystemDrive, 'Users', user], 'darwin': ['/Users', user] }, 'RuntimeLocation': { 'linux': ['/home', user], 'win32': [processEnvSystemDrive, 'Users', user], 'darwin': ['/Users', user] } }; }; return full(user); } data(pathType, user = os.userInfo().username, platform = os.platform()) { const d = this.template(user); const group = d[pathType]; return group ? group[platform] : pathType; } /** * * @param {String} pathType - Masked path eg: "{DocumentsLocation}\Súbory programu Outlook" * @param {String} user * @param {String} platform - win32,darwin,linux * * @returns {String} translated path eg: "C:\Users\Admin\Documents\Súbory programu Outlook" */ from_qt_path(pathType, user = os.userInfo().username, platform = os.platform()) { const startIndex = pathType.indexOf('{') + 1; const endIndex = pathType.indexOf('}') - 1; let part = pathType.substr(startIndex, endIndex); // return original path if subst is not found if(part === '') { return pathType;} if(part === 'RuntimeLocation') { part = 'HomeLocation';} const sep = platform === 'win32' ? path.win32.sep : path.posix.sep; const resolved = this.data(part, user, platform).join(sep); const butcheredPath = pathType.substr(0, startIndex - 1) + resolved + pathType.substr(endIndex + 2); return platform === 'win32' ? butcheredPath.replace(path.posix.sep, path.win32.sep) : butcheredPath; } to_qt_path(pathValue, user = os.userInfo().username, platform = os.platform(), usersHome) { if (typeof usersHome === 'undefined') { usersHome = kc.getHomeDirectory(user, platform); } const sep = platform === 'win32' ? path.win32.sep : path.posix.sep; const t = this.template(user); const keys = Object.keys(t).map(el => { return { path: t[el][platform].join(sep), template: el }; }); // chcem len jeden vysledok preto return [0]; const butchered = keys.filter(key => { return pathValue.startsWith(key.path); }).map(el => { return pathValue.replace(el.path, `{${el.template}}`); })[0]; // return originl path if it can not be transalted eg:no match was found if (typeof butchered === 'undefined') { return pathValue; } return platform === 'win32' ? butchered.replace(path.win32.sep, path.posix.sep) : butchered; } } module.exports = QtPaths;