#!/usr/bin/env node 'use strict' const program = require('commander') const Analytics = require('.') const pkg = require('./package') const toObject = str => JSON.parse(str) program .version(pkg.version) .option('-w, --writeKey ', 'the Segment write key to use') .option('-h, --host ', 'the Segment API hostname to use') .option('-t, --type ', 'the Segment message type') .option('-u, --userId ', 'the user id to send the event as') .option('-a, --anonymousId ', 'the anonymous user id to send the event as') .option('-c, --context ', 'additional context for the event (JSON-encoded)', toObject) .option('-i, --integrations ', 'additional integrations for the event (JSON-encoded)', toObject) .option('-e, --event ', 'the event name to send with the event') .option('-p, --properties ', 'the event properties to send (JSON-encoded)', toObject) .option('-n, --name ', 'name of the screen or page to send with the message') .option('-t, --traits ', 'the identify/group traits to send (JSON-encoded)', toObject) .option('-g, --groupId ', 'the group id') .option('-pid, --previousId ', 'the previous id') .parse(process.argv) if (program.args.length !== 0) { program.help() } const writeKey = program.writeKey const host = program.host const type = program.type const userId = program.userId const anonymousId = program.anonymousId const context = program.context const integrations = program.integrations const event = program.event const properties = program.properties const name = program.name const traits = program.traits const groupId = program.groupId const previousId = program.previousId const run = (method, args) => { const analytics = new Analytics(writeKey, { host, 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) }