Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* @module CLI
*/

const EventEmitter = require('events').EventEmitter
const EventEmitter = require('events')
const GA = require('universal-analytics')
const ArgumentParser = require('argparse').ArgumentParser
const reduce = require('lodash.reduce')
const pkg = require('../package.json')
let Spike = require('spike-core') // this is a let because of test mocking
let Spike = require('spike-core') // mocked in tests
let analytics

/**
* @class CLI
Expand All @@ -17,6 +19,9 @@ module.exports = class CLI extends EventEmitter {
constructor () {
super()

const uid = Spike.globalConfig().id // mocked in tests
analytics = new GA('UA-79663181-1', uid, { strictCidFormat: false })

this.parser = new ArgumentParser({
version: pkg.version,
description: pkg.description,
Expand All @@ -41,13 +46,16 @@ module.exports = class CLI extends EventEmitter {
if (typeof args === 'string') { args = args.split(' ') }
args = this.parser.parseArgs(args)

const fn = require(`./${args.fn}`)
delete args.fn

// argparse uses `null` which doesn't get along with joi, so we remove
// null values here
args = reduce(args, (m, v, k) => { if (v) m[k] = v; return m }, {})

// anonymous analytics for usage data
analytics.event({ ec: 'cmd', ea: args.fn, ev: JSON.stringify(args) }).send()

const fn = require(`./${args.fn}`)
delete args.fn

// all CLI modules must return an event emitter of some sort
// they should also wait to execute anything until nextTick
const emitter = fn(Spike, args)
Expand All @@ -59,7 +67,10 @@ module.exports = class CLI extends EventEmitter {
emitter.on('info', this.emit.bind(this, 'info'))

// instance-method-specific
emitter.on('compile', this.emit.bind(this, 'compile'))
emitter.on('compile', (res) => {
analytics.event({ ec: 'core', ea: 'compile' }).send()
this.emit('compile', res)
})
emitter.on('remove', this.emit.bind(this, 'success'))

return this
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"chalk": "^1.1.3",
"inquirer": "^1.0.2",
"lodash.reduce": "^4.4.0",
"spike-core": "^0.8.0"
"spike-core": "^0.8.0",
"universal-analytics": "^0.3.11"
},
"devDependencies": {
"ava": "^0.15.2",
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ module.exports = {
Since the two configuration files are _merged_, you don't lose all your other settings from the `app.js` file, it just merges in any new ones from `app.production.js`. Very amaze!

To change the environment, just pass `--env name` or `-e name` as an argument to the `compile` or `watch` commands.

### Analytics

Spike's CLI uses basic analytics in order to get more data about how developers are using Spike, so that we can improve this tool more accurately. Analytics are entirely anonymous and the information we collect is minimal.
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ SpikeMock.template = {
}
}

SpikeMock.globalConfig = () => {
return { id: 'TEST' }
}

test.beforeEach((t) => {
CLI.__set__('Spike', SpikeMock)
cli = new CLI()
Expand Down