diff --git a/README.md b/README.md index ab4af45..4bfaad6 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,9 @@ npx sdp-analyzer analyze react,vue,svelte,audio-analyser-cli ### API Reference ```javascript -const { analyzeNpmPackage } = require("sdp-analyzer"); +const { analyze } = require("sdp-analyzer"); try { - const deps = await sdpAnalyer.analyze("react"); + const deps = await analyze("react"); } catch (error) { // handle error } diff --git a/jest.config.js b/jest.config.js index 682ed5c..6f52e68 100644 --- a/jest.config.js +++ b/jest.config.js @@ -22,7 +22,7 @@ export default { }, __JEST_TEST_ENV__: true, }, - collectCoverageFrom: ['/src/**/*.ts'], + collectCoverageFrom: ['/src/**/*.ts', '!types.ts', '!*.d.ts'], coveragePathIgnorePatterns: ['/dist/', '/node_modules/', '/scripts', '/tools'], coverageProvider: 'v8', coverageReporters: isCI ? ['json'] : ['text'], diff --git a/src/__snapshots__/index.spec.ts.snap b/src/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000..957a233 --- /dev/null +++ b/src/__snapshots__/index.spec.ts.snap @@ -0,0 +1,53 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Main Tests can analyze a valid npm package 1`] = ` +Array [ + Object { + "fanIn": 10000, + "fanOut": 1, + "label": "Stable", + "name": "react", + "stability": 0.00009999000099990002, + }, +] +`; + +exports[`Main Tests can analyze for local packages 1`] = ` +Array [ + Object { + "fanIn": 1, + "fanOut": 0, + "instable": 0, + "label": "Stable", + "name": "a", + }, + Object { + "fanIn": 1, + "fanOut": 1, + "instable": 0.5, + "label": "Normal", + "name": "b", + }, + Object { + "fanIn": 1, + "fanOut": 2, + "instable": 0.6666666666666666, + "label": "Flexible", + "name": "c", + }, + Object { + "fanIn": 1, + "fanOut": 3, + "instable": 0.75, + "label": "Flexible", + "name": "d", + }, + Object { + "fanIn": 1, + "fanOut": 4, + "instable": 0.8, + "label": "Instable", + "name": "e", + }, +] +`; diff --git a/src/cli/index.ts b/src/cli/index.ts index 819894e..b5b2a7e 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1,7 +1,6 @@ #!/usr/bin/env node import { Command } from 'commander'; -import { analyzeNpmPackage, analyzeYarnWorkspaces } from '../index'; -import { isLocalPath } from '../utils/checkLocalPath'; +import { analyze } from '../index'; const program = new Command(); @@ -10,13 +9,7 @@ program .command('analyze') .argument('', 'the local package path or a npm package name') .action(async target => { - let result; - if (isLocalPath(target)) { - // default to yarn workspaces - result = await analyzeYarnWorkspaces(target); - } else { - result = await analyzeNpmPackage(target); - } + const result = await analyze(target); console.log(JSON.stringify(result, null, 2)); process.exit(0); }); diff --git a/src/index.spec.ts b/src/index.spec.ts new file mode 100644 index 0000000..9e04a50 --- /dev/null +++ b/src/index.spec.ts @@ -0,0 +1,14 @@ +/** + * This is a sample test suite. + * Replace this with your implementation. + */ +import { analyze } from './index'; + +describe('Main Tests', () => { + it('can analyze a valid npm package', async () => { + await expect(analyze('react')).resolves.toMatchSnapshot(); + }); + it('can analyze for local packages', async () => { + await expect(analyze('./src/platforms/workspaces/yarn/fixture')).resolves.toMatchSnapshot(); + }); +}); diff --git a/src/index.ts b/src/index.ts index b058d02..6696a55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,12 @@ -export { analyze as analyzeNpmPackage } from './platforms/npm-package'; -export { analyze as analyzeYarnWorkspaces } from './platforms/workspaces/yarn'; +import { isLocalPath } from './utils/checkLocalPath'; + +import { analyze as analyzeNpmPackage } from './platforms/npm-package'; +import { analyze as analyzeYarnWorkspaces } from './platforms/workspaces/yarn'; + +export function analyze(target: string) { + if (isLocalPath(target)) { + // default to yarn workspaces + return analyzeYarnWorkspaces(target); + } + return analyzeNpmPackage(target); +} diff --git a/src/platforms/npm-package/__snapshots__/index.spec.ts.snap b/src/platforms/npm-package/__snapshots__/index.spec.ts.snap index bd597db..7f72fdf 100644 --- a/src/platforms/npm-package/__snapshots__/index.spec.ts.snap +++ b/src/platforms/npm-package/__snapshots__/index.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Npm pacakge Tests should return a dep array for multiple packages 1`] = ` +exports[`Npm pacakge Tests can analyze for multiple packages 1`] = ` Array [ Object { "fanIn": 10000, @@ -19,19 +19,7 @@ Array [ ] `; -exports[`Npm pacakge Tests should return a dep array for single package 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array with the package version 1`] = ` +exports[`Npm pacakge Tests can analyze for multiple packages with the package version 1`] = ` Array [ Object { "fanIn": 10000, @@ -50,7 +38,7 @@ Array [ ] `; -exports[`Npm pacakge Tests should return a dep array for multiple packages 1`] = ` +exports[`Npm pacakge Tests can analyze for single package 1`] = ` Array [ Object { "fanIn": 10000, @@ -59,43 +47,5 @@ Array [ "name": "react", "stability": 0.00009999000099990002, }, - Object { - "fanIn": 10000, - "fanOut": 5, - "label": "Stable", - "name": "vue", - "stability": 0.0004997501249375312, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array for single package 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array with the package version 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, - Object { - "fanIn": 10000, - "fanOut": 5, - "label": "Stable", - "name": "vue", - "stability": 0.0004997501249375312, - }, ] `; diff --git a/src/platforms/npm-package/index.spec.ts b/src/platforms/npm-package/index.spec.ts index 20e1c98..77d7f52 100644 --- a/src/platforms/npm-package/index.spec.ts +++ b/src/platforms/npm-package/index.spec.ts @@ -15,13 +15,13 @@ describe('Npm pacakge Tests', () => { { name: 'loose-envify', version: '^1.1.0' }, ]); }); - it('should return a dep array for single package', async () => { + it('can analyze for single package', async () => { await expect(analyze('react')).resolves.toMatchSnapshot(); }); - it('should return a dep array for multiple packages', async () => { + it('can analyze for multiple packages', async () => { await expect(analyze('react,vue')).resolves.toMatchSnapshot(); }); - it('should return a dep array with the package version', async () => { + it('can analyze for multiple packages with the package version', async () => { await expect(analyze('react@16.0.0,vue@3.0.0')).resolves.toMatchSnapshot(); }); }); diff --git a/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap b/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap index 4722a9d..aa7c506 100644 --- a/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap +++ b/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap @@ -1,45 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Workspaces Tests should be that the package.json can be found 1`] = ` -Array [ - Object { - "dependencies": Object {}, - "name": "a", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - }, - "name": "b", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - }, - "name": "c", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - "c": "^1.0.0", - }, - "name": "d", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - "c": "^1.0.0", - "e": "^1.0.0", - }, - "name": "e", - }, -] -`; - -exports[`Workspaces Tests should return a dep tree 1`] = ` +exports[`Workspaces Tests can analyze for local packages 1`] = ` Array [ Object { "fanIn": 1, @@ -117,43 +78,3 @@ Array [ }, ] `; - -exports[`Workspaces Tests should return a dep tree 1`] = ` -Array [ - Object { - "fanIn": 1, - "fanOut": 0, - "instable": 0, - "label": "Stable", - "name": "a", - }, - Object { - "fanIn": 1, - "fanOut": 1, - "instable": 0.5, - "label": "Normal", - "name": "b", - }, - Object { - "fanIn": 1, - "fanOut": 2, - "instable": 0.6666666666666666, - "label": "Flexible", - "name": "c", - }, - Object { - "fanIn": 1, - "fanOut": 3, - "instable": 0.75, - "label": "Flexible", - "name": "d", - }, - Object { - "fanIn": 1, - "fanOut": 4, - "instable": 0.8, - "label": "Instable", - "name": "e", - }, -] -`; diff --git a/src/platforms/workspaces/yarn/index.spec.ts b/src/platforms/workspaces/yarn/index.spec.ts index 6040acb..46e78b9 100644 --- a/src/platforms/workspaces/yarn/index.spec.ts +++ b/src/platforms/workspaces/yarn/index.spec.ts @@ -13,7 +13,7 @@ describe('Workspaces Tests', () => { it('should be that the package.json can be found', () => { expect(readWorkspacesDependencies('./src/platforms/workspaces/yarn/fixture')).toMatchSnapshot(); }); - it('should return a dep tree', async () => { + it('can analyze for local packages', async () => { await expect(analyze('./src/platforms/workspaces/yarn/fixture')).resolves.toMatchSnapshot(); }); }); diff --git a/src/types.ts b/src/types.ts index 6c222a9..9bdff93 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,6 @@ export interface Dep { stability: number; label: string; } - export interface NpmDependency { name: string; version: string;