diff --git a/lib/index.js b/lib/index.js index 807c581..bf18790 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,12 +1,12 @@ 'use strict'; const path = require('path'); const childProcess = require('child_process'); -const osName = require('os-name'); const Conf = require('conf'); const chalk = require('chalk'); const debounce = require('lodash.debounce'); const inquirer = require('inquirer'); const uuid = require('uuid'); +const {osInfo} = require('systeminformation'); const providers = require('./providers.js'); const DEBOUNCE_MS = 100; @@ -34,7 +34,7 @@ class Insight { this.trackingProvider = options.trackingProvider || 'google'; this.packageName = options.pkg.name; this.packageVersion = options.pkg.version || 'undefined'; - this.os = osName(); + this.os = undefined; this.nodeVersion = process.version; this.appVersion = this.packageVersion; this.config = options.config || new Conf({ @@ -48,6 +48,13 @@ class Insight { this._debouncedSend = debounce(this._send, DEBOUNCE_MS, {leading: true}); } + async _appendOSData() { + const data = await osInfo(); + this.os = process.platform === 'darwin' + ? data.codename + : data.distro; + } + get optOut() { return this.config.get('optOut'); } @@ -98,7 +105,11 @@ class Insight { }; } - _getRequestObj(...args) { + async _getRequestObj(...args) { + if (this.os === undefined) { + await this._appendOSData(); + } + return providers[this.trackingProvider].apply(this, args); } diff --git a/lib/push.js b/lib/push.js index 966a723..7a3b11f 100644 --- a/lib/push.js +++ b/lib/push.js @@ -14,12 +14,12 @@ process.on('message', message => { Object.assign(q, message.queue); config.delete('queue'); - async.forEachSeries(Object.keys(q), (element, cb) => { + async.forEachSeries(Object.keys(q), async (element, cb) => { const parts = element.split(' '); const id = parts[0]; const payload = q[element]; - request(insight._getRequestObj(id, payload), error => { + request(await insight._getRequestObj(id, payload), error => { if (error) { cb(error); return; diff --git a/package.json b/package.json index 7254e86..a4dac67 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,8 @@ "conf": "^10.0.1", "inquirer": "^6.3.1", "lodash.debounce": "^4.0.8", - "os-name": "^4.0.1", "request": "^2.88.0", + "systeminformation": "^5.17.4", "tough-cookie": "^4.0.0", "uuid": "^8.3.2" }, diff --git a/test/providers-google-analytics.js b/test/providers-google-analytics.js index 0bca5d4..fd5a800 100644 --- a/test/providers-google-analytics.js +++ b/test/providers-google-analytics.js @@ -1,5 +1,5 @@ import qs from 'querystring'; // eslint-disable-line unicorn/prefer-node-protocol, no-restricted-imports -import osName from 'os-name'; +import {osInfo} from 'systeminformation'; import test from 'ava'; import Insight from '../lib/index.js'; @@ -25,20 +25,28 @@ const insight = new Insight({ packageVersion: ver, }); -test('form valid request for pageview', t => { - const requestObject = insight._getRequestObj(ts, pageviewPayload); +const osName = async () => { + const osData = await osInfo(); + return process.platform === 'darwin' + ? osData.codename + : osData.distro; +}; + +test('form valid request for pageview', async t => { + const requestObject = await insight._getRequestObj(ts, pageviewPayload); const _qs = qs.parse(requestObject.body); t.is(_qs.tid, code); t.is(Number(_qs.cid), Number(insight.clientId)); t.is(_qs.dp, pageviewPayload.path); - t.is(_qs.cd1, osName()); + + t.is(_qs.cd1, await osName()); t.is(_qs.cd2, process.version); t.is(_qs.cd3, ver); }); -test('form valid request for eventTracking', t => { - const requestObject = insight._getRequestObj(ts, eventPayload); +test('form valid request for eventTracking', async t => { + const requestObject = await insight._getRequestObj(ts, eventPayload); const _qs = qs.parse(requestObject.body); t.is(_qs.tid, code); @@ -47,7 +55,7 @@ test('form valid request for eventTracking', t => { t.is(_qs.ea, eventPayload.action); t.is(_qs.el, eventPayload.label); t.is(_qs.ev, eventPayload.value); - t.is(_qs.cd1, osName()); + t.is(_qs.cd1, await osName()); t.is(_qs.cd2, process.version); t.is(_qs.cd3, ver); }); diff --git a/test/providers-yandex-metrica.js b/test/providers-yandex-metrica.js index bfdf895..f1b1f9f 100644 --- a/test/providers-yandex-metrica.js +++ b/test/providers-yandex-metrica.js @@ -21,7 +21,7 @@ test('form valid request', async t => { const request = require('request'); // Test querystrings - const requestObject = insight._getRequestObj(ts, pageviewPayload); + const requestObject = await insight._getRequestObj(ts, pageviewPayload); const _qs = requestObject.qs; t.is(_qs['page-url'], `http://${pkg}.insight/test/path?version=${ver}`);