angular-cn/tools/analytics/build-analytics

52 lines
1.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
'use strict';
var analytics = require('./analytics');
var fs = require('fs');
var path = require('path');
var eventType = process.argv[2];
var actionCategory = process.argv[3];
var actionName = process.argv[4];
if (!analytics[actionCategory + 'Start']) {
throw new Error('Unknown build-analytics actionCategory "' + actionCategory + '"');
}
if (eventType != 'start' && eventType != 'success' && eventType != 'error') {
throw new Error('Unknown build-analytics eventType "' + eventType + '"');
}
var startTimestampFilePath = path.resolve(path.join(__dirname, '..', '..', 'tmp', 'analytics', actionCategory + '-' + actionName));
var analyticsDirPath = path.dirname(startTimestampFilePath);
var tmpDirPath = path.dirname(analyticsDirPath);
if (!fs.existsSync(tmpDirPath)) {
fs.mkdirSync(tmpDirPath);
}
if (!fs.existsSync(analyticsDirPath)) {
fs.mkdirSync(analyticsDirPath);
}
switch (eventType) {
case 'start':
analytics[actionCategory + 'Start'](actionName);
fs.writeFileSync(startTimestampFilePath, Date.now(), 'utf-8');
break;
case 'success':
var startTime = fs.readFileSync(startTimestampFilePath, 'utf-8');
analytics[actionCategory + 'Success'](actionName, Date.now() - startTime);
fs.unlinkSync(startTimestampFilePath);
break;
case 'error':
var startTime = fs.readFileSync(startTimestampFilePath, 'utf-8');
analytics[actionCategory + 'Error'](actionName, Date.now() - startTime);
fs.unlinkSync(startTimestampFilePath);
}