Skip to content
Open
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
20 changes: 14 additions & 6 deletions src/analyze/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,27 @@ export async function runDependencyAnalysis(
stats.dependencyCount.duplicate = duplicateDependencies.length;

for (const duplicate of duplicateDependencies) {
const severityColor =
duplicate.severity === 'exact' ? colors.blue : colors.yellow;

let message = `${severityColor('[duplicate dependency]')} ${colors.bold(duplicate.name)} has ${duplicate.versions.length} installed versions:`;
// Disable colors completely when NO_COLOR is set (for consistent test snapshots)
const noColor = process.env.NO_COLOR === '1';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

this feels like a workaround rather than a necessity.

are you doing this because we didn't have any snapshots yet which use colours? we should just set FORCE_COLOR to 1 in the vitest config env (although vitest 4 might already do this)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it was a workaround cause i couldn't make the tests work properly even after updating the snapshots.

const severityColor = noColor
? (str: string) => str
: duplicate.severity === 'exact'
? colors.blue
: colors.yellow;
const bold = noColor ? (str: string) => str : colors.bold;
const gray = noColor ? (str: string) => str : colors.gray;
const blue = noColor ? (str: string) => str : colors.blue;

let message = `${severityColor('[duplicate dependency]')} ${bold(duplicate.name)} has ${duplicate.versions.length} installed versions:`;

for (const version of duplicate.versions) {
message += `\n ${colors.gray(version.version)} via ${colors.gray(version.path)}`;
message += `\n ${gray(version.version)} via ${gray(version.path)}`;
}

if (duplicate.suggestions && duplicate.suggestions.length > 0) {
message += '\nSuggestions:';
for (const suggestion of duplicate.suggestions) {
message += ` ${colors.blue('💡')} ${colors.gray(suggestion)}`;
message += ` ${blue('💡')} ${gray(suggestion)}`;
}
}

Expand Down
35 changes: 29 additions & 6 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,38 @@ export async function run(ctx: CommandContext<typeof meta.args>) {
root = providedPath;
}

// Check if package.json exists in the root directory
const packageJsonPath = root ? `${root}/package.json` : './package.json';

try {
await fsp.access(packageJsonPath);
} catch {
prompts.cancel(
c.red(
`No package.json found in ${root || 'the current directory'}. Make sure you are in a package directory before running this command.`
)
);
process.exit(1);
}

// Then analyze the tarball
const customManifests = ctx.values['manifest'];

const {stats, messages} = await report({
root,
manifest: customManifests,
baseTarball,
targetTarball
});
let stats, messages;
try {
const result = await report({
root,
manifest: customManifests,
baseTarball,
targetTarball
});
stats = result.stats;
messages = result.messages;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
prompts.cancel(c.red(`Analysis failed: ${errorMessage}`));
process.exit(1);
}

prompts.log.info('Summary');

Expand Down
56 changes: 28 additions & 28 deletions src/test/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`CLI > should display package report 1`] = `
"e18e (cli <version>)
"e18e (cli <version>)

┌ Analyzing...
│
● Summary
│ Package Name mock-package
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
│
● Results:
│
│
└ Done!
Analyzing...
Summary
│ Package Name mock-package
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
Results:
Done!

"
`;

exports[`CLI > should display package report 2`] = `""`;

exports[`CLI > should run successfully with default options 1`] = `
"e18e (cli <version>)
"e18e (cli <version>)

┌ Analyzing...
│
● Summary
│ Package Name mock-package
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
│
● Results:
│
│
└ Done!
Analyzing...
Summary
│ Package Name mock-package
│ Version 1.0.0
│ Install Size 53.0 B
│ Dependencies 1 (1 production, 0 development)
│ ES Modules 100% (1 ESM, 0 CJS)
Results:
Done!

"
`;
26 changes: 13 additions & 13 deletions src/test/__snapshots__/duplicate-dependencies.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ exports[`Duplicate Dependency Detection > should detect exact duplicate dependen
{
"messages": [
{
"message": "[duplicate dependency] shared-lib has 2 installed versions:
2.0.0 via root > package-a > shared-lib
2.0.0 via root > package-b > shared-lib
Suggestions: 💡 Consider standardizing on version 2.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"message": "[duplicate dependency] shared-lib has 2 installed versions:
2.0.0 via root > package-a > shared-lib
2.0.0 via root > package-b > shared-lib
Suggestions: 💡 Consider standardizing on version 2.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"score": 0,
"severity": "warning",
},
Expand All @@ -31,11 +31,11 @@ exports[`Duplicate Dependency Detection > should detect version conflicts 1`] =
{
"messages": [
{
"message": "[duplicate dependency] shared-lib has 3 installed versions:
1.0.0 via root > package-a > shared-lib
1.0.0 via root > package-b > shared-lib
2.0.0 via shared-lib
Suggestions: 💡 Consider standardizing on version 1.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"message": "[duplicate dependency] shared-lib has 3 installed versions:
1.0.0 via root > package-a > shared-lib
1.0.0 via root > package-b > shared-lib
2.0.0 via shared-lib
Suggestions: 💡 Consider standardizing on version 1.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"score": 0,
"severity": "warning",
},
Expand All @@ -59,10 +59,10 @@ exports[`Duplicate Dependency Detection > should generate suggestions for duplic
{
"messages": [
{
"message": "[duplicate dependency] shared-lib has 2 installed versions:
2.0.0 via root > package-a > shared-lib
2.0.0 via root > package-b > shared-lib
Suggestions: 💡 Consider standardizing on version 2.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"message": "[duplicate dependency] shared-lib has 2 installed versions:
2.0.0 via root > package-a > shared-lib
2.0.0 via root > package-b > shared-lib
Suggestions: 💡 Consider standardizing on version 2.0.0 (used by 2 dependencies) 💡 Check if newer versions of consuming packages (package-a, package-b) would resolve this duplicate",
"score": 0,
"severity": "warning",
},
Expand Down
6 changes: 5 additions & 1 deletion src/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ function runCliProcess(
): Promise<{stdout: string; stderr: string; code: number | null}> {
return new Promise((resolve) => {
const cliPath = path.resolve(__dirname, '../../lib/cli.js');
// Set NO_COLOR to ensure consistent plain text output for snapshots
// Remove FORCE_COLOR as it overrides NO_COLOR
const {FORCE_COLOR, ...envWithoutForceColor} = process.env;
const env = {...envWithoutForceColor, NO_COLOR: '1'};
const proc = spawn('node', [cliPath, ...args], {
env: process.env,
env,
cwd: cwd || process.cwd()
});
let stdout = '';
Expand Down
2 changes: 1 addition & 1 deletion src/test/tarball-file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function createTarballBuffer(cwd: string): Promise<ArrayBuffer> {
return buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
);
) as ArrayBuffer;
}

describe('TarballFileSystem', () => {
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
],
test: {
env: {
FORCE_COLOR: '1'
NO_COLOR: '1'
},
reporters: 'dot',
include: [
Expand Down
Loading