-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·128 lines (117 loc) · 2.75 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
const program = require('commander');
const Analytics = require('.');
const pkg = require('./package');
const toObject = (str) => JSON.parse(str);
// node cli.js -w "write-key" -d "http://localhost" -t "identify" -u 'id'
program
.version(pkg.version)
.option('-w, --writeKey <key>', 'the write key to use')
.option('-d, --dataPlaneUrl <dataPlaneUrl>', 'the API hostname to use')
.option('-t, --type <type>', 'the message type')
.option('-u, --userId <id>', 'the user id to send the event as')
.option('-a, --anonymousId <id>', 'the anonymous user id to send the event as')
.option('-c, --context <context>', 'additional context for the event (JSON-encoded)', toObject)
.option(
'-i, --integrations <integrations>',
'additional integrations for the event (JSON-encoded)',
toObject,
)
.option('-e, --event <event>', 'the event name to send with the event')
.option('-p, --properties <properties>', 'the event properties to send (JSON-encoded)', toObject)
.option('-n, --name <name>', 'name of the screen or page to send with the message')
.option('-t, --traits <traits>', 'the identify/group traits to send (JSON-encoded)', toObject)
.option('-g, --groupId <groupId>', 'the group id')
.option('-pid, --previousId <previousId>', 'the previous id')
.parse(process.argv);
if (program.args.length !== 0) {
program.help();
}
const options = program.opts();
const {
writeKey,
dataPlaneUrl,
type,
userId,
anonymousId,
context,
integrations,
event,
properties,
name,
traits,
groupId,
previousId,
} = options;
const run = (method, args) => {
const analytics = new Analytics(writeKey, { dataPlaneUrl, flushAt: 1 });
analytics[method](args, (err) => {
if (err) {
console.error(err.stack);
process.exit(1);
}
});
};
switch (type) {
case 'track':
run('track', {
event,
properties,
userId,
anonymousId,
context,
integrations,
});
break;
case 'page':
run('page', {
name,
properties,
userId,
anonymousId,
context,
integrations,
});
break;
case 'screen':
run('screen', {
name,
properties,
userId,
anonymousId,
context,
integrations,
});
break;
case 'identify':
run('identify', {
traits,
userId,
anonymousId,
context,
integrations,
});
break;
case 'group':
run('group', {
groupId,
traits,
userId,
anonymousId,
context,
integrations,
});
break;
case 'alias':
run('alias', {
previousId,
userId,
anonymousId,
context,
integrations,
});
break;
default:
console.error('invalid type:', type);
process.exit(1);
}