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
8 changes: 6 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ const subCommands = new Map<string, LazyCommand<any>>([
['migrate', lazy(migrateCommand, migrateMeta)]
]);

cli(process.argv.slice(2), defaultCommand, {
const args = process.argv.slice(2);
const isJsonMode = args[0] === 'analyze' && args.includes('--json');

cli(args, defaultCommand, {
name: 'cli',
version,
description: `${styleText('cyan', 'e18e')}`,
subCommands
subCommands,
renderHeader: isJsonMode ? null : undefined
});
5 changes: 5 additions & 0 deletions src/commands/analyze.meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const meta = {
multiple: true,
description:
'Path(s) to custom manifest file(s) for module replacements analysis'
},
json: {
type: 'boolean',
default: false,
description: 'Output results as JSON to stdout'
}
}
} as const;
30 changes: 23 additions & 7 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ export async function run(ctx: CommandContext<typeof meta>) {
const providedPath =
ctx.positionals.length > 1 ? ctx.positionals[1] : undefined;
const logLevel = ctx.values['log-level'];
let root: string | undefined = undefined;
const jsonOutput = ctx.values['json'];
let root: string | undefined;

// Enable debug output based on log level
if (logLevel === 'debug') {
enableDebug('e18e:*');
}

prompts.intro('Analyzing...');
if (!jsonOutput) {
prompts.intro('Analyzing...');
}

// Path can be a directory (analyze project)
if (providedPath) {
Expand All @@ -56,7 +59,11 @@ export async function run(ctx: CommandContext<typeof meta>) {
}

if (!stat || !stat.isDirectory()) {
prompts.cancel(`Path must be a directory: ${providedPath}`);
if (jsonOutput) {
process.stderr.write(`Path must be a directory: ${providedPath}\n`);
} else {
prompts.cancel(`Path must be a directory: ${providedPath}`);
}
process.exit(1);
}

Expand All @@ -71,6 +78,19 @@ export async function run(ctx: CommandContext<typeof meta>) {
manifest: customManifests
});

const thresholdRank = FAIL_THRESHOLD_RANK[logLevel] ?? 0;
const hasFailingMessages =
thresholdRank > 0 &&
messages.some((m) => SEVERITY_RANK[m.severity] >= thresholdRank);

if (jsonOutput) {
process.stdout.write(JSON.stringify({stats, messages}, null, 2) + '\n');
if (hasFailingMessages) {
process.exit(1);
}
return;
}

prompts.log.info('Summary');

const totalDeps =
Expand Down Expand Up @@ -197,10 +217,6 @@ export async function run(ctx: CommandContext<typeof meta>) {
prompts.outro('Done!');

// Exit with non-zero when messages meet the fail threshold (--log-level)
const thresholdRank = FAIL_THRESHOLD_RANK[logLevel] ?? 0;
const hasFailingMessages =
thresholdRank > 0 &&
messages.some((m) => SEVERITY_RANK[m.severity] >= thresholdRank);
if (hasFailingMessages) {
process.exit(1);
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ describe('analyze exit codes', () => {
});
});

describe('analyze --json', () => {
beforeAll(async () => {
const nodeModules = path.join(basicChalkFixture, 'node_modules');
if (!existsSync(nodeModules)) {
execSync('npm install', {cwd: basicChalkFixture, stdio: 'pipe'});
}
});

it('outputs valid JSON to stdout', async () => {
const {stdout, code} = await runCliProcess(
['analyze', '--json', '--log-level=error'],
tempDir
);
expect(code).toBe(0);
const parsed = JSON.parse(stdout);
expect(parsed).toHaveProperty('stats');
expect(parsed).toHaveProperty('messages');
expect(parsed.stats).toHaveProperty('name', 'mock-package');
expect(parsed.stats).toHaveProperty('version', '1.0.0');
expect(parsed.stats).toHaveProperty('dependencyCount');
expect(Array.isArray(parsed.messages)).toBe(true);
});

it('exits 1 with --json when messages meet fail threshold', async () => {
const {stdout, code} = await runCliProcess(
['analyze', '--json'],
basicChalkFixture
);
expect(code).toBe(1);
const parsed = JSON.parse(stdout);
expect(parsed.messages.length).toBeGreaterThan(0);
});
});

describe('analyze fixable summary', () => {
it('includes fixable-by-migrate summary when project has fixable replacement', async () => {
const {stdout, stderr, code} = await runCliProcess(
Expand Down
Loading