/
insight.js
87 lines (74 loc) · 2.59 KB
/
insight.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
var fs = require('fs'),
join = require('path').join,
exec = require('child_process').exec,
colors = require('colors'),
prompt = require('prompt');
/**
* Records the give cmd to Insight.
*
* It's assumed yeomaninsight.py is installed globally as
* /usr/local/bin/_yeomaninsight (or platform equivalent). A full cmd would
* might look like: _yeomaninsight -n yeoman -v 0.0.1 record cmd cmd2
*
* @param {string} cmd The full command line that was run.
* @param {Function} callback Callback to call when Insight is done.
*/
function invokeInsight(cmd, callback) {
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.log(stderr);
return callback(err);
}
callback();
});
}
module.exports = {
init: function(opts) {
var insight = {
// Store records in user's home dir (e.g. /Users/username/.yeoman/insight)
logFile: join(opts.getUserHome(), '.' + opts.pkgname, 'insight', '.log'),
script: '_' + opts.pkgname + 'insight',
recordArgs: ['-n', opts.pkgname, '-v', opts.pkgversion, 'record'],
record: invokeInsight
};
// Example: _yeomaninsight -n yeoman -v 0.9.4 record cmd cmd2.
var cmdStr = insight.script + ' ' +
insight.recordArgs.concat(opts.cmds).join(' ');
fs.stat(insight.logFile, function(err, stats) {
// Error means file doesn't exist and this is the first run.
// Go through stat opt-in flow.
if (!err) {
return insight.record(cmdStr, opts.cb);
}
/*jshint multistr:true */
var msg = "\
==========================================================================".grey + "\n\
We're constantly looking for ways to make ".yellow + opts.pkgname.bold.red + " better! \n\
May we anonymously report usage statistics to improve the tool over time? \n\
More info: yeoman.io/insight.html & http://yeoman.io".yellow + "\n\
==========================================================================".grey;
prompt.message = '[' + '?'.green + ']';
prompt.delimiter = ' ';
var properties = [{
name: 'optin',
message: '[Y/n]: ',
"default": 'Y',
validator: /^[yntf]{1}/i,
empty: false
}];
prompt.start();
console.log(msg);
prompt.get(properties, function(err, result) {
if (err) {
return opts.cb(err);
}
if (/n/i.test(result.optin)) {
cmdStr = insight.script + ' ' +
insight.recordArgs.concat(['NO_STATS']).join(' ');
return insight.record(cmdStr, opts.cb);
}
insight.record(cmdStr, opts.cb);
});
});
}
};