From bb380c10639a323ca6fbaa47bf718d970ea0f3fe Mon Sep 17 00:00:00 2001 From: Pedro Ramos Date: Thu, 2 May 2024 14:09:10 +0200 Subject: [PATCH] feat: add '--status' flag to `analytics` command (#1386) --- src/commands/config/analytics.ts | 18 ++++++++++++++---- test/integration/config/analytics.test.ts | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/commands/config/analytics.ts b/src/commands/config/analytics.ts index e9c1fdaf224..b8cb1303fbf 100644 --- a/src/commands/config/analytics.ts +++ b/src/commands/config/analytics.ts @@ -12,6 +12,8 @@ export default class Analytics extends Command { help: Flags.help({ char: 'h' }), disable: Flags.boolean({ char: 'd', description: 'disable analytics', default: false }), enable: Flags.boolean({ char: 'e', description: 'enable analytics', default: false }), + status: Flags.boolean({ char: 's', description: 'show current status of analytics' }), + }; async run() { @@ -23,15 +25,23 @@ export default class Analytics extends Command { if (flags.disable) { analyticsConfigFileContent.analyticsEnabled = 'false'; - this.log('Analytics disabled.'); + this.log('\nAnalytics disabled.\n'); } else if (flags.enable) { analyticsConfigFileContent.analyticsEnabled = 'true'; - this.log('Analytics enabled.'); - } else { - 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.\n'); + this.log('\nAnalytics enabled.\n'); + } 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'); + } + } } catch (e: any) { switch (e.code) { case 'ENOENT': diff --git a/test/integration/config/analytics.test.ts b/test/integration/config/analytics.test.ts index 80471fbb78b..4d95d641e08 100644 --- a/test/integration/config/analytics.test.ts +++ b/test/integration/config/analytics.test.ts @@ -7,7 +7,7 @@ describe('config:analytics', () => { .stdout() .command(['config:analytics', '--disable']) .it('should show a successful message once the analytics are disabled', async (ctx, done) => { - expect(ctx.stdout).to.equal('Analytics disabled.\n'); + expect(ctx.stdout).to.equal('\nAnalytics disabled.\n\n'); expect(ctx.stderr).to.equal(''); done(); }); @@ -19,7 +19,7 @@ describe('config:analytics', () => { .stdout() .command(['config:analytics', '--enable']) .it('should show a successful message once the analytics are enabled', (ctx, done) => { - expect(ctx.stdout).to.equal('Analytics enabled.\n'); + expect(ctx.stdout).to.equal('\nAnalytics enabled.\n\n'); expect(ctx.stderr).to.equal(''); done(); }); @@ -31,7 +31,19 @@ describe('config:analytics', () => { .stdout() .command(['config:analytics']) .it('should show informational message when no flags are used', (ctx, done) => { - expect(ctx.stdout).to.equal('\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.\n\n'); + expect(ctx.stdout).to.equal('\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\n'); + expect(ctx.stderr).to.equal(''); + done(); + }); + }); + + describe('with status flag', () => { + test + .stderr() + .stdout() + .command(['config:analytics', '--status']) + .it('should show a different informational message depending on the analytics status', (ctx, done) => { + expect(ctx.stdout).to.contain('\nAnalytics are '); expect(ctx.stderr).to.equal(''); done(); });