ag-toolkit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Riya Amemiya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.ja.md ADDED
@@ -0,0 +1,33 @@
1
+ # aglib
2
+
3
+ `aglib` は `agbd` と `agrb` CLIツール間で共有される機能を提供するライブラリです。
4
+
5
+ ## 含まれる機能
6
+
7
+ - **UIコンポーネント:** Inkを使用した対話的なUIコンポーネント。
8
+ - **Git操作:** `simple-git`をラップしたGitコマンド操作ユーティリティ。
9
+ - **設定管理:** 設定ファイルの読み込みや書き込み、管理機能。
10
+ - **その他ユーティリティ:** 引数パーサーやバリデーション関数など。
11
+
12
+ ## 開発
13
+
14
+ このライブラリは`agbd`および`agrb`プロジェクトから利用されることを想定しています。
15
+
16
+ ```bash
17
+ # 依存関係のインストール
18
+ bun install
19
+
20
+ # ビルド
21
+ bun run build
22
+
23
+ # 開発(watch)
24
+ bun run dev
25
+
26
+ # Lint(チェック/自動修正)
27
+ bun run test
28
+ bun run lint
29
+ ```
30
+
31
+ ## ライセンス
32
+
33
+ MIT
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # aglib
2
+
3
+ `aglib` is a library that provides shared functionality for the `agbd` and `agrb` CLI tools.
4
+
5
+ ## Features Included
6
+
7
+ - **UI Components:** Interactive UI components using Ink.
8
+ - **Git Operations:** Git command utilities wrapping `simple-git`.
9
+ - **Configuration Management:** Functions for reading, writing, and managing configuration files.
10
+ - **Other Utilities:** Argument parsers, validation functions, etc.
11
+
12
+ ## Development
13
+
14
+ This library is intended to be used by the `agbd` and `agrb` projects.
15
+
16
+ ```bash
17
+ # Install dependencies
18
+ bun install
19
+
20
+ # Build
21
+ bun run build
22
+
23
+ # Development (watch)
24
+ bun run dev
25
+
26
+ # Lint (check/fix)
27
+ bun run test
28
+ bun run lint
29
+ ```
30
+
31
+ ## License
32
+
33
+ MIT
@@ -0,0 +1,14 @@
1
+ export type ConfigItem<T> = {
2
+ key: keyof T;
3
+ type: "string" | "number" | "boolean" | "array" | "select";
4
+ options?: string[];
5
+ };
6
+ type ConfigEditorProps<T extends Record<string, unknown>> = {
7
+ toolName: string;
8
+ configItems: ConfigItem<T>[];
9
+ defaultConfig: T;
10
+ loadConfig: () => Promise<T>;
11
+ writeConfig: (config: T) => Promise<void>;
12
+ };
13
+ export declare const ConfigEditor: <T extends Record<string, unknown>>({ toolName, configItems, defaultConfig, loadConfig, writeConfig, }: ConfigEditorProps<T>) => import("react/jsx-runtime").JSX.Element;
14
+ export {};
@@ -0,0 +1,196 @@
1
+ import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Box, Text, useApp, useInput } from "ink";
3
+ import SelectInput from "ink-select-input";
4
+ import Spinner from "ink-spinner";
5
+ import { useEffect, useMemo, useState } from "react";
6
+ import { formatConfigValue } from "../lib/format.js";
7
+ import { isDeepEqual } from "../lib/isDeepEqual.js";
8
+ import { BooleanItem, Item } from "./ConfigEditorItem.js";
9
+ export const ConfigEditor = ({ toolName, configItems, defaultConfig, loadConfig, writeConfig, }) => {
10
+ const { exit } = useApp();
11
+ const [config, setConfig] = useState(null);
12
+ const [initialConfig, setInitialConfig] = useState(null);
13
+ const [status, setStatus] = useState("loading");
14
+ const [editingItem, setEditingItem] = useState(null);
15
+ const [inputBuffer, setInputBuffer] = useState("");
16
+ const [inputError, setInputError] = useState(null);
17
+ const isDirty = useMemo(() => config && initialConfig ? !isDeepEqual(config, initialConfig) : false, [config, initialConfig]);
18
+ useEffect(() => {
19
+ (async () => {
20
+ const loadedConfig = await loadConfig();
21
+ const fullConfig = { ...defaultConfig, ...loadedConfig };
22
+ setConfig(fullConfig);
23
+ setInitialConfig(JSON.parse(JSON.stringify(fullConfig)));
24
+ setStatus("selecting");
25
+ })();
26
+ }, [loadConfig, defaultConfig]);
27
+ const handleSave = async () => {
28
+ if (config) {
29
+ setStatus("saving");
30
+ await writeConfig(config);
31
+ setStatus("done");
32
+ exit();
33
+ }
34
+ };
35
+ useInput((input) => {
36
+ if (status !== "selecting") {
37
+ return;
38
+ }
39
+ if (input.toLowerCase() === "q") {
40
+ if (isDirty) {
41
+ setStatus("confirm_quit");
42
+ }
43
+ else {
44
+ exit();
45
+ }
46
+ return;
47
+ }
48
+ if (input.toLowerCase() === "s") {
49
+ handleSave();
50
+ }
51
+ }, { isActive: status === "selecting" });
52
+ const items = config
53
+ ? configItems.map((item) => ({
54
+ label: `${String(item.key)}: ${formatConfigValue(config[item.key])}`,
55
+ value: item.key,
56
+ }))
57
+ : [];
58
+ const handleSelect = (selected) => {
59
+ const item = configItems.find((i) => i.key === selected.value);
60
+ if (!config || !item) {
61
+ return;
62
+ }
63
+ setEditingItem(item);
64
+ setStatus("editing");
65
+ const value = config[item.key];
66
+ switch (item.type) {
67
+ case "string":
68
+ case "number":
69
+ setInputBuffer(value !== undefined ? String(value) : "");
70
+ break;
71
+ case "array":
72
+ setInputBuffer(Array.isArray(value) && value.length > 0 ? value.join(", ") : "");
73
+ break;
74
+ }
75
+ setInputError(null);
76
+ };
77
+ const cancelEditing = () => {
78
+ setEditingItem(null);
79
+ setInputBuffer("");
80
+ setInputError(null);
81
+ setStatus("selecting");
82
+ };
83
+ const commitEditing = () => {
84
+ if (!config || !editingItem)
85
+ return;
86
+ const { key, type } = editingItem;
87
+ let nextValue;
88
+ switch (type) {
89
+ case "string": {
90
+ const trimmed = inputBuffer.trim();
91
+ nextValue = trimmed.length > 0 ? trimmed : undefined;
92
+ break;
93
+ }
94
+ case "number": {
95
+ const valueText = inputBuffer.trim();
96
+ if (valueText.length === 0) {
97
+ nextValue = undefined;
98
+ }
99
+ else {
100
+ const parsed = Number.parseInt(valueText, 10);
101
+ if (Number.isNaN(parsed) || parsed < 0) {
102
+ setInputError("Please enter an integer >= 0");
103
+ return;
104
+ }
105
+ nextValue = parsed;
106
+ }
107
+ break;
108
+ }
109
+ case "array":
110
+ nextValue = inputBuffer
111
+ .split(",")
112
+ .map((part) => part.trim())
113
+ .filter((part) => part.length > 0);
114
+ break;
115
+ default:
116
+ cancelEditing();
117
+ return;
118
+ }
119
+ setConfig({ ...config, [key]: nextValue });
120
+ cancelEditing();
121
+ };
122
+ useInput((input, key) => {
123
+ if (status !== "editing" || !editingItem)
124
+ return;
125
+ if (key.escape) {
126
+ cancelEditing();
127
+ return;
128
+ }
129
+ if (editingItem.type === "string" ||
130
+ editingItem.type === "number" ||
131
+ editingItem.type === "array") {
132
+ if (key.return) {
133
+ commitEditing();
134
+ return;
135
+ }
136
+ if (key.backspace || key.delete) {
137
+ setInputBuffer((prev) => prev.slice(0, -1));
138
+ return;
139
+ }
140
+ if (input && !key.ctrl && !key.meta) {
141
+ setInputBuffer((prev) => prev + input);
142
+ }
143
+ }
144
+ }, { isActive: status === "editing" });
145
+ const handleBooleanChange = (item) => {
146
+ if (config && editingItem) {
147
+ setConfig({ ...config, [editingItem.key]: item.value });
148
+ cancelEditing();
149
+ }
150
+ };
151
+ const handleSelectChange = (item) => {
152
+ if (config && editingItem) {
153
+ setConfig({ ...config, [editingItem.key]: item.value });
154
+ cancelEditing();
155
+ }
156
+ };
157
+ const handleQuitConfirm = (item) => {
158
+ if (item.value === "yes") {
159
+ exit();
160
+ }
161
+ else {
162
+ setStatus("selecting");
163
+ }
164
+ };
165
+ const renderEditor = () => {
166
+ if (!editingItem)
167
+ return null;
168
+ switch (editingItem.type) {
169
+ case "boolean":
170
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: ["Set value for \"", String(editingItem.key), "\":"] }), _jsx(SelectInput, { items: [
171
+ { label: "true", value: true },
172
+ { label: "false", value: false },
173
+ ], onSelect: handleBooleanChange, itemComponent: BooleanItem })] }));
174
+ case "select":
175
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: ["Set value for \"", String(editingItem.key), "\":"] }), _jsx(SelectInput, { items: editingItem.options?.map((opt) => ({
176
+ label: opt,
177
+ value: opt,
178
+ })), onSelect: handleSelectChange, itemComponent: Item })] }));
179
+ case "string":
180
+ case "number":
181
+ case "array": {
182
+ let prompt = "Enter a string";
183
+ if (editingItem.type === "number")
184
+ prompt = "Enter a number (>= 0)";
185
+ if (editingItem.type === "array")
186
+ prompt = "Enter comma-separated values";
187
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: [prompt, " (leave empty to unset):"] }), _jsx(Text, { color: "cyan", children: inputBuffer || "<empty>" }), _jsx(Text, { color: "gray", children: "Enter: save / Esc: cancel" }), inputError && _jsx(Text, { color: "red", children: inputError })] }));
188
+ }
189
+ }
190
+ return null;
191
+ };
192
+ return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsxs(Text, { bold: true, children: [toolName, " Configuration Editor"] }), status === "loading" && (_jsxs(Text, { children: [_jsx(Spinner, {}), " Loading configuration..."] })), status === "selecting" && config && (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(SelectInput, { items: items, onSelect: handleSelect, itemComponent: Item }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: ["Select an item to edit. 'S' to save, 'Q' to exit.", isDirty && _jsx(Text, { color: "yellow", children: " (unsaved)" })] }) })] })), status === "editing" && renderEditor(), status === "confirm_quit" && (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "yellow", children: "Unsaved changes detected. Exit?" }), _jsx(SelectInput, { items: [
193
+ { label: "No", value: "no" },
194
+ { label: "Yes", value: "yes" },
195
+ ], onSelect: handleQuitConfirm })] })), status === "saving" && _jsx(Text, { children: "\uD83D\uDCBE Saving configuration..." })] }));
196
+ };
@@ -0,0 +1,3 @@
1
+ import type { ItemProps } from "ink-select-input";
2
+ export declare const Item: ({ label, isSelected }: ItemProps) => import("react/jsx-runtime").JSX.Element;
3
+ export declare const BooleanItem: ({ label, isSelected }: ItemProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Text } from "ink";
3
+ export const Item = ({ label, isSelected }) => (_jsx(Text, { color: isSelected ? "cyan" : undefined, children: label }));
4
+ export const BooleanItem = ({ label, isSelected }) => (_jsx(Text, { color: isSelected ? "cyan" : undefined, children: label === "true" ? (_jsx(Text, { color: "green", children: "true" })) : (_jsx(Text, { color: "red", children: "false" })) }));
@@ -0,0 +1,2 @@
1
+ export * from "./ConfigEditor.js";
2
+ export * from "./ConfigEditorItem.js";
@@ -0,0 +1,2 @@
1
+ export * from "./ConfigEditor.js";
2
+ export * from "./ConfigEditorItem.js";
package/dist/git.d.ts ADDED
@@ -0,0 +1,61 @@
1
+ export type BranchType = "local" | "remote";
2
+ export interface BranchInfo {
3
+ ref: string;
4
+ name: string;
5
+ type: BranchType;
6
+ remote?: string;
7
+ lastCommitDate: Date | null;
8
+ lastCommitSha: string | null;
9
+ lastCommitSubject: string | null;
10
+ isMerged: boolean;
11
+ ahead: number;
12
+ behind: number;
13
+ }
14
+ export declare class GitOperations {
15
+ private git;
16
+ constructor(workingDir?: string);
17
+ getCurrentBranch(): Promise<string>;
18
+ isWorkdirClean(): Promise<boolean>;
19
+ fetchAll(): Promise<void>;
20
+ getBranchInfos(options?: {
21
+ includeRemote?: boolean;
22
+ }): Promise<BranchInfo[]>;
23
+ deleteLocalBranch(branch: string, options?: {
24
+ force?: boolean;
25
+ }): Promise<void>;
26
+ deleteRemoteBranch(branch: {
27
+ remote: string;
28
+ name: string;
29
+ }): Promise<void>;
30
+ detectDefaultBranch(remote?: string): Promise<string | null>;
31
+ private getBaseBranch;
32
+ private getAheadBehind;
33
+ private getMergedSet;
34
+ private listBranches;
35
+ private parseBranchLine;
36
+ getAllBranches(): Promise<string[]>;
37
+ getLocalBranches(): Promise<string[]>;
38
+ branchExists(branch: string): Promise<boolean>;
39
+ private resolveBranchRef;
40
+ setupCherryPick(targetBranch: string): Promise<string>;
41
+ getMergeBase(branch1: string, branch2: string): Promise<string>;
42
+ getCommitsToCherryPick(from: string, to: string): Promise<string[]>;
43
+ cherryPick(commitSha: string, options?: {
44
+ allowEmpty?: boolean;
45
+ }): Promise<void>;
46
+ continueCherryPick(): Promise<void>;
47
+ skipCherryPick(): Promise<void>;
48
+ abortCherryPick(): Promise<void>;
49
+ resolveConflictWithStrategy(strategy: "ours" | "theirs"): Promise<void>;
50
+ finishCherryPick(currentBranch: string, tempBranchName: string, options?: {
51
+ createBackup?: boolean;
52
+ }): Promise<void>;
53
+ cleanupCherryPick(tempBranchName: string, originalBranch: string): Promise<void>;
54
+ performLinearRebase(currentBranch: string, targetBranch: string, progressCallback?: (message: string) => void, options?: {
55
+ continueOnConflict?: boolean;
56
+ }): Promise<void>;
57
+ getCommitSubject(sha: string): Promise<string>;
58
+ startAutostash(): Promise<string | null>;
59
+ popStash(stashRef: string): Promise<void>;
60
+ pushWithLease(branch: string): Promise<void>;
61
+ }