From 0909db9f911df11a96d23f1fac76483abe017bb6 Mon Sep 17 00:00:00 2001 From: Pedro Ramos Date: Tue, 4 Jun 2024 11:17:57 +0200 Subject: [PATCH] feat: make analytics config file's path configurable (#1451) --- docs/metrics_collection.md | 5 +++++ src/base.ts | 2 +- src/commands/config/analytics.ts | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/metrics_collection.md b/docs/metrics_collection.md index a27853f898b..3e89f6bddc6 100644 --- a/docs/metrics_collection.md +++ b/docs/metrics_collection.md @@ -39,6 +39,11 @@ We are making use of [New Relic API](https://docs.newrelic.com/docs/apis/intro-a Metrics won't be collected in CI environments, or when the "CI" env variable is set up to "true". +The analytics config file will be store by default at your home directory. In case you prefer to change the file path then you should set the `ASYNCAPI_METRICS_CONFIG_PATH` env var to any specific path value when running any command. For instance: +```` +ASYNCAPI_METRICS_CONFIG_PATH=/tmp/.asyncapi-analytics asyncapi config analytics --status +```` + ## How to disable tracking To disable tracking, please run the following command: `asyncapi config analytics --disable` diff --git a/src/base.ts b/src/base.ts index d5d859b4001..3398df58c74 100644 --- a/src/base.ts +++ b/src/base.ts @@ -98,7 +98,7 @@ export default abstract class extends Command { async recorderFromEnv(prefix: string): Promise { let sink: Sink = new DiscardSink(); - const analyticsConfigFile = join(homedir(), '.asyncapi-analytics'); + const analyticsConfigFile = process.env.ASYNCAPI_METRICS_CONFIG_PATH || join(homedir(), '.asyncapi-analytics'); if (!existsSync(analyticsConfigFile)) { await writeFile(analyticsConfigFile, JSON.stringify({ analyticsEnabled: 'true', infoMessageShown: 'false', userID: uuidv4()}), { encoding: 'utf8' }); diff --git a/src/commands/config/analytics.ts b/src/commands/config/analytics.ts index b8cb1303fbf..d3e74db19eb 100644 --- a/src/commands/config/analytics.ts +++ b/src/commands/config/analytics.ts @@ -18,7 +18,7 @@ export default class Analytics extends Command { async run() { const { flags } = await this.parse(Analytics); - const analyticsConfigFile = join(homedir(), '.asyncapi-analytics'); + const analyticsConfigFile = process.env.ASYNCAPI_METRICS_CONFIG_PATH || join(homedir(), '.asyncapi-analytics'); try { const analyticsConfigFileContent = JSON.parse(await readFile(resolve(analyticsConfigFile), { encoding: 'utf8' })); @@ -45,7 +45,7 @@ export default class Analytics extends Command { } 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 your user's home directory ("${homedir()}") but the file could not be found.`); + 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.`);