Skip to content

Commit

Permalink
refactor: util file
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Oct 3, 2018
1 parent ea47550 commit fc74ec1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
26 changes: 5 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
Expand All @@ -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');
Expand Down Expand Up @@ -276,7 +259,8 @@ function waitForAllAppsToClose() {
fulfill(currentWindowList);
}
})
.catch(reject);;
.catch(reject);
;
}, CFG.POLL_ALL_APPS_STARTED_INTERVAL);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/metaWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,7 +74,7 @@ function readAndSetAdditionalMetaDataForWin(win) {
}

function findDesktopFile(file) {
return utility.findDesktopFile(file);
return utilWithCfg.findDesktopFile(file);
}

function startProgram(executableFile, desktopFilePath) {
Expand Down Expand Up @@ -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);
Expand Down
41 changes: 36 additions & 5 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

0 comments on commit fc74ec1

Please sign in to comment.