From fc74ec187ffaa87c380e261f27f6f3844ba7116b Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Wed, 3 Oct 2018 16:25:20 +0200 Subject: [PATCH] refactor: util file --- lib/index.js | 26 +++++--------------------- lib/metaWrapper.js | 12 ++++++------ lib/utility.js | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/lib/index.js b/lib/index.js index 167942f..f11ed47 100755 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ const DESKTOP_ENV = process.env.DESKTOP_SESSION; const CFG_DATA_DIR = getUserHome() + '/.lwsm'; const CFG_FILE_PATH = CFG_DATA_DIR + '/config.json'; const SESSION_DATA_DIR = CFG_DATA_DIR + '/sessionData'; +const util = require('./utility'); let metaW; @@ -16,24 +17,6 @@ let CFG; // INIT // ------------ -const mkdirSync = (dirPath) => { - try { - fs.mkdirSync(dirPath); - } catch (err) { - if (err.code !== 'EEXIST') { - throw err; - } - } -}; - -const copySync = (src, dest) => { - if (!fs.existsSync(src)) { - return false; - } - const data = fs.readFileSync(src, 'utf-8'); - fs.writeFileSync(dest, data); -}; - try { // if config is already in place CFG = JSON.parse(fs.readFileSync(CFG_FILE_PATH, 'utf8')); @@ -45,8 +28,8 @@ try { CFG.CMD_JSFILE_PATH = __dirname + '/../cmd.js'; CFG.JSFILE_INDEX_PATH = __dirname + '/index.js'; - mkdirSync(CFG_DATA_DIR); - mkdirSync(SESSION_DATA_DIR); + util.mkdirSync(CFG_DATA_DIR); + util.mkdirSync(SESSION_DATA_DIR); // write config to user dir fs.writeFileSync(CFG_FILE_PATH, JSON.stringify(CFG, null, 2), 'utf8'); @@ -276,7 +259,8 @@ function waitForAllAppsToClose() { fulfill(currentWindowList); } }) - .catch(reject);; + .catch(reject); + ; }, CFG.POLL_ALL_APPS_STARTED_INTERVAL); } diff --git a/lib/metaWrapper.js b/lib/metaWrapper.js index 63f0e91..f6de86f 100644 --- a/lib/metaWrapper.js +++ b/lib/metaWrapper.js @@ -3,19 +3,19 @@ let otherCmd; let x11w; let CFG; -let utility; +let utilWithCfg; const waterfall = require('promise-waterfall'); const fs = require('fs'); const path = require('path'); const otherCmdMod = require('./otherCmd'); const x11wMod = require('./x11Wrapper'); -const utilityMod = require('./utility'); +const util = require('./utility'); module.exports = (passedCFG) => { CFG = passedCFG; otherCmd = otherCmdMod(CFG); x11w = x11wMod(CFG); - utility = utilityMod(CFG); + utilWithCfg = util.withCfg(CFG); return { // expose x11 helper @@ -74,7 +74,7 @@ function readAndSetAdditionalMetaDataForWin(win) { } function findDesktopFile(file) { - return utility.findDesktopFile(file); + return utilWithCfg.findDesktopFile(file); } function startProgram(executableFile, desktopFilePath) { @@ -141,13 +141,13 @@ function addParsedExecutableFilesFromWmClassNames(windowList) { }); // TODO replace waterfall with regular exec - if(promises.length){ + if (promises.length) { waterfall(promises) .then(() => { fulfill(windowList); }) .catch(reject); - }else{ + } else { fulfill(windowList); } }).catch(catchGenericErr); diff --git a/lib/utility.js b/lib/utility.js index 5ee0c97..78e7846 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -11,11 +11,14 @@ const DEFAULT_DESKTOP_FILE_LOCATIONS = [ let CFG; -module.exports = (passedCFG) => { - CFG = passedCFG; - return { - findDesktopFile, - }; +module.exports = { + mkdirSync, + mkfileSync, + copySync, + withCfg: (passedCFG) => { + CFG = passedCFG; + return { findDesktopFile }; + } }; function catchGenericErr(err) { @@ -45,3 +48,31 @@ function findDesktopFile(fileName) { } }).catch(catchGenericErr); } + +function mkdirSync(dirPath) { + try { + fs.mkdirSync(dirPath); + } catch (err) { + if (err.code !== 'EEXIST') { + throw err; + } + } +} + +function mkfileSync(dirPath) { + try { + fs.mkdirSync(dirPath); + } catch (err) { + if (err.code !== 'EEXIST') { + throw err; + } + } +} + +function copySync(src, dest) { + if (!fs.existsSync(src)) { + return false; + } + const data = fs.readFileSync(src, 'utf-8'); + fs.writeFileSync(dest, data); +}