-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
analytics.ts
55 lines (49 loc) · 2.65 KB
/
analytics.ts
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
import { join, resolve } from 'path';
import Command from '../../core/base';
import { promises as fPromises } from 'fs';
import { homedir } from 'os';
import { analyticsFlags } from '../../core/flags/config/analytics.flags';
const { readFile, writeFile } = fPromises;
export default class Analytics extends Command {
static readonly description = 'Enable or disable analytics for metrics collection';
static readonly flags = analyticsFlags();
async run() {
const { flags } = await this.parse(Analytics);
const analyticsConfigFile = process.env.ASYNCAPI_METRICS_CONFIG_PATH || join(homedir(), '.asyncapi-analytics');
try {
const analyticsConfigFileContent = JSON.parse(await readFile(resolve(analyticsConfigFile), { encoding: 'utf8' }));
if (flags.disable) {
analyticsConfigFileContent.analyticsEnabled = 'false';
this.log('\nAnalytics disabled.\n');
this.metricsMetadata.analytics_disabled = flags.disable;
} else if (flags.enable) {
analyticsConfigFileContent.analyticsEnabled = 'true';
this.log('\nAnalytics enabled.\n');
this.metricsMetadata.analytics_enabled = flags.enable;
} else if (!flags.status) {
this.log('\nPlease append the "--disable" flag to the command in case you prefer to disable analytics, or use the "--enable" flag if you want to enable analytics back again. In case you do not know the analytics current status, then you can append the "--status" flag to be aware of it.\n');
return;
}
await writeFile(analyticsConfigFile, JSON.stringify(analyticsConfigFileContent), { encoding: 'utf8' });
if (flags.status) {
if (analyticsConfigFileContent.analyticsEnabled === 'true') {
this.log('\nAnalytics are enabled.\n');
} else {
this.log('\nAnalytics are disabled. Please append the "--enable" flag to the command in case you prefer to enable analytics.\n');
}
this.metricsMetadata.analytics_status_checked = flags.status;
}
} catch (e: any) {
switch (e.code) {
case 'ENOENT':
this.error(`Unable to access the analytics configuration file. We tried to access the ".asyncapi-analytics" file in in the path "${analyticsConfigFile}" but the file could not be found.`);
break;
case 'EEXIST':
this.error(`Unable to update the analytics configuration file. We tried to update your ".asyncapi-analytics" file in the path "${analyticsConfigFile}" but the file does not exist.`);
break;
default:
this.error(`Unable to change your analytics configuration. Please check the following message for further info about the error:\n\n${e}`);
}
}
}
}