1
1
#!/usr/bin/env node
2
+ 'use strict'
2
3
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 )
7
24
8
25
program
9
26
. version ( pkg . version )
@@ -17,9 +34,9 @@ program
17
34
. option ( '-p, --properties <data>' , 'the event properties to send (JSON-encoded)' , toObject )
18
35
. option ( '-t, --timestamp <date>' , 'the date of the event' , toDate )
19
36
. option ( '-c, --context <data>' , 'additional context for the event (JSON-encoded)' , toObject )
20
- . action ( function ( event , options ) {
37
+ . action ( ( event , options ) => {
21
38
run ( 'track' , {
22
- event : event ,
39
+ event,
23
40
userId : options . user ,
24
41
anonymousId : options . anonymous ,
25
42
properties : options . properties ,
@@ -37,7 +54,7 @@ program
37
54
. option ( '-p, --properties <data>' , 'attributes of the page (JSON-encoded)' , toObject )
38
55
. option ( '-t, --timestamp <date>' , 'the date of the event' , toDate )
39
56
. option ( '-c, --context <data>' , 'additional context for the event (JSON-encoded)' , toObject )
40
- . action ( function ( options ) {
57
+ . action ( options => {
41
58
run ( 'page' , {
42
59
userId : options . user ,
43
60
name : options . name ,
@@ -55,7 +72,7 @@ program
55
72
. option ( '-T, --traits <data>' , 'the user traits to send (JSON-encoded)' , toObject )
56
73
. option ( '-t, --timestamp <date>' , 'the date of the event' , toDate )
57
74
. option ( '-c, --context <data>' , 'additional context for the event (JSON-encoded)' , toObject )
58
- . action ( function ( options ) {
75
+ . action ( options => {
59
76
run ( 'identify' , {
60
77
userId : options . user ,
61
78
traits : options . traits ,
@@ -73,7 +90,7 @@ program
73
90
. option ( '-T, --traits <data>' , 'attributes about the group (JSON-encoded)' , toObject )
74
91
. option ( '-t, --timestamp <date>' , 'the date of the event' , toDate )
75
92
. option ( '-c, --context <data>' , 'additional context for the event (JSON-encoded)' , toObject )
76
- . action ( function ( options ) {
93
+ . action ( options => {
77
94
run ( 'group' , {
78
95
userId : options . user ,
79
96
anonymousId : options . anonymous ,
@@ -89,7 +106,7 @@ program
89
106
. description ( 'remap a user to a new id' )
90
107
. option ( '-u, --user <id>' , 'the user id to send the event as' )
91
108
. option ( '-p, --previous <id>' , 'the previous user id (to add the alias for)' )
92
- . action ( function ( options ) {
109
+ . action ( options => {
93
110
run ( 'alias' , {
94
111
userId : options . user ,
95
112
previousId : options . previous
@@ -101,29 +118,3 @@ program.parse(process.argv)
101
118
if ( program . args . length === 0 ) {
102
119
program . help ( )
103
120
}
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
- }
0 commit comments