Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Convert gitmoji commands & flags to ts #1277

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions src/constants/flags.js → src/constants/flags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
const FLAGS = Object.freeze({
COMMIT: 'commit',
CONFIG: 'config',
Expand All @@ -10,6 +9,6 @@ const FLAGS = Object.freeze({
SEARCH: 'search',
UPDATE: 'update',
VERSION: 'version'
})
}) as const

export default FLAGS
58 changes: 41 additions & 17 deletions src/utils/findGitmojiCommand.js → src/utils/findGitmojiCommand.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
// @flow
import COMMIT_MODES from '@constants/commit'
import { CommitModes } from '@constants/commit'
import FLAGS from '@constants/flags'
Copy link
Collaborator

Choose a reason for hiding this comment

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

You apparently changed the import but you didn't include the actual enum change


const isSupportedCommand = (command: ?string, options: Object): boolean => {
return Object.keys(options).includes(command)
type Options = {
[key: string]: object
}

Comment on lines +4 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't use object as a type. If you really want to specify a generic object opt for something like this instead:

Suggested change
type Options = {
[key: string]: object
}
type Options = Record<string, any>;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually no, would probably be better if you imported options from cli.ts and specified this as type Options = typeof options.

type Cli = {
flags: Record<string, object>
input: string[]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

showHelp(): void
}

const isSupportedCommand = (
command: string | undefined,
options: Options
): boolean => {
return command !== undefined && Object.keys(options).includes(command)
}

type CommandType = 'flag' | 'command'

type CommandResult = {
type: CommandType
command: string | null
}

const determineCommand = (
flags: Object,
flags: Options,
input: string[],
options: Object
): {
type: 'flag' | 'command',
command: ?string
} => {
options: Options
): CommandResult => {
const command = Object.keys(flags)
.map((flag) => flags[flag] && flag)
.find((flag) => options[flag])
Expand All @@ -25,22 +41,30 @@ const determineCommand = (
}
: {
type: 'command',
command: input[0]
command: input[0] || null
}
}

type CommandOptions = {
message?: string
mode?: CommitModes
scope?: string
title?: string
query?: string[]
}

const getOptionsForCommand = (
command: ?string,
flags: Object,
command: string | null,
flags: Options,
input: string[],
type: 'flag' | 'command'
): ?Object => {
type: CommandType
): CommandOptions | null => {
switch (command) {
case FLAGS.COMMIT:
case FLAGS.HOOK:
return {
message: flags['message'],
mode: command === FLAGS.HOOK ? COMMIT_MODES.HOOK : COMMIT_MODES.CLIENT,
mode: command === FLAGS.HOOK ? CommitModes.HOOK : CommitModes.CLIENT,
scope: flags['scope'],
title: flags['title']
}
Expand All @@ -53,7 +77,7 @@ const getOptionsForCommand = (
return null
}

const findGitmojiCommand = (cli: any, options: Object): void => {
const findGitmojiCommand = (cli: Cli, options: Options): void => {
const { command, type } = determineCommand(cli.flags, cli.input, options)

if (!command || !isSupportedCommand(command, options)) {
Expand Down