Skip to content

Commit

Permalink
perf(branch): #57 replace zod with valibot (#89)
Browse files Browse the repository at this point in the history
Closes: #57

---------

Co-authored-by: Erik Verduin <[email protected]>
  • Loading branch information
ravvi-kumar and Everduin94 authored May 1, 2024
1 parent d7d3380 commit 446e550
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 289 deletions.
43 changes: 11 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"@clack/prompts": "^0.6.2",
"configstore": "^5.0.1",
"picocolors": "^1.0.0",
"zod": "^3.21.3",
"zod-validation-error": "^1.0.1"
"valibot": "^0.30.0"
},
"scripts": {
"start": "jiti ./src/index.ts",
Expand Down
40 changes: 21 additions & 19 deletions src/branch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
#! /usr/bin/env node

import { CommitState, Config } from "./zod-state";
import * as p from "@clack/prompts";
import { execSync } from "child_process";
import Configstore from "configstore";
import color from "picocolors";
import { chdir } from "process";
import { Output, parse } from "valibot";

// This must be imported before ./utils 🤦
import { BranchState, CommitState, Config } from "./vali-state";

import {
BRANCH_ACTION_OPTIONS,
CACHE_PROMPT,
load_setup,
OPTIONAL_PROMPT,
Z_BRANCH_ACTIONS,
Z_BRANCH_CONFIG_FIELDS,
Z_BRANCH_FIELDS,
V_BRANCH_ACTIONS,
V_BRANCH_CONFIG_FIELDS,
V_BRANCH_FIELDS,
load_setup,
} from "./utils";
import { BranchState } from "./zod-state";
import * as p from "@clack/prompts";
import Configstore from "configstore";
import { z } from "zod";
import { execSync } from "child_process";
import color from "picocolors";
import { chdir } from "process";

main(load_setup(" better-branch "));

async function main(config: z.infer<typeof Config>) {
const branch_state = BranchState.parse({});
async function main(config: Output<typeof Config>) {
const branch_state = parse(BranchState, {});

let checkout_type: z.infer<typeof Z_BRANCH_ACTIONS> = "branch";
let checkout_type: Output<typeof V_BRANCH_ACTIONS> = "branch";
if (config.enable_worktrees) {
const branch_or_worktree = await p.select({
message: `Checkout a branch or create a worktree?`,
Expand Down Expand Up @@ -178,12 +180,12 @@ async function main(config: z.infer<typeof Config>) {
}

function build_branch(
branch: z.infer<typeof BranchState>,
config: z.infer<typeof Config>,
branch: Output<typeof BranchState>,
config: Output<typeof Config>,
) {
let res = "";
config.branch_order.forEach((b: z.infer<typeof Z_BRANCH_FIELDS>) => {
const config_key: z.infer<typeof Z_BRANCH_CONFIG_FIELDS> = `branch_${b}`;
config.branch_order.forEach((b: Output<typeof V_BRANCH_FIELDS>) => {
const config_key: Output<typeof V_BRANCH_CONFIG_FIELDS> = `branch_${b}`;
if (branch[b]) res += branch[b] + config[config_key].separator;
});
if (res.endsWith("-") || res.endsWith("/") || res.endsWith("_")) {
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as p from "@clack/prompts";
import color from "picocolors";
import { execSync } from "child_process";
import { chdir } from "process";
import { z } from "zod";
import { CommitState, Config } from "./zod-state";
import { Output, parse } from "valibot";
import { CommitState, Config } from "./vali-state";
import {
load_setup,
addNewLine,
Expand All @@ -18,7 +18,7 @@ import {
clean_commit_title,
COMMIT_FOOTER_OPTIONS,
infer_type_from_branch,
Z_FOOTER_OPTIONS,
V_FOOTER_OPTIONS,
CUSTOM_SCOPE_KEY,
get_git_root,
REGEX_SLASH_UND,
Expand All @@ -28,8 +28,8 @@ import { git_add, git_status } from "./git";

main(load_setup());

export async function main(config: z.infer<typeof Config>) {
let commit_state = CommitState.parse({});
export async function main(config: Output<typeof Config>) {
let commit_state = parse(CommitState, {});
chdir(get_git_root());

if (config.check_status) {
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function main(config: z.infer<typeof Config>) {
message: `Select optional footers ${SPACE_TO_SELECT}`,
initialValues: config.commit_footer.initial_value,
options: COMMIT_FOOTER_OPTIONS as {
value: z.infer<typeof Z_FOOTER_OPTIONS>;
value: Output<typeof V_FOOTER_OPTIONS>;
label: string;
hint: string;
}[],
Expand Down Expand Up @@ -324,8 +324,8 @@ export async function main(config: z.infer<typeof Config>) {
}

function build_commit_string(
commit_state: z.infer<typeof CommitState>,
config: z.infer<typeof Config>,
commit_state: Output<typeof CommitState>,
config: Output<typeof Config>,
colorize: boolean = false,
escape_quotes: boolean = false,
include_trailer: boolean = false,
Expand Down
11 changes: 6 additions & 5 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#! /usr/bin/env node

import { Config } from "./zod-state";
import color from "picocolors";
import fs from "fs";
import * as p from "@clack/prompts";
import { CONFIG_FILE_NAME, get_git_root, load_setup } from "./utils";
import fs from "fs";
import color from "picocolors";
import { parse } from "valibot";
import { CONFIG_FILE_NAME, get_git_root } from "./utils";
import { Config } from "./vali-state";

try {
console.clear();
p.intro(`${color.bgCyan(color.black(" better-commits-init "))}`);
const root = get_git_root();
const root_path = `${root}/${CONFIG_FILE_NAME}`;
const default_config = Config.parse({});
const default_config = parse(Config, {});
fs.writeFileSync(root_path, JSON.stringify(default_config, null, 4));
p.log.success(`${color.green("Successfully created .better-commits.json")}`);
p.outro(
Expand Down
43 changes: 23 additions & 20 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { homedir } from "os";
import { z } from "zod";
import color from "picocolors";
import { execSync } from "child_process";
import * as p from "@clack/prompts";
import { execSync } from "child_process";
import fs from "fs";
import { fromZodError } from "zod-validation-error";
import { Config } from "./zod-state";
import { homedir } from "os";
import color from "picocolors";
import { Output, ValiError, parse, picklist } from "valibot";
import { Config } from "./vali-state";

export const CONFIG_FILE_NAME = ".better-commits.json";
export const SPACE_TO_SELECT = `${color.dim("(<space> to select)")}`;
Expand Down Expand Up @@ -120,44 +119,44 @@ export const COMMIT_FOOTER_OPTIONS = [
];
export const CUSTOM_SCOPE_KEY: "custom" = "custom";

export const Z_FOOTER_OPTIONS = z.enum([
export const V_FOOTER_OPTIONS = picklist([
"closes",
"trailer",
"breaking-change",
"deprecated",
"custom",
]);
export const Z_BRANCH_FIELDS = z.enum([
export const V_BRANCH_FIELDS = picklist([
"user",
"version",
"type",
"ticket",
"description",
]);
export const Z_BRANCH_CONFIG_FIELDS = z.enum([
export const V_BRANCH_CONFIG_FIELDS = picklist([
"branch_user",
"branch_version",
"branch_type",
"branch_ticket",
"branch_description",
]);
export const BRANCH_ORDER_DEFAULTS: z.infer<typeof Z_BRANCH_FIELDS>[] = [
export const BRANCH_ORDER_DEFAULTS: Output<typeof V_BRANCH_FIELDS>[] = [
"user",
"version",
"type",
"ticket",
"description",
];
export const Z_BRANCH_ACTIONS = z.enum(["branch", "worktree"]);
export const FOOTER_OPTION_VALUES: z.infer<typeof Z_FOOTER_OPTIONS>[] = [
export const V_BRANCH_ACTIONS = picklist(["branch", "worktree"]);
export const FOOTER_OPTION_VALUES: Output<typeof V_FOOTER_OPTIONS>[] = [
"closes",
"trailer",
"breaking-change",
"deprecated",
"custom",
];
export const BRANCH_ACTION_OPTIONS: {
value: z.infer<typeof Z_BRANCH_ACTIONS>;
value: Output<typeof V_BRANCH_ACTIONS>;
label: string;
hint?: string;
}[] = [
Expand All @@ -168,7 +167,7 @@ export const BRANCH_ACTION_OPTIONS: {
/* LOAD */
export function load_setup(
cli_name = " better-commits ",
): z.infer<typeof Config> {
): Output<typeof Config> {
console.clear();
p.intro(`${color.bgCyan(color.black(cli_name))}`);

Expand Down Expand Up @@ -197,7 +196,7 @@ export function load_setup(

if (global_config) return global_config;

const default_config = Config.parse({});
const default_config = parse(Config, {});
p.log.step(
"Config not found. Generating default .better-commit.json at $HOME",
);
Expand All @@ -217,13 +216,17 @@ function read_config_from_path(config_path: string) {
return validate_config(res);
}

function validate_config(
config: z.infer<typeof Config>,
): z.infer<typeof Config> {
function validate_config(config: Output<typeof Config>): Output<typeof Config> {
try {
return Config.parse(config);
return parse(Config, config);
} catch (err: any) {
console.log(fromZodError(err).message);
if (err instanceof ValiError) {
const first_issue_path = err.issues[0].path ?? [];
const dot_path = first_issue_path.map((item) => item.key).join(".");
p.log.error(
`Invalid Configuration: ${color.red(dot_path)}\n` + err.message,
);
}
process.exit(0);
}
}
Expand Down
Loading

0 comments on commit 446e550

Please sign in to comment.