Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added basic docs and cleaned up code
  • Loading branch information
Hi-Ray committed Nov 11, 2019
commit eaa54bb910c60aa307bbaf30c1aea1f45acf3f44
55 changes: 46 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ const IS_WIN = process.platform === 'win32';
const IS_MAC = process.platform === 'darwin';

class LCUConnector extends EventEmitter {

/**
* Gets the lcu path from a running instance.
* @returns {Promise<unknown>}
*/
static getLCUPathFromProcess() {
return new Promise(resolve => {
const INSTALL_REGEX_WIN = /"--install-directory=(.*?)"/;
const INSTALL_REGEX_MAC = /--install-directory=(.*?)( --|\n|$)/;
const INSTALL_REGEX = IS_WIN ? INSTALL_REGEX_WIN : INSTALL_REGEX_MAC;
const command = IS_WIN ?
`WMIC PROCESS WHERE name='LeagueClientUx.exe' GET commandline` :
`ps x -o args | grep 'LeagueClientUx'`;
const command = IS_WIN
? 'WMIC PROCESS WHERE name=\'LeagueClientUx.exe\' GET commandline'
: 'ps x -o args | grep \'LeagueClientUx\'';

cp.exec(command, (err, stdout, stderr) => {
if (err || !stdout || stderr) {
Expand All @@ -32,6 +35,12 @@ class LCUConnector extends EventEmitter {
});
}

/**
* Checks if the path given is a valid lcu path.
*
* @param dirPath
* @returns {*|boolean|boolean}
*/
static isValidLCUPath(dirPath) {
if (!dirPath) {
return false;
Expand All @@ -41,11 +50,13 @@ class LCUConnector extends EventEmitter {
const common = fs.existsSync(path.join(dirPath, lcuClientApp)) && fs.existsSync(path.join(dirPath, 'Config'));
const isGlobal = common && fs.existsSync(path.join(dirPath, 'RADS'));
const isCN = common && fs.existsSync(path.join(dirPath, 'TQM'));
const isGarena = common; // Garena has no other

return isGlobal || isCN || isGarena;
// Garena has no other
return isGlobal || isCN || common;
}

/**
* @param {string} [executablePath]
*/
constructor(executablePath) {
super();

Expand All @@ -54,6 +65,9 @@ class LCUConnector extends EventEmitter {
}
}

/**
* Start the lcu-connector
*/
start() {
if (LCUConnector.isValidLCUPath(this._dirPath)) {
this._initLockfileWatcher();
Expand All @@ -63,11 +77,17 @@ class LCUConnector extends EventEmitter {
this._initProcessWatcher();
}

/**
* Stop the lcu-connector
*/
stop() {
this._clearProcessWatcher();
this._clearLockfileWatcher();
}

/**
* @private
*/
_initLockfileWatcher() {
if (this._lockfileWatcher) {
return;
Expand All @@ -81,12 +101,19 @@ class LCUConnector extends EventEmitter {
this._lockfileWatcher.on('unlink', this._onFileRemoved.bind(this));
}

/**
* @private
*/
_clearLockfileWatcher() {
if (this._lockfileWatcher) {
this._lockfileWatcher.close();
}
}

/**
* @returns {Promise<unknown>}
* @private
*/
_initProcessWatcher() {
return LCUConnector.getLCUPathFromProcess().then(lcuPath => {
if (lcuPath) {
Expand All @@ -102,12 +129,19 @@ class LCUConnector extends EventEmitter {
});
}

/**
* @private
*/
_clearProcessWatcher() {
clearInterval(this._processWatcher);
}

_onFileCreated(path) {
lockfile.read(path).then(data => {
/**
* @param filePath
* @private
*/
_onFileCreated(filePath) {
lockfile.read(filePath).then(data => {
const result = {
protocol: data.protocol,
address: '127.0.0.1',
Expand All @@ -120,6 +154,9 @@ class LCUConnector extends EventEmitter {
});
}

/**
* @private
*/
_onFileRemoved() {
this.emit('disconnect');
}
Expand Down