From 25719bef9e39f3e1126cb27f154d51d6b4db0d7a Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Mon, 9 Dec 2019 16:37:43 +0100 Subject: [PATCH] Generate analytics file with {enabled: false} (#25) --- src/utils/usage-analytics.ts | 7 +++++-- test/init.spec.ts | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/utils/usage-analytics.ts b/src/utils/usage-analytics.ts index fbe896ac..637e8276 100644 --- a/src/utils/usage-analytics.ts +++ b/src/utils/usage-analytics.ts @@ -7,12 +7,15 @@ import * as fs from 'fs'; import * as path from 'path'; export async function usageAnalytics(projectDir: string, agreeToAnalytics: boolean, salt?: string) { + const analyticsFilePath = path.resolve(projectDir, 'sap-cloud-sdk-analytics.json'); if (agreeToAnalytics === false) { - return; + return fs.writeFileSync(analyticsFilePath, JSON.stringify({ enabled: false })); } if (agreeToAnalytics || (await cli.confirm('Do you want to provide anonymous usage analytics to help us improve the SDK? (y|n)'))) { const jsonContent = salt ? { enabled: true, salt } : { enabled: true }; - fs.writeFileSync(path.resolve(projectDir, 'sap-cloud-sdk-analytics.json'), JSON.stringify(jsonContent)); + fs.writeFileSync(analyticsFilePath, JSON.stringify(jsonContent)); + } else { + fs.writeFileSync(analyticsFilePath, JSON.stringify({ enabled: false })); } } diff --git a/test/init.spec.ts b/test/init.spec.ts index 4af3c654..1dff2d7b 100644 --- a/test/init.spec.ts +++ b/test/init.spec.ts @@ -163,4 +163,13 @@ describe('Init', () => { expect(JSON.parse(fs.readFileSync(`${projectDir}/sap-cloud-sdk-analytics.json`, 'utf8'))).toEqual({ enabled: true, salt: 'SAPCLOUDSDK4LIFE' }); }, 60000); + + it('should add a disabled analytics file', async () => { + const projectDir = getCleanProjectDir('add-to-gitignore'); + fs.copySync(expressAppDir, projectDir, { recursive: true }); + + await Init.run(['--projectName=testingApp', '--startCommand="npm start"', `--projectDir=${projectDir}`, '--no-analytics']); + + expect(JSON.parse(fs.readFileSync(`${projectDir}/sap-cloud-sdk-analytics.json`, 'utf8'))).toEqual({ enabled: false }); + }, 60000); });