Skip to content

Commit 446e550

Browse files
perf(branch): #57 replace zod with valibot (#89)
Closes: #57 --------- Co-authored-by: Erik Verduin <everduin94@gmail.com>
1 parent d7d3380 commit 446e550

File tree

8 files changed

+323
-289
lines changed

8 files changed

+323
-289
lines changed

package-lock.json

Lines changed: 11 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"@clack/prompts": "^0.6.2",
2525
"configstore": "^5.0.1",
2626
"picocolors": "^1.0.0",
27-
"zod": "^3.21.3",
28-
"zod-validation-error": "^1.0.1"
27+
"valibot": "^0.30.0"
2928
},
3029
"scripts": {
3130
"start": "jiti ./src/index.ts",

src/branch.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
#! /usr/bin/env node
22

3-
import { CommitState, Config } from "./zod-state";
3+
import * as p from "@clack/prompts";
4+
import { execSync } from "child_process";
5+
import Configstore from "configstore";
6+
import color from "picocolors";
7+
import { chdir } from "process";
8+
import { Output, parse } from "valibot";
9+
10+
// This must be imported before ./utils 🤦
11+
import { BranchState, CommitState, Config } from "./vali-state";
12+
413
import {
514
BRANCH_ACTION_OPTIONS,
615
CACHE_PROMPT,
7-
load_setup,
816
OPTIONAL_PROMPT,
9-
Z_BRANCH_ACTIONS,
10-
Z_BRANCH_CONFIG_FIELDS,
11-
Z_BRANCH_FIELDS,
17+
V_BRANCH_ACTIONS,
18+
V_BRANCH_CONFIG_FIELDS,
19+
V_BRANCH_FIELDS,
20+
load_setup,
1221
} from "./utils";
13-
import { BranchState } from "./zod-state";
14-
import * as p from "@clack/prompts";
15-
import Configstore from "configstore";
16-
import { z } from "zod";
17-
import { execSync } from "child_process";
18-
import color from "picocolors";
19-
import { chdir } from "process";
2022

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

23-
async function main(config: z.infer<typeof Config>) {
24-
const branch_state = BranchState.parse({});
25+
async function main(config: Output<typeof Config>) {
26+
const branch_state = parse(BranchState, {});
2527

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

180182
function build_branch(
181-
branch: z.infer<typeof BranchState>,
182-
config: z.infer<typeof Config>,
183+
branch: Output<typeof BranchState>,
184+
config: Output<typeof Config>,
183185
) {
184186
let res = "";
185-
config.branch_order.forEach((b: z.infer<typeof Z_BRANCH_FIELDS>) => {
186-
const config_key: z.infer<typeof Z_BRANCH_CONFIG_FIELDS> = `branch_${b}`;
187+
config.branch_order.forEach((b: Output<typeof V_BRANCH_FIELDS>) => {
188+
const config_key: Output<typeof V_BRANCH_CONFIG_FIELDS> = `branch_${b}`;
187189
if (branch[b]) res += branch[b] + config[config_key].separator;
188190
});
189191
if (res.endsWith("-") || res.endsWith("/") || res.endsWith("_")) {

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as p from "@clack/prompts";
44
import color from "picocolors";
55
import { execSync } from "child_process";
66
import { chdir } from "process";
7-
import { z } from "zod";
8-
import { CommitState, Config } from "./zod-state";
7+
import { Output, parse } from "valibot";
8+
import { CommitState, Config } from "./vali-state";
99
import {
1010
load_setup,
1111
addNewLine,
@@ -18,7 +18,7 @@ import {
1818
clean_commit_title,
1919
COMMIT_FOOTER_OPTIONS,
2020
infer_type_from_branch,
21-
Z_FOOTER_OPTIONS,
21+
V_FOOTER_OPTIONS,
2222
CUSTOM_SCOPE_KEY,
2323
get_git_root,
2424
REGEX_SLASH_UND,
@@ -28,8 +28,8 @@ import { git_add, git_status } from "./git";
2828

2929
main(load_setup());
3030

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

3535
if (config.check_status) {
@@ -211,7 +211,7 @@ export async function main(config: z.infer<typeof Config>) {
211211
message: `Select optional footers ${SPACE_TO_SELECT}`,
212212
initialValues: config.commit_footer.initial_value,
213213
options: COMMIT_FOOTER_OPTIONS as {
214-
value: z.infer<typeof Z_FOOTER_OPTIONS>;
214+
value: Output<typeof V_FOOTER_OPTIONS>;
215215
label: string;
216216
hint: string;
217217
}[],
@@ -324,8 +324,8 @@ export async function main(config: z.infer<typeof Config>) {
324324
}
325325

326326
function build_commit_string(
327-
commit_state: z.infer<typeof CommitState>,
328-
config: z.infer<typeof Config>,
327+
commit_state: Output<typeof CommitState>,
328+
config: Output<typeof Config>,
329329
colorize: boolean = false,
330330
escape_quotes: boolean = false,
331331
include_trailer: boolean = false,

src/init.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#! /usr/bin/env node
22

3-
import { Config } from "./zod-state";
4-
import color from "picocolors";
5-
import fs from "fs";
63
import * as p from "@clack/prompts";
7-
import { CONFIG_FILE_NAME, get_git_root, load_setup } from "./utils";
4+
import fs from "fs";
5+
import color from "picocolors";
6+
import { parse } from "valibot";
7+
import { CONFIG_FILE_NAME, get_git_root } from "./utils";
8+
import { Config } from "./vali-state";
89

910
try {
1011
console.clear();
1112
p.intro(`${color.bgCyan(color.black(" better-commits-init "))}`);
1213
const root = get_git_root();
1314
const root_path = `${root}/${CONFIG_FILE_NAME}`;
14-
const default_config = Config.parse({});
15+
const default_config = parse(Config, {});
1516
fs.writeFileSync(root_path, JSON.stringify(default_config, null, 4));
1617
p.log.success(`${color.green("Successfully created .better-commits.json")}`);
1718
p.outro(

src/utils.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { homedir } from "os";
2-
import { z } from "zod";
3-
import color from "picocolors";
4-
import { execSync } from "child_process";
51
import * as p from "@clack/prompts";
2+
import { execSync } from "child_process";
63
import fs from "fs";
7-
import { fromZodError } from "zod-validation-error";
8-
import { Config } from "./zod-state";
4+
import { homedir } from "os";
5+
import color from "picocolors";
6+
import { Output, ValiError, parse, picklist } from "valibot";
7+
import { Config } from "./vali-state";
98

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

123-
export const Z_FOOTER_OPTIONS = z.enum([
122+
export const V_FOOTER_OPTIONS = picklist([
124123
"closes",
125124
"trailer",
126125
"breaking-change",
127126
"deprecated",
128127
"custom",
129128
]);
130-
export const Z_BRANCH_FIELDS = z.enum([
129+
export const V_BRANCH_FIELDS = picklist([
131130
"user",
132131
"version",
133132
"type",
134133
"ticket",
135134
"description",
136135
]);
137-
export const Z_BRANCH_CONFIG_FIELDS = z.enum([
136+
export const V_BRANCH_CONFIG_FIELDS = picklist([
138137
"branch_user",
139138
"branch_version",
140139
"branch_type",
141140
"branch_ticket",
142141
"branch_description",
143142
]);
144-
export const BRANCH_ORDER_DEFAULTS: z.infer<typeof Z_BRANCH_FIELDS>[] = [
143+
export const BRANCH_ORDER_DEFAULTS: Output<typeof V_BRANCH_FIELDS>[] = [
145144
"user",
146145
"version",
147146
"type",
148147
"ticket",
149148
"description",
150149
];
151-
export const Z_BRANCH_ACTIONS = z.enum(["branch", "worktree"]);
152-
export const FOOTER_OPTION_VALUES: z.infer<typeof Z_FOOTER_OPTIONS>[] = [
150+
export const V_BRANCH_ACTIONS = picklist(["branch", "worktree"]);
151+
export const FOOTER_OPTION_VALUES: Output<typeof V_FOOTER_OPTIONS>[] = [
153152
"closes",
154153
"trailer",
155154
"breaking-change",
156155
"deprecated",
157156
"custom",
158157
];
159158
export const BRANCH_ACTION_OPTIONS: {
160-
value: z.infer<typeof Z_BRANCH_ACTIONS>;
159+
value: Output<typeof V_BRANCH_ACTIONS>;
161160
label: string;
162161
hint?: string;
163162
}[] = [
@@ -168,7 +167,7 @@ export const BRANCH_ACTION_OPTIONS: {
168167
/* LOAD */
169168
export function load_setup(
170169
cli_name = " better-commits ",
171-
): z.infer<typeof Config> {
170+
): Output<typeof Config> {
172171
console.clear();
173172
p.intro(`${color.bgCyan(color.black(cli_name))}`);
174173

@@ -197,7 +196,7 @@ export function load_setup(
197196

198197
if (global_config) return global_config;
199198

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

220-
function validate_config(
221-
config: z.infer<typeof Config>,
222-
): z.infer<typeof Config> {
219+
function validate_config(config: Output<typeof Config>): Output<typeof Config> {
223220
try {
224-
return Config.parse(config);
221+
return parse(Config, config);
225222
} catch (err: any) {
226-
console.log(fromZodError(err).message);
223+
if (err instanceof ValiError) {
224+
const first_issue_path = err.issues[0].path ?? [];
225+
const dot_path = first_issue_path.map((item) => item.key).join(".");
226+
p.log.error(
227+
`Invalid Configuration: ${color.red(dot_path)}\n` + err.message,
228+
);
229+
}
227230
process.exit(0);
228231
}
229232
}

0 commit comments

Comments
 (0)