Skip to content

Commit 7f76171

Browse files
author
Vadim Demedes
authored
Convert to ES6 (segmentio#110)
1 parent 96466d8 commit 7f76171

File tree

5 files changed

+227
-224
lines changed

5 files changed

+227
-224
lines changed

bin/analytics

+27-36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/usr/bin/env node
2+
'use strict'
23

3-
var Analytics = require('..')
4-
var assert = require('assert')
5-
var pkg = require('../package.json')
6-
var program = require('commander')
4+
const assert = require('assert')
5+
const program = require('commander')
6+
const Analytics = require('..')
7+
const pkg = require('../package')
8+
9+
const run = (method, options) => {
10+
const writeKey = process.env.SEGMENT_WRITE_KEY || program.writeKey
11+
assert(writeKey, 'You need to define your write key via the $SEGMENT_WRITE_KEY environment variable or the --write-key flag.')
12+
13+
const analytics = new Analytics(writeKey, { flushAt: 1 })
14+
analytics[method](options, err => {
15+
if (err) {
16+
console.error(err.stack)
17+
process.exit(1)
18+
}
19+
})
20+
}
21+
22+
const toDate = str => new Date(str)
23+
const toObject = str => JSON.parse(str)
724

825
program
926
.version(pkg.version)
@@ -17,9 +34,9 @@ program
1734
.option('-p, --properties <data>', 'the event properties to send (JSON-encoded)', toObject)
1835
.option('-t, --timestamp <date>', 'the date of the event', toDate)
1936
.option('-c, --context <data>', 'additional context for the event (JSON-encoded)', toObject)
20-
.action(function (event, options) {
37+
.action((event, options) => {
2138
run('track', {
22-
event: event,
39+
event,
2340
userId: options.user,
2441
anonymousId: options.anonymous,
2542
properties: options.properties,
@@ -37,7 +54,7 @@ program
3754
.option('-p, --properties <data>', 'attributes of the page (JSON-encoded)', toObject)
3855
.option('-t, --timestamp <date>', 'the date of the event', toDate)
3956
.option('-c, --context <data>', 'additional context for the event (JSON-encoded)', toObject)
40-
.action(function (options) {
57+
.action(options => {
4158
run('page', {
4259
userId: options.user,
4360
name: options.name,
@@ -55,7 +72,7 @@ program
5572
.option('-T, --traits <data>', 'the user traits to send (JSON-encoded)', toObject)
5673
.option('-t, --timestamp <date>', 'the date of the event', toDate)
5774
.option('-c, --context <data>', 'additional context for the event (JSON-encoded)', toObject)
58-
.action(function (options) {
75+
.action(options => {
5976
run('identify', {
6077
userId: options.user,
6178
traits: options.traits,
@@ -73,7 +90,7 @@ program
7390
.option('-T, --traits <data>', 'attributes about the group (JSON-encoded)', toObject)
7491
.option('-t, --timestamp <date>', 'the date of the event', toDate)
7592
.option('-c, --context <data>', 'additional context for the event (JSON-encoded)', toObject)
76-
.action(function (options) {
93+
.action(options => {
7794
run('group', {
7895
userId: options.user,
7996
anonymousId: options.anonymous,
@@ -89,7 +106,7 @@ program
89106
.description('remap a user to a new id')
90107
.option('-u, --user <id>', 'the user id to send the event as')
91108
.option('-p, --previous <id>', 'the previous user id (to add the alias for)')
92-
.action(function (options) {
109+
.action(options => {
93110
run('alias', {
94111
userId: options.user,
95112
previousId: options.previous
@@ -101,29 +118,3 @@ program.parse(process.argv)
101118
if (program.args.length === 0) {
102119
program.help()
103120
}
104-
105-
function run (method, options) {
106-
var writeKey = process.env.SEGMENT_WRITE_KEY || program.writeKey
107-
assert(writeKey, 'you need to define your write key via the $SEGMENT_WRITE_KEY environment variable or the --write-key flag')
108-
109-
var analytics = new Analytics(writeKey, { flushAt: 1 })
110-
111-
analytics[method](options, done)
112-
}
113-
114-
function toDate (str) {
115-
return new Date(str)
116-
}
117-
118-
function toObject (str) {
119-
return JSON.parse(str)
120-
}
121-
122-
function done (err) {
123-
if (err) {
124-
console.error(err.stack)
125-
process.exit(1)
126-
} else {
127-
process.exit(0)
128-
}
129-
}

circle.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ machine:
55
dependencies:
66
pre:
77
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.21.3
8-
- nvm install 4.1
98
- nvm install 4
109
- nvm install 6
10+
- nvm install 8
1111
cache_directories:
1212
- ~/.yarn-cache
1313
override:
@@ -17,6 +17,6 @@ test:
1717
override:
1818
- make lint
1919
- node_modules/.bin/nsp check
20-
- nvm use 4.1; npm rebuild > /dev/null; make test
2120
- nvm use 4; npm rebuild > /dev/null; make test
2221
- nvm use 6; npm rebuild > /dev/null; make test
22+
- nvm use 8; npm rebuild > /dev/null; make test

0 commit comments

Comments
 (0)