Skip to content

Commit

Permalink
Require Node.js 12.20
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
sindresorhus committed Jul 29, 2021
1 parent 8525aa1 commit 3c7c737
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 119 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

44 changes: 23 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chalk = require('chalk');
const debounce = require('lodash.debounce');
const inquirer = require('inquirer');
const uuid = require('uuid');
const providers = require('./providers');
const providers = require('./providers.js');

const DEBOUNCE_MS = 100;

Expand Down Expand Up @@ -40,8 +40,8 @@ class Insight {
this.config = options.config || new Conf({
configName: `insight-${this.packageName}`,
defaults: {
clientId: options.clientId || Math.floor(Date.now() * Math.random())
}
clientId: options.clientId || Math.floor(Date.now() * Math.random()),
},
});
this._queue = {};
this._permissionTimeout = 30;
Expand All @@ -52,16 +52,16 @@ class Insight {
return this.config.get('optOut');
}

set optOut(val) {
this.config.set('optOut', val);
set optOut(value) {
this.config.set('optOut', value);
}

get clientId() {
return this.config.get('clientId');
}

set clientId(val) {
this.config.set('clientId', val);
set clientId(value) {
this.config.set('clientId', value);
}

_save() {
Expand Down Expand Up @@ -90,11 +90,11 @@ class Insight {

_getPayload() {
return {
queue: Object.assign({}, this._queue),
queue: {...this._queue},
packageName: this.packageName,
packageVersion: this.packageVersion,
trackingCode: this.trackingCode,
trackingProvider: this.trackingProvider
trackingProvider: this.trackingProvider,
};
}

Expand All @@ -107,12 +107,12 @@ class Insight {
return;
}

const path = '/' + args.map(el => String(el).trim().replace(/ /, '-')).join('/');
const path = '/' + args.map(element => String(element).trim().replace(/ /, '-')).join('/');

// Timestamp isn't unique enough since it can end up with duplicate entries
this._queue[`${Date.now()} ${uuid.v4()}`] = {
path,
type: 'pageview'
type: 'pageview',
};
this._save();
}
Expand All @@ -136,23 +136,23 @@ class Insight {
action: options.action,
label: options.label,
value: options.value,
type: 'event'
type: 'event',
};
this._save();
}

askPermission(msg) {
const defaultMsg = `May ${chalk.cyan(this.packageName)} anonymously report usage statistics to improve the tool over time?`;
askPermission(message) {
const defaultMessage = `May ${chalk.cyan(this.packageName)} anonymously report usage statistics to improve the tool over time?`;

if (!process.stdout.isTTY || process.argv.indexOf('--no-insight') !== -1 || process.env.CI) {
if (!process.stdout.isTTY || process.argv.includes('--no-insight') || process.env.CI) {
return Promise.resolve();
}

const prompt = inquirer.prompt({
type: 'confirm',
name: 'optIn',
message: msg || defaultMsg,
default: true
message: message || defaultMessage,
default: true,
});

// Set a 30 sec timeout before giving up on getting an answer
Expand All @@ -168,13 +168,15 @@ class Insight {
}, this._permissionTimeout * 1000);
});

const promise = prompt.then(result => {
const promise = (async () => {
const {optIn} = await prompt;

// Clear the permission timeout upon getting an answer
clearTimeout(permissionTimeout);

this.optOut = !result.optIn;
return result.optIn;
});
this.optOut = !optIn;
return optIn;
})();

// Return the result of the prompt if it finishes first otherwise default to the timeout's value.
return Promise.race([promise, timeoutPromise]);
Expand Down
14 changes: 7 additions & 7 deletions lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ module.exports = {
cd3: this.appVersion,

// Queue time - delta (ms) between now and track time
qt: now - parseInt(id, 10),
qt: now - Number.parseInt(id, 10),

// Cache busting, need to be last param sent
z: now
z: now,
};

// Set payload data based on the tracking type
Expand All @@ -64,14 +64,14 @@ module.exports = {
url: 'https://ssl.google-analytics.com/collect',
method: 'POST',
// GA docs recommends body payload via POST instead of querystring via GET
body: qs.stringify(_qs)
body: qs.stringify(_qs),
};
},
// Yandex.Metrica - https://metrica.yandex.com
yandex(id, payload) {
const request = require('request');

const ts = new Date(parseInt(id, 10))
const ts = new Date(Number.parseInt(id, 10))
.toISOString()
.replace(/[-:T]/g, '')
.replace(/\..*$/, '');
Expand All @@ -83,7 +83,7 @@ module.exports = {
'page-url': `http://${this.packageName}.insight${path}?version=${this.packageVersion}`,
'browser-info': `i:${ts}:z:0:t:${path}`,
// Cache busting
rn: Date.now()
rn: Date.now(),
};

const url = `https://mc.yandex.ru/watch/${this.trackingCode}`;
Expand All @@ -98,7 +98,7 @@ module.exports = {
url,
method: 'GET',
qs,
jar: _jar
jar: _jar,
};
}
},
};
22 changes: 11 additions & 11 deletions lib/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ const Insight = require('.');
// Messaged on each debounced `track()`
// Gets the queue, merges is with the previous and tries to upload everything
// If it fails, it will save everything again
process.on('message', msg => {
const insight = new Insight(msg);
process.on('message', message => {
const insight = new Insight(message);
const {config} = insight;
const q = config.get('queue') || {};

Object.assign(q, msg.queue);
Object.assign(q, message.queue);
config.delete('queue');

async.forEachSeries(Object.keys(q), (el, cb) => {
const parts = el.split(' ');
async.forEachSeries(Object.keys(q), (element, cb) => {
const parts = element.split(' ');
const id = parts[0];
const payload = q[el];
const payload = q[element];

request(insight._getRequestObj(id, payload), err => {
if (err) {
cb(err);
request(insight._getRequestObj(id, payload), error => {
if (error) {
cb(error);
return;
}

cb();
});
}, err => {
if (err) {
}, error => {
if (error) {
const q2 = config.get('queue') || {};
Object.assign(q2, q);
config.set('queue', q2);
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"main": "lib",
"engines": {
"node": ">=6"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava --timeout=20s"
},
"xo": {
"ignores": [
"test/fixtures"
]
"rules": {
"unicorn/prefer-module": "off"
}
},
"files": [
"lib"
Expand All @@ -36,20 +36,20 @@
],
"dependencies": {
"async": "^2.6.2",
"chalk": "^2.4.2",
"conf": "^1.4.0",
"chalk": "^4.1.1",
"conf": "^10.0.1",
"inquirer": "^6.3.1",
"lodash.debounce": "^4.0.8",
"os-name": "^4.0.1",
"request": "^2.88.0",
"tough-cookie": "^3.0.1",
"tough-cookie": "^4.0.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^2.4.0",
"execa": "^1.0.0",
"object-values": "^2.0.0",
"sinon": "^7.3.1",
"xo": "^0.24.0"
"xo": "^0.42.0"
}
}
Loading

0 comments on commit 3c7c737

Please sign in to comment.