diff --git a/packages/platform-core/src/lock-file-reader/lock_file_reader_factory.test.ts b/packages/platform-core/src/lock-file-reader/lock_file_reader_factory.test.ts index 5b03d01a89..197480cafb 100644 --- a/packages/platform-core/src/lock-file-reader/lock_file_reader_factory.test.ts +++ b/packages/platform-core/src/lock-file-reader/lock_file_reader_factory.test.ts @@ -49,7 +49,7 @@ void describe('LockFileReaderFactory', () => { }); } - void it('should throw an error for unsupport package managers', () => { + void it('should throw an error for unsupported package managers', () => { process.env.npm_config_user_agent = 'unsupported/1.0.0 node/v15.0.0 darwin x64'; assert.throws(() => new LockFileReaderFactory().getLockFileReader(), { diff --git a/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.test.ts b/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.test.ts index d7fa27cb64..91fdbbf07b 100644 --- a/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.test.ts +++ b/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.test.ts @@ -5,7 +5,6 @@ import path from 'path'; import { NpmLockFileReader } from './npm_lock_file_reader'; void describe('NpmLockFileReader', () => { - const fspAccessMock = mock.method(fsp, 'access', () => true); const fspReadFileMock = mock.method(fsp, 'readFile', () => JSON.stringify({ name: 'test_project', @@ -47,37 +46,18 @@ void describe('NpmLockFileReader', () => { }; assert.deepEqual(lockFileContents, expectedLockFileContents); assert.strictEqual( - fspAccessMock.mock.calls[0].arguments[0], + fspReadFileMock.mock.calls[0].arguments[0], path.resolve(process.cwd(), 'package-lock.json') ); assert.strictEqual(fspReadFileMock.mock.callCount(), 1); }); - void it('throws when package-lock.json is not present', async () => { - fspAccessMock.mock.mockImplementationOnce(() => + void it('returns empty lock file contents when package-lock.json is not present or parse-able', async () => { + fspReadFileMock.mock.mockImplementationOnce(() => Promise.reject(new Error()) ); - await assert.rejects( - () => npmLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok( - error.message.startsWith('Could not find a package-lock.json file') - ); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 0); - }); - - void it('throws when package-lock.json is not parse-able', async () => { - fspReadFileMock.mock.mockImplementationOnce(() => 'not json content'); - await assert.rejects( - () => npmLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not parse the contents')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 1); + const lockFileContents = + await npmLockFileReader.getLockFileContentsFromCwd(); + assert.deepEqual(lockFileContents, { dependencies: [] }); }); }); diff --git a/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.ts b/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.ts index 4ded6cc122..fa3e56aa8f 100644 --- a/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.ts +++ b/packages/platform-core/src/lock-file-reader/npm_lock_file_reader.ts @@ -1,7 +1,6 @@ import fsp from 'fs/promises'; import path from 'path'; import z from 'zod'; -import { AmplifyUserError } from '../errors'; import { Dependencies, LockFileContents, @@ -13,40 +12,24 @@ import { */ export class NpmLockFileReader implements LockFileReader { getLockFileContentsFromCwd = async (): Promise => { + const dependencies: Dependencies = []; const packageLockJsonPath = path.resolve( process.cwd(), 'package-lock.json' ); - try { - await fsp.access(packageLockJsonPath); - } catch (error) { - throw new AmplifyUserError('InvalidPackageLockJsonError', { - message: `Could not find a package-lock.json file at ${packageLockJsonPath}`, - resolution: `Ensure that ${packageLockJsonPath} exists and is a valid JSON file`, - }); - } - let jsonLockParsedValue: Record; try { const jsonLockContents = await fsp.readFile(packageLockJsonPath, 'utf-8'); jsonLockParsedValue = JSON.parse(jsonLockContents); } catch (error) { - throw new AmplifyUserError( - 'InvalidPackageLockJsonError', - { - message: `Could not parse the contents of ${packageLockJsonPath}`, - resolution: `Ensure that ${packageLockJsonPath} exists and is a valid JSON file`, - }, - error as Error - ); + // We failed to get lock file contents either because file doesn't exist or it is not parse-able + return { dependencies }; } // This will strip fields that are not part of the package lock schema const packageLockJson = packageLockJsonSchema.parse(jsonLockParsedValue); - const dependencies: Dependencies = []; - for (const key in packageLockJson.packages) { if (key === '') { // Skip root project in packages diff --git a/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.test.ts b/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.test.ts index e2bda902df..3fef3d88c3 100644 --- a/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.test.ts +++ b/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.test.ts @@ -5,10 +5,10 @@ import path from 'path'; import { PnpmLockFileReader } from './pnpm_lock_file_reader'; void describe('PnpmLockFileReader', () => { - const fspAccessMock = mock.method(fsp, 'access', () => true); const fspReadFileMock = mock.method( fsp, 'readFile', + // eslint-disable-next-line spellcheck/spell-checker () => `lockfileVersion: '9.0' settings: @@ -81,39 +81,18 @@ packages: }; assert.deepEqual(lockFileContents, expectedLockFileContents); assert.strictEqual( - fspAccessMock.mock.calls[0].arguments[0], + fspReadFileMock.mock.calls[0].arguments[0], path.resolve(process.cwd(), 'pnpm-lock.yaml') ); assert.strictEqual(fspReadFileMock.mock.callCount(), 1); }); - void it('throws when pnpm-lock.yaml is not present', async () => { - fspAccessMock.mock.mockImplementationOnce(() => - Promise.reject(new Error()) - ); - await assert.rejects( - () => pnpmLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok( - error.message.startsWith('Could not find a pnpm-lock.yaml file') - ); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 0); - }); - - void it('throws when pnpm-lock.yaml is not parse-able', async () => { + void it('returns empty lock file contents when pnpm-lock.yaml is not present or parse-able', async () => { fspReadFileMock.mock.mockImplementationOnce(() => Promise.reject(new Error()) ); - await assert.rejects( - () => pnpmLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not parse the contents')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 1); + const lockFileContents = + await pnpmLockFileReader.getLockFileContentsFromCwd(); + assert.deepEqual(lockFileContents, { dependencies: [] }); }); }); diff --git a/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.ts b/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.ts index f437b503ca..0323bc58dc 100644 --- a/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.ts +++ b/packages/platform-core/src/lock-file-reader/pnpm_lock_file_reader.ts @@ -6,25 +6,14 @@ import { LockFileContents, LockFileReader, } from './lock_file_reader_factory'; -import { AmplifyUserError } from '../errors'; /** * PnpmLockFileReader is an abstraction around the logic used to read and parse lock file contents */ export class PnpmLockFileReader implements LockFileReader { getLockFileContentsFromCwd = async (): Promise => { - const pnpmLockPath = path.resolve(process.cwd(), 'pnpm-lock.yaml'); - - try { - await fsp.access(pnpmLockPath); - } catch (error) { - throw new AmplifyUserError('InvalidPackageLockJsonError', { - message: `Could not find a pnpm-lock.yaml file at ${pnpmLockPath}`, - resolution: `Ensure that ${pnpmLockPath} exists and is a valid pnpm-lock.yaml file`, - }); - } - const dependencies: Dependencies = []; + const pnpmLockPath = path.resolve(process.cwd(), 'pnpm-lock.yaml'); try { const pnpmLockContents = await fsp.readFile(pnpmLockPath, 'utf-8'); @@ -52,14 +41,8 @@ export class PnpmLockFileReader implements LockFileReader { dependencies.push({ name: dependencyName, version: dependencyVersion }); } } catch (error) { - throw new AmplifyUserError( - 'InvalidPackageLockJsonError', - { - message: `Could not parse the contents of ${pnpmLockPath}`, - resolution: `Ensure that ${pnpmLockPath} exists and is a valid pnpm-lock.yaml file`, - }, - error as Error - ); + // We failed to get lock file contents either because file doesn't exist or it is not parse-able + return { dependencies }; } return { dependencies }; diff --git a/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.test.ts b/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.test.ts index 027fa36097..2e82d58807 100644 --- a/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.test.ts +++ b/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.test.ts @@ -5,7 +5,6 @@ import path from 'path'; import { YarnClassicLockFileReader } from './yarn_classic_lock_file_reader'; void describe('YarnClassicLockFileReader', () => { - const fspAccessMock = mock.method(fsp, 'access', () => true); const fspReadFileMock = mock.method( fsp, 'readFile', @@ -51,37 +50,18 @@ some_other_dep@12.13.14: }; assert.deepEqual(lockFileContents, expectedLockFileContents); assert.strictEqual( - fspAccessMock.mock.calls[0].arguments[0], + fspReadFileMock.mock.calls[0].arguments[0], path.resolve(process.cwd(), 'yarn.lock') ); assert.strictEqual(fspReadFileMock.mock.callCount(), 1); }); - void it('throws when yarn.lock is not present', async () => { - fspAccessMock.mock.mockImplementationOnce(() => - Promise.reject(new Error()) - ); - await assert.rejects( - () => yarnClassicLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not find a yarn.lock file')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 0); - }); - - void it('throws when yarn.lock is not parse-able', async () => { + void it('returns empty lock file contents when yarn.lock is not present or parse-able', async () => { fspReadFileMock.mock.mockImplementationOnce(() => Promise.reject(new Error()) ); - await assert.rejects( - () => yarnClassicLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not parse the contents')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 1); + const lockFileContents = + await yarnClassicLockFileReader.getLockFileContentsFromCwd(); + assert.deepEqual(lockFileContents, { dependencies: [] }); }); }); diff --git a/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.ts b/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.ts index c3a6c87d1a..d01f9ecc7b 100644 --- a/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.ts +++ b/packages/platform-core/src/lock-file-reader/yarn_classic_lock_file_reader.ts @@ -6,25 +6,14 @@ import { LockFileContents, LockFileReader, } from './lock_file_reader_factory'; -import { AmplifyUserError } from '../errors'; /** * YarnClassicLockFileReader is an abstraction around the logic used to read and parse lock file contents */ export class YarnClassicLockFileReader implements LockFileReader { getLockFileContentsFromCwd = async (): Promise => { - const yarnLockPath = path.resolve(process.cwd(), 'yarn.lock'); - - try { - await fsp.access(yarnLockPath); - } catch (error) { - throw new AmplifyUserError('InvalidPackageLockJsonError', { - message: `Could not find a yarn.lock file at ${yarnLockPath}`, - resolution: `Ensure that ${yarnLockPath} exists and is a valid yarn.lock file`, - }); - } - const dependencies: Dependencies = []; + const yarnLockPath = path.resolve(process.cwd(), 'yarn.lock'); try { const yarnLockContents = await fsp.readFile(yarnLockPath, 'utf-8'); @@ -46,14 +35,8 @@ export class YarnClassicLockFileReader implements LockFileReader { dependencies.push({ name: dependencyName, version: dependencyVersion }); } } catch (error) { - throw new AmplifyUserError( - 'InvalidPackageLockJsonError', - { - message: `Could not parse the contents of ${yarnLockPath}`, - resolution: `Ensure that ${yarnLockPath} exists and is a valid yarn.lock file`, - }, - error as Error - ); + // We failed to get lock file contents either because file doesn't exist or it is not parse-able + return { dependencies }; } return { dependencies }; diff --git a/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.test.ts b/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.test.ts index cf00d43fd8..e7c575b30f 100644 --- a/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.test.ts +++ b/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.test.ts @@ -5,7 +5,6 @@ import path from 'path'; import { YarnModernLockFileReader } from './yarn_modern_lock_file_reader'; void describe('YarnModernLockFileReader', () => { - const fspAccessMock = mock.method(fsp, 'access', () => true); const fspReadFileMock = mock.method( fsp, 'readFile', @@ -58,37 +57,18 @@ __metadata: }; assert.deepEqual(lockFileContents, expectedLockFileContents); assert.strictEqual( - fspAccessMock.mock.calls[0].arguments[0], + fspReadFileMock.mock.calls[0].arguments[0], path.resolve(process.cwd(), 'yarn.lock') ); assert.strictEqual(fspReadFileMock.mock.callCount(), 1); }); - void it('throws when yarn.lock is not present', async () => { - fspAccessMock.mock.mockImplementationOnce(() => - Promise.reject(new Error()) - ); - await assert.rejects( - () => yarnModernLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not find a yarn.lock file')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 0); - }); - - void it('throws when yarn.lock is not parse-able', async () => { + void it('returns empty lock file contents when yarn.lock is not present or parse-able', async () => { fspReadFileMock.mock.mockImplementationOnce(() => Promise.reject(new Error()) ); - await assert.rejects( - () => yarnModernLockFileReader.getLockFileContentsFromCwd(), - (error: Error) => { - assert.ok(error.message.startsWith('Could not parse the contents')); - return true; - } - ); - assert.strictEqual(fspReadFileMock.mock.callCount(), 1); + const lockFileContents = + await yarnModernLockFileReader.getLockFileContentsFromCwd(); + assert.deepEqual(lockFileContents, { dependencies: [] }); }); }); diff --git a/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.ts b/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.ts index aa0bd5f859..b5082b1bb3 100644 --- a/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.ts +++ b/packages/platform-core/src/lock-file-reader/yarn_modern_lock_file_reader.ts @@ -6,25 +6,14 @@ import { LockFileContents, LockFileReader, } from './lock_file_reader_factory'; -import { AmplifyUserError } from '../errors'; /** * YarnModernLockFileReader is an abstraction around the logic used to read and parse lock file contents */ export class YarnModernLockFileReader implements LockFileReader { getLockFileContentsFromCwd = async (): Promise => { - const yarnLockPath = path.resolve(process.cwd(), 'yarn.lock'); - - try { - await fsp.access(yarnLockPath); - } catch (error) { - throw new AmplifyUserError('InvalidPackageLockJsonError', { - message: `Could not find a yarn.lock file at ${yarnLockPath}`, - resolution: `Ensure that ${yarnLockPath} exists and is a valid yarn.lock file`, - }); - } - const dependencies: Dependencies = []; + const yarnLockPath = path.resolve(process.cwd(), 'yarn.lock'); try { const yarnLockContents = await fsp.readFile(yarnLockPath, 'utf-8'); @@ -46,14 +35,8 @@ export class YarnModernLockFileReader implements LockFileReader { dependencies.push({ name: dependencyName, version: dependencyVersion }); } } catch (error) { - throw new AmplifyUserError( - 'InvalidPackageLockJsonError', - { - message: `Could not parse the contents of ${yarnLockPath}`, - resolution: `Ensure that ${yarnLockPath} exists and is a valid yarn.lock file`, - }, - error as Error - ); + // We failed to get lock file contents either because file doesn't exist or it is not parse-able + return { dependencies }; } return { dependencies }; diff --git a/packages/platform-core/src/usage-data/dependency_version_fetcher.test.ts b/packages/platform-core/src/usage-data/dependency_version_fetcher.test.ts new file mode 100644 index 0000000000..2764b20326 --- /dev/null +++ b/packages/platform-core/src/usage-data/dependency_version_fetcher.test.ts @@ -0,0 +1,75 @@ +import assert from 'assert'; +import { describe, it } from 'node:test'; +import { DependencyVersionFetcher } from './dependency_version_fetcher'; +import { LockFileReader } from '../lock-file-reader/lock_file_reader_factory'; + +void describe('getDependencyVersions', () => { + void it('successfully returns dependency versions', async () => { + const lockFileReaderMock = { + getLockFileContentsFromCwd: async () => + Promise.resolve({ + dependencies: [ + { + name: 'aws-cdk', + version: '1.2.3', + }, + { + name: 'aws-cdk-lib', + version: '12.13.14', + }, + { + name: 'test_dep', + version: '1.23.45', + }, + { + name: 'some_other_dep', + version: '12.1.3', + }, + ], + }), + } as LockFileReader; + const dependencyVersionFetcher = new DependencyVersionFetcher( + lockFileReaderMock + ); + const dependencyVersions = + await dependencyVersionFetcher.getDependencyVersions(); + const expectedVersions = [ + { + name: 'aws-cdk', + version: '1.2.3', + }, + { + name: 'aws-cdk-lib', + version: '12.13.14', + }, + ]; + + assert.deepEqual(dependencyVersions, expectedVersions); + }); + + void it('returns empty array if there are no matching dependencies', async () => { + const lockFileReaderMock = { + getLockFileContentsFromCwd: async () => + Promise.resolve({ + dependencies: [ + { + name: 'test_dep', + version: '1.23.45', + }, + { + name: 'some_other_dep', + version: '12.1.3', + }, + ], + }), + } as LockFileReader; + + const dependencyVersionFetcher = new DependencyVersionFetcher( + lockFileReaderMock + ); + const dependencyVersions = + await dependencyVersionFetcher.getDependencyVersions(); + + assert.deepEqual(dependencyVersions, []); + }); +}); diff --git a/packages/platform-core/src/usage-data/dependency_version_fetcher.ts b/packages/platform-core/src/usage-data/dependency_version_fetcher.ts new file mode 100644 index 0000000000..6d47e19e04 --- /dev/null +++ b/packages/platform-core/src/usage-data/dependency_version_fetcher.ts @@ -0,0 +1,33 @@ +import { + Dependencies, + LockFileReader, + LockFileReaderFactory, +} from '../lock-file-reader/lock_file_reader_factory'; + +/** + * Get dependency versions from customer project's lock file + */ +export class DependencyVersionFetcher { + /** + * Creates dependency version fetcher + */ + constructor( + private readonly lockFileReader: LockFileReader = new LockFileReaderFactory().getLockFileReader() + ) {} + + getDependencyVersions = async () => { + const lockFileContents = + await this.lockFileReader.getLockFileContentsFromCwd(); + const targetDependencies = ['aws-cdk', 'aws-cdk-lib']; + + const dependencyVersions: Dependencies = []; + + for (const { name, version } of lockFileContents.dependencies) { + if (targetDependencies.includes(name)) { + dependencyVersions.push({ name, version }); + } + } + + return dependencyVersions; + }; +} diff --git a/packages/platform-core/src/usage-data/get_dependency_versions.test.ts b/packages/platform-core/src/usage-data/get_dependency_versions.test.ts deleted file mode 100644 index fb726664b4..0000000000 --- a/packages/platform-core/src/usage-data/get_dependency_versions.test.ts +++ /dev/null @@ -1,61 +0,0 @@ -import assert from 'assert'; -import { describe, it } from 'node:test'; -import { getDependencyVersions } from './get_dependency_versions'; -import { LockFileContents } from '../lock-file-reader/lock_file_reader_factory'; - -void describe('getDependencyVersions', () => { - void it('successfully returns dependency versions', () => { - const lockFileContents: LockFileContents = { - dependencies: [ - { - name: 'aws-cdk', - version: '1.2.3', - }, - { - name: 'aws-cdk-lib', - version: '12.13.14', - }, - { - name: 'test_dep', - version: '1.23.45', - }, - { - name: 'some_other_dep', - version: '12.1.3', - }, - ], - }; - - const dependencyVersions = getDependencyVersions(lockFileContents); - const expectedVersions = [ - { - name: 'aws-cdk', - version: '1.2.3', - }, - { - name: 'aws-cdk-lib', - version: '12.13.14', - }, - ]; - - assert.deepEqual(dependencyVersions, expectedVersions); - }); - - void it('returns empty array if there are no matching dependencies', () => { - const lockFileContents: LockFileContents = { - dependencies: [ - { - name: 'test_dep', - version: '1.23.45', - }, - { - name: 'some_other_dep', - version: '12.1.3', - }, - ], - }; - - const dependencyVersions = getDependencyVersions(lockFileContents); - assert.deepEqual(dependencyVersions, []); - }); -}); diff --git a/packages/platform-core/src/usage-data/get_dependency_versions.ts b/packages/platform-core/src/usage-data/get_dependency_versions.ts deleted file mode 100644 index 6f3bb380b7..0000000000 --- a/packages/platform-core/src/usage-data/get_dependency_versions.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { - Dependencies, - LockFileContents, -} from '../lock-file-reader/lock_file_reader_factory'; - -/** - * Get dependency versions from customer project's lock file - */ -export const getDependencyVersions = (lockFileContents: LockFileContents) => { - const targetDependencies = ['aws-cdk', 'aws-cdk-lib']; - - const dependencyVersions: Dependencies = []; - - for (const { name, version } of lockFileContents.dependencies) { - if (targetDependencies.includes(name)) { - dependencyVersions.push({ name, version }); - } - } - - return dependencyVersions; -}; diff --git a/packages/platform-core/src/usage-data/usage_data_emitter.test.ts b/packages/platform-core/src/usage-data/usage_data_emitter.test.ts index 1e08b86e6c..730ef161a6 100644 --- a/packages/platform-core/src/usage-data/usage_data_emitter.test.ts +++ b/packages/platform-core/src/usage-data/usage_data_emitter.test.ts @@ -12,6 +12,7 @@ import { UsageData } from './usage_data'; import isCI from 'is-ci'; import { AmplifyError, AmplifyUserError } from '..'; import { LockFileReader } from '../lock-file-reader/lock_file_reader_factory'; +import { DependencyVersionFetcher } from './dependency_version_fetcher'; const originalNpmUserAgent = process.env.npm_config_user_agent; const testNpmUserAgent = 'testNpmUserAgent'; @@ -41,7 +42,7 @@ void describe('UsageDataEmitter', () => { fetch: async () => '123456789012', } as AccountIdFetcher; - // For LockFileReader + // For DependencyVersionFetcher const lockFileReaderMock = { getLockFileContentsFromCwd: async () => Promise.resolve({ @@ -57,6 +58,9 @@ void describe('UsageDataEmitter', () => { ], }), } as LockFileReader; + const dependencyVersionFetcherMock = new DependencyVersionFetcher( + lockFileReaderMock + ); mock.method(https, 'request', () => reqMock); @@ -176,7 +180,7 @@ void describe('UsageDataEmitter', () => { v4(), testURL, accountIdFetcherMock, - lockFileReaderMock + dependencyVersionFetcherMock ); let usageDataEmitterPromise; diff --git a/packages/platform-core/src/usage-data/usage_data_emitter.ts b/packages/platform-core/src/usage-data/usage_data_emitter.ts index 40109bfd52..92b871b8ef 100644 --- a/packages/platform-core/src/usage-data/usage_data_emitter.ts +++ b/packages/platform-core/src/usage-data/usage_data_emitter.ts @@ -10,8 +10,7 @@ import isCI from 'is-ci'; import { SerializableError } from './serializable_error.js'; import { UsageDataEmitter } from './usage_data_emitter_factory.js'; import { AmplifyError } from '../index.js'; -import { getDependencyVersions } from './get_dependency_versions.js'; -import { LockFileReaderFactory } from '../lock-file-reader/lock_file_reader_factory.js'; +import { DependencyVersionFetcher } from './dependency_version_fetcher.js'; /** * Entry point for sending usage data metrics @@ -25,7 +24,7 @@ export class DefaultUsageDataEmitter implements UsageDataEmitter { private readonly sessionUuid = uuid(), private readonly url = getUrl(), private readonly accountIdFetcher = new AccountIdFetcher(), - private readonly lockFileReader = new LockFileReaderFactory().getLockFileReader() + private readonly dependencyVersionFetcher = new DependencyVersionFetcher() ) {} emitSuccess = async ( @@ -68,9 +67,6 @@ export class DefaultUsageDataEmitter implements UsageDataEmitter { dimensions?: Record; error?: AmplifyError; }): Promise => { - const lockFileContents = - await this.lockFileReader.getLockFileContentsFromCwd(); - const dependencyVersions = getDependencyVersions(lockFileContents); return { accountId: await this.accountIdFetcher.fetch(), sessionUuid: this.sessionUuid, @@ -94,7 +90,9 @@ export class DefaultUsageDataEmitter implements UsageDataEmitter { isCi: isCI, projectSetting: { editor: process.env.npm_config_user_agent, - details: JSON.stringify(dependencyVersions), + details: JSON.stringify( + await this.dependencyVersionFetcher.getDependencyVersions() + ), }, }; };