Skip to content

Commit 6e7aba1

Browse files
committed
feat: Add json reporter
1 parent 5656e4f commit 6e7aba1

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

new.codify.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/commands/plan/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default class Plan extends BaseCommand {
1313
]
1414

1515
async init(): Promise<void> {
16-
console.log('Running Codify plan...')
1716
return super.init();
1817
}
1918

src/common/base-command.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import { Reporter, ReporterFactory, ReporterType } from '../ui/reporters/reporte
99
import { prettyPrintError } from './errors.js';
1010

1111
export abstract class BaseCommand extends Command {
12-
static enableJsonFlag = true;
13-
1412
static baseFlags = {
1513
'debug': Flags.boolean(),
1614
'output': Flags.option({
1715
char: 'o',
1816
default: 'default',
19-
options: ['plain', 'default', 'debug', 'json'],
17+
options: ['plain', 'default', 'json'],
2018
})(),
2119
'secure': Flags.boolean({
2220
char: 's',
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import { MultiSelect } from '@inkjs/ui';
1+
import { Select } from '@inkjs/ui';
22
import { Box, Static, Text } from 'ink';
33
import BigText from 'ink-big-text';
44
import Gradient from 'ink-gradient';
55
import EventEmitter from 'node:events';
66
import React from 'react';
7+
78
import { RenderEvent } from '../../reporters/reporter.js';
89

910
export function InitBanner(props: { emitter: EventEmitter }) {
1011
return <Box flexDirection='column'>
1112
<Static items={[{}]}>{
1213
() => <Box flexDirection='column' key='0'>
1314
<Gradient name='morning'>
14-
<BigText text='Codify' font='tiny'/>
15+
<BigText font='tiny' text='Codify'/>
1516
</Gradient>
1617
<Text>Codify is a configuration-as-code tool that helps you setup and manage your system.</Text>
1718
<Text>Use this init flow to get started quickly with Codify.</Text>
1819
<Text> </Text>
1920
<Text bold>Codify will scan your system for any supported programs or settings and automatically generate configs for you.</Text>
2021
</Box>
2122
}</Static>
22-
<MultiSelect options={[{ label: 'Continue', value: 'Continue' }]} onSubmit={() => { props.emitter.emit(RenderEvent.PROMPT_RESULT); }}/>
23+
<Select onChange={() => { props.emitter.emit(RenderEvent.PROMPT_RESULT); }} options={[{ label: 'Continue', value: 'Continue' }]}/>
2324
</Box>
2425
}

src/ui/reporters/json-reporter.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { SudoRequestData, SudoRequestResponseData } from 'codify-schemas';
2+
3+
import { Plan } from '../../entities/plan.js';
4+
import { ResourceConfig } from '../../entities/resource-config.js';
5+
import { ResourceInfo } from '../../entities/resource-info.js';
6+
import { ImportResult } from '../../orchestrators/import.js';
7+
import { FileModificationResult } from '../../utils/file-modification-calculator.js';
8+
import { PromptType, Reporter } from './reporter.js';
9+
10+
export class JsonReporter implements Reporter {
11+
displayPlan(plan: Plan): void {
12+
console.log(JSON.stringify(plan.resources.map((r) => r.raw), null, 2));
13+
}
14+
15+
async displayInitBanner(): Promise<void> {}
16+
17+
async displayProgress(): Promise<void> {}
18+
19+
async hide(): Promise<void> {}
20+
21+
async promptInitResultSelection(availableTypes: string[]): Promise<string[]> {
22+
return availableTypes;
23+
}
24+
25+
async promptInput(prompt: string, error?: string | undefined, validation?: (() => Promise<boolean>) | undefined, autoComplete?: ((input: string) => string[]) | undefined): Promise<string> {
26+
throw new Error(`Json reporter error: user input is required for prompt: ${prompt}. The Json reporter doesn't support user input. Make sure to have parameters preconfigured`);
27+
}
28+
29+
async promptConfirmation(message: string): Promise<boolean> {
30+
return true;
31+
}
32+
33+
async promptOptions(message: string, options: string[]): Promise<number> {
34+
throw new Error('Json reporter error: this reporter does not support prompting options. Use another reporter.')
35+
}
36+
37+
async promptSudo(pluginName: string, data: SudoRequestData, secureMode: boolean): Promise<SudoRequestResponseData> {
38+
throw new Error(`Json reporter error: sudo required for command: ${data.command}. Make sure to preconfigure the sudo password for the Json reporter`);
39+
}
40+
41+
async promptUserForValues(resources: ResourceInfo[], promptType: PromptType): Promise<ResourceConfig[]> {
42+
throw new Error('Json reporter error: cannot prompt user for values while using Json reporter. Use a different reporter.');
43+
}
44+
45+
async displayImportResult(importResult: ImportResult, showConfigs: boolean): void {
46+
console.log(JSON.stringify(importResult.result.map((r) => r.raw)));
47+
}
48+
49+
async displayFileModifications(diff: { file: string; modification: FileModificationResult; }[]): void {}
50+
51+
displayMessage(message: string): void {}
52+
53+
async displayImportWarning(requiresParameters: string[], noParametersRequired: string[]): Promise<void> {}
54+
}

src/ui/reporters/reporter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ResourceConfig } from '../../entities/resource-config.js';
88
import { FileModificationResult } from '../../utils/file-modification-calculator.js';
99
import { PlainReporter } from './plain-reporter.js';
1010
import { DebugReporter } from './debug-reporter.js';
11+
import { JsonReporter } from './json-reporter.js';
1112

1213
export enum RenderEvent {
1314
LOG = 'log',
@@ -90,7 +91,7 @@ export const ReporterFactory = {
9091
}
9192

9293
case ReporterType.JSON: {
93-
return new DefaultReporter();
94+
return new JsonReporter();
9495
}
9596

9697
default: {

0 commit comments

Comments
 (0)