diff --git a/index.js b/index.js index f6e5287..128e6ab 100644 --- a/index.js +++ b/index.js @@ -4,29 +4,56 @@ const chromeFinder = require('chrome-launcher/dist/src/chrome-finder'); const chromeUtils = require('chrome-launcher/dist/src/utils'); const parser = require('xml2js'); +function getMajorFromVersion(version) { + return +version.split('.')[0]; +} + function getChromeMajorVersion() { const promise = new Promise((resolve, reject) => { const platform = chromeUtils.getPlatform(); - const installations = chromeFinder[platform](); + console.log('Platform', platform); + console.log('Chrome Installations', installations); + if (installations && installations.length > 0) { - const source = spawn( - installations[0], - [ - '--version' - ] - ); - - source.stdout.on('data', (data) => { - const chromeVersion = data.toString().trim().substr('Google Chrome '.length); - const chromeMajorVersion = +chromeVersion.split('.')[0]; - - resolve({ - chromeVersion: chromeVersion, - chromeMajorVersion: chromeMajorVersion + if (platform === 'win32') { + + const source = spawn('wmic'); + source.stdin.end( + `datafile where "name='${installations[0].replace(/\\/g, '\\\\')}'" get version /value` + ); + + source.stdout.on('close', () => reject('Unable to get Chrome version from wmic (close)')); + source.stderr.on('data', () => reject('Unable to get Chrome version from wmic (err)')); + source.stdout.on('data', (data) => { + + const dataCleaned = data.toString().trim().toLowerCase(); + if (dataCleaned.indexOf('version=') > -1) { + const chromeVersion = dataCleaned.split('version=')[1].split('wmic')[0].replace(/\r?\n|\r/g, ''); + resolve({ + chromeVersion: chromeVersion, + chromeMajorVersion: getMajorFromVersion(chromeVersion) + }); + } }); - }); + + } else { + + const source = spawn(installations[0], ['--version']); + source.stdout.on('close', () => reject('Unable to get Chrome version (close)')); + source.stderr.on('data', () => reject('Unable to get Chrome version (err)')); + source.stdout.on('data', (data) => { + const chromeVersion = data.toString() + .trim().substr('Google Chrome '.length).split(' ')[0]; + resolve({ + chromeVersion: chromeVersion, + chromeMajorVersion: getMajorFromVersion(chromeVersion) + }); + }); + + } + } else { reject('No local installation of Chrome was found.'); } @@ -38,7 +65,7 @@ function getChromeMajorVersion() { function getLatestChromeDriverVersion() { return new Promise((resolve, reject) => { request( - 'https://chromedriver.storage.googleapis.com/', + 'https://chromedriver.storage.googleapis.com/', (error, response, body) => { if (error) { reject(error); @@ -57,18 +84,18 @@ function getLatestChromeDriverVersion() { .sort((a, b) => { const aMinorVersion = +a.split('.')[1]; const bMinorVersion = +b.split('.')[1]; - + if (aMinorVersion < bMinorVersion) { return 1; } - + if (bMinorVersion < aMinorVersion) { return -1; } - + return 0; }); - + resolve(versionNumbers[0]); } }); @@ -81,7 +108,7 @@ function getLatestChromeDriverVersion() { function getReleaseNotes(latestChromeDriverVersion) { return new Promise((resolve, reject) => { request( - 'https://chromedriver.storage.googleapis.com/' + + 'https://chromedriver.storage.googleapis.com/' + latestChromeDriverVersion + '/notes.txt', (error, response, body) => { @@ -101,7 +128,7 @@ function findVersionInReleaseNotes(releaseNotes, chromeMajorVersion) { // Supports Chrome v69-71 const lines = releaseNotes.split('\n'); - + for (let i = 0, n = lines.length; i < n; i++) { const line = lines[i]; @@ -123,11 +150,11 @@ function findVersionInReleaseNotes(releaseNotes, chromeMajorVersion) { } function getChromeDriverVersion() { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { Promise.all( [ getChromeMajorVersion(), - getLatestChromeDriverVersion() + getLatestChromeDriverVersion() ] ) .then((results) => { @@ -139,11 +166,15 @@ function getChromeDriverVersion() { resolve({ chromeVersion: chromeVersion.chromeVersion, chromeDriverVersion: findVersionInReleaseNotes( - releaseNotes, + releaseNotes, chromeVersion.chromeMajorVersion ) }); }); + }) + .catch((err) => { + console.log(err); + reject(err); }); }); } @@ -152,4 +183,5 @@ module.exports = { getChromeDriverVersion: getChromeDriverVersion }; -// getChromeDriverVersion().then(result => console.log(result)); \ No newline at end of file +// USAGE: +// getChromeDriverVersion().then(result => console.log(result));