Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions src/analyze/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {analyzePackageModuleType} from '../compute-type.js';
import type {
ReportPluginResult,
Message,
Expand All @@ -24,29 +23,11 @@ export async function runDependencyAnalysis(
const prodDependencies = Object.keys(pkg.dependencies || {}).length;
const devDependencies = Object.keys(pkg.devDependencies || {}).length;

let cjsDependencies = 0;
let esmDependencies = 0;

// Recursively traverse dependencies
async function traverse(
packagePath: string,
depth: number,
pathInTree: string
) {
async function traverse(packagePath: string, pathInTree: string) {
const depPkg = await getPackageJson(context.fs, packagePath);
if (!depPkg || !depPkg.name) return;

// Only count CJS/ESM for non-root packages
if (depth > 0) {
const type = analyzePackageModuleType(depPkg);
if (type === 'cjs') cjsDependencies++;
if (type === 'esm') esmDependencies++;
if (type === 'dual') {
cjsDependencies++;
esmDependencies++;
}
}

for (const depName of Object.keys(depPkg.dependencies || {})) {
let packageMatch = packageFiles.find((packageFile) =>
normalizePath(packageFile).endsWith(
Expand All @@ -65,23 +46,21 @@ export async function runDependencyAnalysis(
}

if (packageMatch) {
await traverse(packageMatch, depth + 1, pathInTree + ' > ' + depName);
await traverse(packageMatch, pathInTree + ' > ' + depName);
}
}
}

// Start traversal from root
await traverse('/package.json', 0, 'root');
await traverse('/package.json', 'root');

const stats: Partial<Stats> = {
name: pkg.name,
version: pkg.version,
installSize,
dependencyCount: {
production: prodDependencies,
development: devDependencies,
esm: esmDependencies,
cjs: cjsDependencies
development: devDependencies
}
};

Expand Down
4 changes: 1 addition & 3 deletions src/analyze/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export async function report(options: Options) {
version: packageFile.version || '0.0.0',
dependencyCount: {
production: 0,
development: 0,
cjs: 0,
esm: 0
development: 0
},
extraStats: []
};
Expand Down
9 changes: 0 additions & 9 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export async function run(ctx: CommandContext<typeof meta>) {

const totalDeps =
stats.dependencyCount.production + stats.dependencyCount.development;
const totalDeepDeps = stats.dependencyCount.cjs + stats.dependencyCount.esm;
const esmPercentage =
totalDeepDeps > 0
? Math.floor((stats.dependencyCount.esm / totalDeepDeps) * 100)
: 0;
const summaryPairs: Array<[string, string]> = [
['Package Name', stats.name],
['Version', stats.version],
Expand All @@ -77,10 +72,6 @@ export async function run(ctx: CommandContext<typeof meta>) {
[
'Dependencies',
`${totalDeps} (${stats.dependencyCount.production} production, ${stats.dependencyCount.development} development)`
],
[
'ES Modules',
`${esmPercentage}% (${stats.dependencyCount.esm} ESM, ${stats.dependencyCount.cjs} CJS)`
]
];

Expand Down
4 changes: 2 additions & 2 deletions src/test/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ exports[`CLI > should display package report 1`] = `
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
│ Duplicate Dependency Count 0
│
● Results:
Expand All @@ -33,7 +32,6 @@ exports[`CLI > should run successfully with default options 1`] = `
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
│ Duplicate Dependency Count 0
│
● Results:
Expand All @@ -43,3 +41,5 @@ exports[`CLI > should run successfully with default options 1`] = `

"
`;

exports[`CLI > should run successfully with default options 2`] = `""`;
8 changes: 0 additions & 8 deletions src/test/analyze/__snapshots__/dependencies.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ exports[`analyzeDependencies (local) > should analyze dependencies correctly 1`]
"messages": [],
"stats": {
"dependencyCount": {
"cjs": 1,
"development": 1,
"esm": 1,
"production": 2,
},
"installSize": 956,
Expand All @@ -22,9 +20,7 @@ exports[`analyzeDependencies (local) > should handle empty project 1`] = `
"messages": [],
"stats": {
"dependencyCount": {
"cjs": 0,
"development": 0,
"esm": 0,
"production": 0,
},
"installSize": 0,
Expand All @@ -39,9 +35,7 @@ exports[`analyzeDependencies (local) > should handle missing node_modules 1`] =
"messages": [],
"stats": {
"dependencyCount": {
"cjs": 0,
"development": 0,
"esm": 0,
"production": 1,
},
"installSize": 0,
Expand All @@ -56,9 +50,7 @@ exports[`analyzeDependencies (local) > should handle symlinks 1`] = `
"messages": [],
"stats": {
"dependencyCount": {
"cjs": 0,
"development": 0,
"esm": 1,
"production": 1,
},
"installSize": 297,
Expand Down
2 changes: 0 additions & 2 deletions src/test/analyze/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ describe('analyzeDependencies (local)', () => {
name: 'unknown',
version: 'unknown',
dependencyCount: {
cjs: 0,
esm: 0,
production: 0,
development: 0
},
Expand Down
7 changes: 5 additions & 2 deletions src/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const stripVersion = (str: string): string =>
'(cli <version>)'
);

const normalizeStderr = (str: string): string =>
str.replace(/\(node:\d+\)/g, '(node:<pid>)');

beforeAll(async () => {
// Create a temporary directory for the test package
tempDir = await createTempDir();
Expand Down Expand Up @@ -80,13 +83,13 @@ describe('CLI', () => {
}
expect(code).toBe(0);
expect(stripVersion(stdout)).toMatchSnapshot();
expect(stderr).toBe('');
expect(normalizeStderr(stderr)).toMatchSnapshot();
});

it('should display package report', async () => {
const {stdout, stderr, code} = await runCliProcess(['analyze'], tempDir);
expect(code).toBe(0);
expect(stripVersion(stdout)).toMatchSnapshot();
expect(stderr).toMatchSnapshot();
expect(normalizeStderr(stderr)).toMatchSnapshot();
});
});
2 changes: 0 additions & 2 deletions src/test/custom-manifests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ describe('Custom Manifests', () => {
name: 'unknown',
version: 'unknown',
dependencyCount: {
cjs: 0,
esm: 0,
production: 0,
development: 0
},
Expand Down
8 changes: 2 additions & 6 deletions src/test/duplicate-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ describe('Duplicate Dependency Detection', () => {
version: 'unknown',
dependencyCount: {
production: 0,
development: 0,
esm: 0,
cjs: 0
development: 0
},
extraStats: []
},
Expand Down Expand Up @@ -158,9 +156,7 @@ describe('Duplicate Dependency Detection', () => {
version: 'unknown',
dependencyCount: {
production: 0,
development: 0,
esm: 0,
cjs: 0
development: 0
},
extraStats: []
},
Expand Down
2 changes: 1 addition & 1 deletion src/test/plugin-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fsMock: FileSystem = {
fileExists: async () => false
};

const depCounts = {production: 0, development: 0, cjs: 0, esm: 0, duplicate: 0};
const depCounts = {production: 0, development: 0};

describe('runPlugins', () => {
it('should aggregate messages and merge stats with extraStats de-dup', async () => {
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export interface Stats {
dependencyCount: {
production: number;
development: number;
cjs: number;
esm: number;
};
extraStats?: Stat[];
}
Expand Down
Loading