Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Opeyem1a committed Sep 23, 2024
1 parent d10f96f commit 62c2c70
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Box, Text } from 'ink';
import { PropValidationResult } from './types.js';
import { type Result } from 'meow';
import { findCommand } from './utils/commands.js';

Expand All @@ -23,21 +24,21 @@ export default function App({ cli }: Props) {
);
}

const { valid, errors } = command.config.validateProps
const validationResult = command.config.validateProps
? command.config.validateProps({
cli,
input: cli.input,
})
: { valid: true, errors: [] };
: ({ valid: true } as PropValidationResult);

if (!valid) {
if (!validationResult.valid) {
return (
<Box flexDirection="column">
<Text>
Invalid inputs for command:{' '}
<Text color="red">{sanitizedInput.join(' ')}</Text>
</Text>
{errors?.map((error) => (
{validationResult.errors.map((error) => (
<Text key={error}>
- <Text color="red">{error}</Text>
</Text>
Expand Down
7 changes: 7 additions & 0 deletions src/command-registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import ChangesAdd, { changesAddConfig } from './commands/changes/add.js';
import ChangesCommit, {
changesCommitConfig,
} from './commands/changes/commit.js';
import Help, { helpConfig } from './commands/help.js';
import Hop, { hopConfig } from './commands/hop.js';
import Sync, { syncConfig } from './commands/sync.js';
Expand Down Expand Up @@ -31,5 +34,9 @@ export const REGISTERED_COMMANDS: CommandGroup = {
component: ChangesAdd,
config: changesAddConfig,
},
commit: {
component: ChangesCommit,
config: changesCommitConfig,
},
} as CommandGroup,
} as const;
3 changes: 2 additions & 1 deletion src/commands/changes/commit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { useGit } from '../../hooks/use-git.js';

function ChangesCommit({ input }: CommandProps) {
const [, , message] = input;
const result = useChangesCommit({ message });
// todo: refactor to a sanitize input pattern
const result = useChangesCommit({ message: message! });

if (result.isError) {
return <ErrorDisplay error={result.error} />;
Expand Down
5 changes: 5 additions & 0 deletions src/services/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface GitService {
branchLocal: () => Promise<ReturnType<SimpleGit['branchLocal']>>;
checkout: (branch: string) => Promise<ReturnType<SimpleGit['checkout']>>;
addAllFiles: () => Promise<void>;
commit: (args: { message: string }) => Promise<void>;
}

export const createGitService = ({
Expand All @@ -33,5 +34,9 @@ export const createGitService = ({
addAllFiles: async () => {
await gitEngine.add('.');
},
commit: async ({ message }) => {
const a = await gitEngine.commit(message);
console.log({a})
},
};
};
13 changes: 9 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ComponentType } from 'react';
import { Result } from 'meow';

type ValidateProps<T extends Record<string, unknown>> = (props: T) => {
valid: boolean;
errors?: string[];
};
export type ValidateProps<T extends Record<string, unknown>> = (
props: T
) => PropValidationResult;

export type PropValidationResult =
| {
valid: true;
}
| { valid: false; errors: string[] };

export interface CommandProps extends Record<string, unknown> {
cli: Pick<Result<any>, 'flags' | 'unnormalizedFlags'>;
Expand Down

0 comments on commit 62c2c70

Please sign in to comment.