Skip to content

Commit

Permalink
feat(main): custom scope prompt
Browse files Browse the repository at this point in the history
Added a config custom_scope default: false to add an option that prompts for a custom scope

Refs: #20
  • Loading branch information
Everduin94 committed Mar 28, 2023
1 parent 9f31c61 commit c5dbb8f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "better-commits",
"private": false,
"version": "1.0.9",
"version": "1.1.0",
"description": "A CLI for creating better commits following the conventional commit guidelines",
"author": "Erik Verduin (https://github.com/everduin94)",
"keywords": [
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ All properties are optional, they can be removed from your configuration and wil
"commit_scope": {
"enable": true,
"initial_value": "app",
"custom_scope": false,
"options": [
{
"value": "app",
Expand Down
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { execSync } from 'child_process';
import { z } from "zod";
import { fromZodError } from 'zod-validation-error';
import { CommitState, Config } from './zod-state';
import { CONFIG_FILE_NAME, get_default_config_path, check_missing_stage, addNewLine, SPACE_TO_SELECT, REGEX_SLASH_TAG, REGEX_SLASH_NUM, REGEX_START_TAG, REGEX_START_NUM, OPTIONAL_PROMPT, clean_commit_title, COMMIT_FOOTER_OPTIONS, infer_type_from_branch, Z_FOOTER_OPTIONS } from './utils';
import { CONFIG_FILE_NAME, get_default_config_path, check_missing_stage, addNewLine, SPACE_TO_SELECT, REGEX_SLASH_TAG, REGEX_SLASH_NUM, REGEX_START_TAG, REGEX_START_NUM, OPTIONAL_PROMPT, clean_commit_title, COMMIT_FOOTER_OPTIONS, infer_type_from_branch, Z_FOOTER_OPTIONS, CUSTOM_SCOPE_KEY } from './utils';

main(load_setup());

Expand Down Expand Up @@ -106,12 +106,19 @@ async function main(config: z.infer<typeof Config>) {
}

if (config.commit_scope.enable) {
const commit_scope = await p.select({
let commit_scope = await p.select({
message: 'Select a commit scope',
initialValue: config.commit_scope.initial_value,
options: config.commit_scope.options
})
if (p.isCancel(commit_scope)) process.exit(0)
if (commit_scope === CUSTOM_SCOPE_KEY && config.commit_scope.custom_scope) {
commit_scope = await p.text({
message: 'Write a custom scope',
placeholder: ''
})
if (p.isCancel(commit_scope)) process.exit(0)
}
commit_state.scope = commit_scope;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const COMMIT_FOOTER_OPTIONS = [
{ value: 'deprecated', label: 'deprecated', hint: 'Add deprecated change'},
{ value: 'custom', label: 'custom', hint: 'Add a custom footer'},
]
export const CUSTOM_SCOPE_KEY: 'custom' = 'custom'

export const Z_FOOTER_OPTIONS = z.enum(['closes', 'breaking-change', 'deprecated', 'custom'])
export const FOOTER_OPTION_VALUES: z.infer<typeof Z_FOOTER_OPTIONS>[] = ['closes', 'breaking-change', 'deprecated', 'custom']
Expand Down
15 changes: 13 additions & 2 deletions src/zod-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod"
import { DEFAULT_SCOPE_OPTIONS, DEFAULT_TYPE_OPTIONS, FOOTER_OPTION_VALUES, Z_FOOTER_OPTIONS } from "./utils"
import { CUSTOM_SCOPE_KEY, DEFAULT_SCOPE_OPTIONS, DEFAULT_TYPE_OPTIONS, FOOTER_OPTION_VALUES, Z_FOOTER_OPTIONS } from "./utils"

// TODO: add "Ref", "Fixes", ability to change phrase "Closes/closes/closes:"
export const Config = z.object({
Expand All @@ -19,15 +19,26 @@ export const Config = z.object({
}, (val) => ({ message: `Type: initial_value "${val.initial_value}" must exist in options` })),
commit_scope: z.object({
enable: z.boolean().default(true),
custom_scope: z.boolean().default(false),
initial_value: z.string().default('app'),
options: z.array(z.object({
value: z.string(),
label: z.string().optional(),
hint: z.string().optional(),
})).default(DEFAULT_SCOPE_OPTIONS)
}).default({})
.transform(val => {
const options = val.options.map(v => v.value)
if (val.custom_scope && !options.includes(CUSTOM_SCOPE_KEY)) {
return {
...val,
options: [...val.options, { label: CUSTOM_SCOPE_KEY, value: CUSTOM_SCOPE_KEY, hint: 'Write a custom scope'}]
}
}
return val
})
.refine(val => {
const options = val.options.map(v => v.value)
const options = val.options.map(v => v.value)
return options.includes(val.initial_value)
}, (val) => ({ message: `Scope: initial_value "${val.initial_value}" must exist in options` })),
check_ticket: z.object({
Expand Down

0 comments on commit c5dbb8f

Please sign in to comment.