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

dep!: replace os-name with systeminformation #84

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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');
}
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
22 changes: 15 additions & 7 deletions test/providers-google-analytics.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion test/providers-yandex-metrica.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down