@outfitter/cli 0.1.0-rc.1
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/README.md +436 -0
- package/dist/actions.d.ts +13 -0
- package/dist/actions.js +175 -0
- package/dist/cli.d.ts +104 -0
- package/dist/cli.js +55 -0
- package/dist/command.d.ts +74 -0
- package/dist/command.js +46 -0
- package/dist/index.d.ts +607 -0
- package/dist/index.js +50 -0
- package/dist/input.d.ts +278 -0
- package/dist/input.js +439 -0
- package/dist/output.d.ts +69 -0
- package/dist/output.js +156 -0
- package/dist/pagination.d.ts +96 -0
- package/dist/pagination.js +90 -0
- package/dist/shared/@outfitter/cli-4yy82cmp.js +20 -0
- package/dist/types.d.ts +253 -0
- package/dist/types.js +12 -0
- package/package.json +100 -0
package/dist/input.d.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for collectIds() input utility.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const ids = await collectIds(args, {
|
|
7
|
+
* allowFile: true, // @file expansion
|
|
8
|
+
* allowStdin: true, // - reads from stdin
|
|
9
|
+
* });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
interface CollectIdsOptions {
|
|
13
|
+
/** Allow @file expansion (reads IDs from file) */
|
|
14
|
+
readonly allowFile?: boolean;
|
|
15
|
+
/** Allow glob patterns */
|
|
16
|
+
readonly allowGlob?: boolean;
|
|
17
|
+
/** Allow reading from stdin with "-" */
|
|
18
|
+
readonly allowStdin?: boolean;
|
|
19
|
+
/** Separator for comma-separated values */
|
|
20
|
+
readonly separator?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Options for expandFileArg() input utility.
|
|
24
|
+
*/
|
|
25
|
+
interface ExpandFileOptions {
|
|
26
|
+
/** Encoding for file reads (defaults to utf-8) */
|
|
27
|
+
readonly encoding?: BufferEncoding;
|
|
28
|
+
/** Maximum file size to read (in bytes) */
|
|
29
|
+
readonly maxSize?: number;
|
|
30
|
+
/** Whether to trim the file content */
|
|
31
|
+
readonly trim?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Options for parseGlob() input utility.
|
|
35
|
+
*/
|
|
36
|
+
interface ParseGlobOptions {
|
|
37
|
+
/** Current working directory for glob resolution */
|
|
38
|
+
readonly cwd?: string;
|
|
39
|
+
/** Whether to follow symlinks */
|
|
40
|
+
readonly followSymlinks?: boolean;
|
|
41
|
+
/** Patterns to exclude */
|
|
42
|
+
readonly ignore?: readonly string[];
|
|
43
|
+
/** Only match files (not directories) */
|
|
44
|
+
readonly onlyFiles?: boolean;
|
|
45
|
+
/** Only match directories (not files) */
|
|
46
|
+
readonly onlyDirectories?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Options for normalizeId().
|
|
50
|
+
*/
|
|
51
|
+
interface NormalizeIdOptions {
|
|
52
|
+
/** Whether to lowercase the ID */
|
|
53
|
+
readonly lowercase?: boolean;
|
|
54
|
+
/** Whether to trim whitespace */
|
|
55
|
+
readonly trim?: boolean;
|
|
56
|
+
/** Minimum length requirement */
|
|
57
|
+
readonly minLength?: number;
|
|
58
|
+
/** Maximum length requirement */
|
|
59
|
+
readonly maxLength?: number;
|
|
60
|
+
/** Pattern the ID must match */
|
|
61
|
+
readonly pattern?: RegExp;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Options for confirmDestructive().
|
|
65
|
+
*/
|
|
66
|
+
interface ConfirmDestructiveOptions {
|
|
67
|
+
/** Message to display to the user */
|
|
68
|
+
readonly message: string;
|
|
69
|
+
/** Whether to bypass confirmation (e.g., --yes flag) */
|
|
70
|
+
readonly bypassFlag?: boolean;
|
|
71
|
+
/** Number of items affected (shown in confirmation) */
|
|
72
|
+
readonly itemCount?: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Numeric or date range parsed from CLI input.
|
|
76
|
+
*/
|
|
77
|
+
type Range = NumericRange | DateRange;
|
|
78
|
+
/**
|
|
79
|
+
* Numeric range (e.g., "1-10").
|
|
80
|
+
*/
|
|
81
|
+
interface NumericRange {
|
|
82
|
+
readonly type: "number";
|
|
83
|
+
readonly min: number;
|
|
84
|
+
readonly max: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Date range (e.g., "2024-01-01..2024-12-31").
|
|
88
|
+
*/
|
|
89
|
+
interface DateRange {
|
|
90
|
+
readonly type: "date";
|
|
91
|
+
readonly start: Date;
|
|
92
|
+
readonly end: Date;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Filter expression parsed from CLI input.
|
|
96
|
+
*/
|
|
97
|
+
interface FilterExpression {
|
|
98
|
+
readonly field: string;
|
|
99
|
+
readonly value: string;
|
|
100
|
+
readonly operator?: "eq" | "ne" | "gt" | "lt" | "gte" | "lte" | "contains";
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Sort criteria parsed from CLI input.
|
|
104
|
+
*/
|
|
105
|
+
interface SortCriteria {
|
|
106
|
+
readonly field: string;
|
|
107
|
+
readonly direction: "asc" | "desc";
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Key-value pair parsed from CLI input.
|
|
111
|
+
*/
|
|
112
|
+
interface KeyValuePair {
|
|
113
|
+
readonly key: string;
|
|
114
|
+
readonly value: string;
|
|
115
|
+
}
|
|
116
|
+
import { CancelledError as CancelledError2, ValidationError as ValidationError2 } from "@outfitter/contracts";
|
|
117
|
+
import { Result as Result2 } from "better-result";
|
|
118
|
+
/**
|
|
119
|
+
* Collect IDs from various input formats.
|
|
120
|
+
*
|
|
121
|
+
* Handles space-separated, comma-separated, repeated flags, @file, and stdin.
|
|
122
|
+
*
|
|
123
|
+
* @param input - Raw input from CLI arguments
|
|
124
|
+
* @param options - Collection options
|
|
125
|
+
* @returns Array of collected IDs
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // All these produce the same result:
|
|
130
|
+
* // wm show id1 id2 id3
|
|
131
|
+
* // wm show id1,id2,id3
|
|
132
|
+
* // wm show --ids id1 --ids id2
|
|
133
|
+
* // wm show @ids.txt
|
|
134
|
+
* const ids = await collectIds(args.ids, {
|
|
135
|
+
* allowFile: true,
|
|
136
|
+
* allowStdin: true,
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare function collectIds(input: string | readonly string[], options?: CollectIdsOptions): Promise<string[]>;
|
|
141
|
+
/**
|
|
142
|
+
* Expand @file references to file contents.
|
|
143
|
+
*
|
|
144
|
+
* If the input starts with @, reads the file and returns its contents.
|
|
145
|
+
* Otherwise, returns the input unchanged.
|
|
146
|
+
*
|
|
147
|
+
* @param input - Raw input that may be a @file reference
|
|
148
|
+
* @param options - Expansion options
|
|
149
|
+
* @returns File contents or original input
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // wm create @template.md
|
|
154
|
+
* const content = await expandFileArg(args.content);
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
declare function expandFileArg(input: string, options?: ExpandFileOptions): Promise<string>;
|
|
158
|
+
/**
|
|
159
|
+
* Parse and expand glob patterns.
|
|
160
|
+
*
|
|
161
|
+
* Uses Bun.Glob with workspace constraints.
|
|
162
|
+
*
|
|
163
|
+
* @param pattern - Glob pattern to expand
|
|
164
|
+
* @param options - Glob options
|
|
165
|
+
* @returns Array of matched file paths
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // wm index "src/**\/*.ts"
|
|
170
|
+
* const files = await parseGlob(args.pattern, {
|
|
171
|
+
* cwd: workspaceRoot,
|
|
172
|
+
* ignore: ["node_modules/**"],
|
|
173
|
+
* });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare function parseGlob(pattern: string, options?: ParseGlobOptions): Promise<string[]>;
|
|
177
|
+
/**
|
|
178
|
+
* Parse key=value pairs from CLI input.
|
|
179
|
+
*
|
|
180
|
+
* @param input - Raw input containing key=value pairs
|
|
181
|
+
* @returns Array of parsed key-value pairs
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* // --set key=value --set key2=value2
|
|
186
|
+
* // --set key=value,key2=value2
|
|
187
|
+
* const pairs = parseKeyValue(args.set);
|
|
188
|
+
* // => [{ key: "key", value: "value" }, { key: "key2", value: "value2" }]
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
declare function parseKeyValue(input: string | readonly string[]): Result2<KeyValuePair[], InstanceType<typeof ValidationError2>>;
|
|
192
|
+
/**
|
|
193
|
+
* Parse range inputs (numeric or date).
|
|
194
|
+
*
|
|
195
|
+
* @param input - Range string (e.g., "1-10" or "2024-01-01..2024-12-31")
|
|
196
|
+
* @param type - Type of range to parse
|
|
197
|
+
* @returns Parsed range
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* parseRange("1-10", "number");
|
|
202
|
+
* // => Result<{ type: "number", min: 1, max: 10 }, ValidationError>
|
|
203
|
+
*
|
|
204
|
+
* parseRange("2024-01-01..2024-12-31", "date");
|
|
205
|
+
* // => Result<{ type: "date", start: Date, end: Date }, ValidationError>
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
declare function parseRange(input: string, type: "number" | "date"): Result2<Range, InstanceType<typeof ValidationError2>>;
|
|
209
|
+
/**
|
|
210
|
+
* Parse filter expressions from CLI input.
|
|
211
|
+
*
|
|
212
|
+
* @param input - Filter string (e.g., "status:active,priority:high")
|
|
213
|
+
* @returns Array of parsed filter expressions
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* parseFilter("status:active,priority:high");
|
|
218
|
+
* // => Result<[
|
|
219
|
+
* // { field: "status", value: "active" },
|
|
220
|
+
* // { field: "priority", value: "high" }
|
|
221
|
+
* // ], ValidationError>
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
declare function parseFilter(input: string): Result2<FilterExpression[], InstanceType<typeof ValidationError2>>;
|
|
225
|
+
/**
|
|
226
|
+
* Parse sort specification from CLI input.
|
|
227
|
+
*
|
|
228
|
+
* @param input - Sort string (e.g., "modified:desc,title:asc")
|
|
229
|
+
* @returns Array of parsed sort criteria
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* parseSortSpec("modified:desc,title:asc");
|
|
234
|
+
* // => Result<[
|
|
235
|
+
* // { field: "modified", direction: "desc" },
|
|
236
|
+
* // { field: "title", direction: "asc" }
|
|
237
|
+
* // ], ValidationError>
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare function parseSortSpec(input: string): Result2<SortCriteria[], InstanceType<typeof ValidationError2>>;
|
|
241
|
+
/**
|
|
242
|
+
* Normalize an identifier (trim, lowercase where appropriate).
|
|
243
|
+
*
|
|
244
|
+
* @param input - Raw identifier input
|
|
245
|
+
* @param options - Normalization options
|
|
246
|
+
* @returns Normalized identifier
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* normalizeId(" MY-ID ", { lowercase: true, trim: true });
|
|
251
|
+
* // => Result<"my-id", ValidationError>
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
declare function normalizeId(input: string, options?: NormalizeIdOptions): Result2<string, InstanceType<typeof ValidationError2>>;
|
|
255
|
+
/**
|
|
256
|
+
* Prompt for confirmation before destructive operations.
|
|
257
|
+
*
|
|
258
|
+
* Respects --yes flag for non-interactive mode.
|
|
259
|
+
*
|
|
260
|
+
* @param options - Confirmation options
|
|
261
|
+
* @returns Whether the user confirmed
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const confirmed = await confirmDestructive({
|
|
266
|
+
* message: "Delete 5 notes?",
|
|
267
|
+
* bypassFlag: flags.yes,
|
|
268
|
+
* itemCount: 5,
|
|
269
|
+
* });
|
|
270
|
+
*
|
|
271
|
+
* if (confirmed.isErr()) {
|
|
272
|
+
* // User cancelled
|
|
273
|
+
* process.exit(0);
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
declare function confirmDestructive(options: ConfirmDestructiveOptions): Promise<Result2<boolean, InstanceType<typeof CancelledError2>>>;
|
|
278
|
+
export { parseSortSpec, parseRange, parseKeyValue, parseGlob, parseFilter, normalizeId, expandFileArg, confirmDestructive, collectIds };
|
package/dist/input.js
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
__require,
|
|
4
|
+
__toESM
|
|
5
|
+
} from "./shared/@outfitter/cli-4yy82cmp.js";
|
|
6
|
+
|
|
7
|
+
// packages/cli/src/input.ts
|
|
8
|
+
import path from "path";
|
|
9
|
+
import { CancelledError, ValidationError } from "@outfitter/contracts";
|
|
10
|
+
import { Err, Ok } from "better-result";
|
|
11
|
+
function isSecurePath(filePath, allowAbsolute = false) {
|
|
12
|
+
if (filePath.includes("\x00")) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (filePath.includes("..")) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const normalized = path.normalize(filePath);
|
|
19
|
+
if (!allowAbsolute && path.isAbsolute(normalized)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function isSecureGlobPattern(pattern) {
|
|
25
|
+
if (pattern.startsWith("..")) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (pattern.includes("/../")) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
function isWithinWorkspace(resolvedPath, workspaceRoot) {
|
|
34
|
+
const normalizedPath = path.normalize(resolvedPath);
|
|
35
|
+
const normalizedRoot = path.normalize(workspaceRoot);
|
|
36
|
+
return normalizedPath === normalizedRoot || normalizedPath.startsWith(normalizedRoot + path.sep);
|
|
37
|
+
}
|
|
38
|
+
async function readStdin() {
|
|
39
|
+
const chunks = [];
|
|
40
|
+
for await (const chunk of process.stdin) {
|
|
41
|
+
if (typeof chunk === "string") {
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
} else if (Buffer.isBuffer(chunk)) {
|
|
44
|
+
chunks.push(chunk.toString("utf-8"));
|
|
45
|
+
} else if (chunk instanceof Uint8Array) {
|
|
46
|
+
chunks.push(Buffer.from(chunk).toString("utf-8"));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return chunks.join("");
|
|
50
|
+
}
|
|
51
|
+
function splitIds(input) {
|
|
52
|
+
return input.split(",").flatMap((part) => part.trim().split(/\s+/)).map((id) => id.trim()).filter(Boolean);
|
|
53
|
+
}
|
|
54
|
+
async function isDirectory(path2) {
|
|
55
|
+
try {
|
|
56
|
+
const result = await Bun.$`test -d ${path2}`.quiet();
|
|
57
|
+
return result.exitCode === 0;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function isFile(path2) {
|
|
63
|
+
try {
|
|
64
|
+
const result = await Bun.$`test -f ${path2}`.quiet();
|
|
65
|
+
return result.exitCode === 0;
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function collectIds(input, options) {
|
|
71
|
+
const { allowFile = true, allowStdin = true } = options ?? {};
|
|
72
|
+
const ids = [];
|
|
73
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
74
|
+
for (const item of inputs) {
|
|
75
|
+
if (!item)
|
|
76
|
+
continue;
|
|
77
|
+
if (item.startsWith("@")) {
|
|
78
|
+
const filePath = item.slice(1);
|
|
79
|
+
if (filePath === "-") {
|
|
80
|
+
if (!allowStdin) {
|
|
81
|
+
throw new Error("Reading from stdin is not allowed");
|
|
82
|
+
}
|
|
83
|
+
const stdinContent = await readStdin();
|
|
84
|
+
const stdinIds = stdinContent.split(`
|
|
85
|
+
`).map((line) => line.trim()).filter(Boolean);
|
|
86
|
+
ids.push(...stdinIds);
|
|
87
|
+
} else {
|
|
88
|
+
if (!allowFile) {
|
|
89
|
+
throw new Error("File references are not allowed");
|
|
90
|
+
}
|
|
91
|
+
if (!isSecurePath(filePath, true)) {
|
|
92
|
+
throw new Error(`Security error: path traversal not allowed: ${filePath}`);
|
|
93
|
+
}
|
|
94
|
+
const file = Bun.file(filePath);
|
|
95
|
+
const exists = await file.exists();
|
|
96
|
+
if (!exists) {
|
|
97
|
+
throw new Error(`File not found: ${filePath}`);
|
|
98
|
+
}
|
|
99
|
+
const content = await file.text();
|
|
100
|
+
const fileIds = content.split(`
|
|
101
|
+
`).map((line) => line.trim()).filter(Boolean);
|
|
102
|
+
ids.push(...fileIds);
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
ids.push(...splitIds(item));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return [...new Set(ids)];
|
|
109
|
+
}
|
|
110
|
+
async function expandFileArg(input, options) {
|
|
111
|
+
const {
|
|
112
|
+
encoding: _encoding = "utf-8",
|
|
113
|
+
maxSize,
|
|
114
|
+
trim = false
|
|
115
|
+
} = options ?? {};
|
|
116
|
+
if (!input.startsWith("@")) {
|
|
117
|
+
return input;
|
|
118
|
+
}
|
|
119
|
+
const filePath = input.slice(1);
|
|
120
|
+
if (filePath === "-") {
|
|
121
|
+
let content2 = await readStdin();
|
|
122
|
+
if (trim) {
|
|
123
|
+
content2 = content2.trim();
|
|
124
|
+
}
|
|
125
|
+
return content2;
|
|
126
|
+
}
|
|
127
|
+
if (!isSecurePath(filePath, true)) {
|
|
128
|
+
throw new Error(`Security error: path traversal not allowed: ${filePath}`);
|
|
129
|
+
}
|
|
130
|
+
const file = Bun.file(filePath);
|
|
131
|
+
const exists = await file.exists();
|
|
132
|
+
if (!exists) {
|
|
133
|
+
throw new Error(`File not found: ${filePath}`);
|
|
134
|
+
}
|
|
135
|
+
if (maxSize !== undefined) {
|
|
136
|
+
const size = file.size;
|
|
137
|
+
if (size > maxSize) {
|
|
138
|
+
throw new Error(`File exceeds maximum size of ${maxSize} bytes`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let content = await file.text();
|
|
142
|
+
if (trim) {
|
|
143
|
+
content = content.trim();
|
|
144
|
+
}
|
|
145
|
+
return content;
|
|
146
|
+
}
|
|
147
|
+
async function parseGlob(pattern, options) {
|
|
148
|
+
const {
|
|
149
|
+
cwd = process.cwd(),
|
|
150
|
+
ignore = [],
|
|
151
|
+
onlyFiles = false,
|
|
152
|
+
onlyDirectories = false,
|
|
153
|
+
followSymlinks = false
|
|
154
|
+
} = options ?? {};
|
|
155
|
+
if (!isSecureGlobPattern(pattern)) {
|
|
156
|
+
throw new Error(`Security error: glob pattern may escape workspace: ${pattern}`);
|
|
157
|
+
}
|
|
158
|
+
const resolvedCwd = path.resolve(cwd);
|
|
159
|
+
const glob = new Bun.Glob(pattern);
|
|
160
|
+
const matches = [];
|
|
161
|
+
const scanOptions = {
|
|
162
|
+
cwd,
|
|
163
|
+
followSymlinks,
|
|
164
|
+
onlyFiles: onlyFiles === true
|
|
165
|
+
};
|
|
166
|
+
for await (const match of glob.scan(scanOptions)) {
|
|
167
|
+
const fullPath = path.resolve(cwd, match);
|
|
168
|
+
if (!isWithinWorkspace(fullPath, resolvedCwd)) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
let shouldIgnore = false;
|
|
172
|
+
for (const ignorePattern of ignore) {
|
|
173
|
+
const ignoreGlob = new Bun.Glob(ignorePattern);
|
|
174
|
+
if (ignoreGlob.match(match)) {
|
|
175
|
+
shouldIgnore = true;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (shouldIgnore)
|
|
180
|
+
continue;
|
|
181
|
+
if (onlyDirectories) {
|
|
182
|
+
const isDir = await isDirectory(fullPath);
|
|
183
|
+
if (!isDir)
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (onlyFiles) {
|
|
187
|
+
const isF = await isFile(fullPath);
|
|
188
|
+
if (!isF)
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
matches.push(match);
|
|
192
|
+
}
|
|
193
|
+
return matches;
|
|
194
|
+
}
|
|
195
|
+
function parseKeyValue(input) {
|
|
196
|
+
const pairs = [];
|
|
197
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
198
|
+
for (const item of inputs) {
|
|
199
|
+
if (!item)
|
|
200
|
+
continue;
|
|
201
|
+
const parts = item.split(",");
|
|
202
|
+
for (const part of parts) {
|
|
203
|
+
const trimmed = part.trim();
|
|
204
|
+
if (!trimmed)
|
|
205
|
+
continue;
|
|
206
|
+
const eqIndex = trimmed.indexOf("=");
|
|
207
|
+
if (eqIndex === -1) {
|
|
208
|
+
return new Err(new ValidationError({
|
|
209
|
+
message: `Missing '=' in key-value pair: ${trimmed}`
|
|
210
|
+
}));
|
|
211
|
+
}
|
|
212
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
213
|
+
const value = trimmed.slice(eqIndex + 1);
|
|
214
|
+
if (!key) {
|
|
215
|
+
return new Err(new ValidationError({ message: "Empty key in key-value pair" }));
|
|
216
|
+
}
|
|
217
|
+
pairs.push({ key, value });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return new Ok(pairs);
|
|
221
|
+
}
|
|
222
|
+
function parseRange(input, type) {
|
|
223
|
+
const trimmed = input.trim();
|
|
224
|
+
if (type === "date") {
|
|
225
|
+
const parts = trimmed.split("..");
|
|
226
|
+
if (parts.length === 1) {
|
|
227
|
+
const dateStr = parts[0];
|
|
228
|
+
if (dateStr === undefined) {
|
|
229
|
+
return new Err(new ValidationError({ message: "Empty date input" }));
|
|
230
|
+
}
|
|
231
|
+
const date = new Date(dateStr.trim());
|
|
232
|
+
if (Number.isNaN(date.getTime())) {
|
|
233
|
+
return new Err(new ValidationError({ message: `Invalid date format: ${dateStr}` }));
|
|
234
|
+
}
|
|
235
|
+
return new Ok({ type: "date", start: date, end: date });
|
|
236
|
+
}
|
|
237
|
+
if (parts.length === 2) {
|
|
238
|
+
const startStr = parts[0];
|
|
239
|
+
const endStr = parts[1];
|
|
240
|
+
if (startStr === undefined || endStr === undefined) {
|
|
241
|
+
return new Err(new ValidationError({ message: "Invalid date range format" }));
|
|
242
|
+
}
|
|
243
|
+
const start = new Date(startStr.trim());
|
|
244
|
+
const end = new Date(endStr.trim());
|
|
245
|
+
if (Number.isNaN(start.getTime())) {
|
|
246
|
+
return new Err(new ValidationError({ message: `Invalid date format: ${startStr}` }));
|
|
247
|
+
}
|
|
248
|
+
if (Number.isNaN(end.getTime())) {
|
|
249
|
+
return new Err(new ValidationError({ message: `Invalid date format: ${endStr}` }));
|
|
250
|
+
}
|
|
251
|
+
if (start.getTime() > end.getTime()) {
|
|
252
|
+
return new Err(new ValidationError({
|
|
253
|
+
message: "Start date must be before or equal to end date"
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
return new Ok({ type: "date", start, end });
|
|
257
|
+
}
|
|
258
|
+
return new Err(new ValidationError({ message: `Invalid date range format: ${input}` }));
|
|
259
|
+
}
|
|
260
|
+
const singleNum = Number(trimmed);
|
|
261
|
+
if (!(Number.isNaN(singleNum) || trimmed.includes("-", trimmed.startsWith("-") ? 1 : 0))) {
|
|
262
|
+
return new Ok({ type: "number", min: singleNum, max: singleNum });
|
|
263
|
+
}
|
|
264
|
+
let separatorIndex = -1;
|
|
265
|
+
for (let i = 1;i < trimmed.length; i++) {
|
|
266
|
+
const char = trimmed[i];
|
|
267
|
+
const prevChar = trimmed[i - 1];
|
|
268
|
+
if (char === "-" && prevChar !== undefined && /[\d\s]/.test(prevChar)) {
|
|
269
|
+
separatorIndex = i;
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (separatorIndex === -1) {
|
|
274
|
+
return new Err(new ValidationError({ message: `Invalid numeric range format: ${input}` }));
|
|
275
|
+
}
|
|
276
|
+
const minStr = trimmed.slice(0, separatorIndex).trim();
|
|
277
|
+
const maxStr = trimmed.slice(separatorIndex + 1).trim();
|
|
278
|
+
const min = Number(minStr);
|
|
279
|
+
const max = Number(maxStr);
|
|
280
|
+
if (Number.isNaN(min)) {
|
|
281
|
+
return new Err(new ValidationError({ message: `Invalid number: ${minStr}` }));
|
|
282
|
+
}
|
|
283
|
+
if (Number.isNaN(max)) {
|
|
284
|
+
return new Err(new ValidationError({ message: `Invalid number: ${maxStr}` }));
|
|
285
|
+
}
|
|
286
|
+
if (min > max) {
|
|
287
|
+
return new Err(new ValidationError({ message: "Min must be less than or equal to max" }));
|
|
288
|
+
}
|
|
289
|
+
return new Ok({ type: "number", min, max });
|
|
290
|
+
}
|
|
291
|
+
function parseFilter(input) {
|
|
292
|
+
const trimmed = input.trim();
|
|
293
|
+
if (!trimmed) {
|
|
294
|
+
return new Ok([]);
|
|
295
|
+
}
|
|
296
|
+
const filters = [];
|
|
297
|
+
const parts = trimmed.split(",");
|
|
298
|
+
for (const part of parts) {
|
|
299
|
+
let partTrimmed = part.trim();
|
|
300
|
+
if (!partTrimmed)
|
|
301
|
+
continue;
|
|
302
|
+
let isNegated = false;
|
|
303
|
+
if (partTrimmed.startsWith("!")) {
|
|
304
|
+
isNegated = true;
|
|
305
|
+
partTrimmed = partTrimmed.slice(1).trim();
|
|
306
|
+
}
|
|
307
|
+
const colonIndex = partTrimmed.indexOf(":");
|
|
308
|
+
if (colonIndex === -1) {
|
|
309
|
+
return new Err(new ValidationError({
|
|
310
|
+
message: `Missing ':' in filter expression: ${part.trim()}`
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
const field = partTrimmed.slice(0, colonIndex).trim();
|
|
314
|
+
let value = partTrimmed.slice(colonIndex + 1).trim();
|
|
315
|
+
let operator;
|
|
316
|
+
if (isNegated) {
|
|
317
|
+
operator = "ne";
|
|
318
|
+
} else if (value.startsWith(">=")) {
|
|
319
|
+
operator = "gte";
|
|
320
|
+
value = value.slice(2).trim();
|
|
321
|
+
} else if (value.startsWith("<=")) {
|
|
322
|
+
operator = "lte";
|
|
323
|
+
value = value.slice(2).trim();
|
|
324
|
+
} else if (value.startsWith(">")) {
|
|
325
|
+
operator = "gt";
|
|
326
|
+
value = value.slice(1).trim();
|
|
327
|
+
} else if (value.startsWith("<")) {
|
|
328
|
+
operator = "lt";
|
|
329
|
+
value = value.slice(1).trim();
|
|
330
|
+
} else if (value.startsWith("~")) {
|
|
331
|
+
operator = "contains";
|
|
332
|
+
value = value.slice(1).trim();
|
|
333
|
+
}
|
|
334
|
+
const filter = { field, value };
|
|
335
|
+
if (operator) {
|
|
336
|
+
filter.operator = operator;
|
|
337
|
+
}
|
|
338
|
+
filters.push(filter);
|
|
339
|
+
}
|
|
340
|
+
return new Ok(filters);
|
|
341
|
+
}
|
|
342
|
+
function parseSortSpec(input) {
|
|
343
|
+
const trimmed = input.trim();
|
|
344
|
+
if (!trimmed) {
|
|
345
|
+
return new Ok([]);
|
|
346
|
+
}
|
|
347
|
+
const criteria = [];
|
|
348
|
+
const parts = trimmed.split(",");
|
|
349
|
+
for (const part of parts) {
|
|
350
|
+
const partTrimmed = part.trim();
|
|
351
|
+
if (!partTrimmed)
|
|
352
|
+
continue;
|
|
353
|
+
const colonIndex = partTrimmed.indexOf(":");
|
|
354
|
+
if (colonIndex === -1) {
|
|
355
|
+
criteria.push({ field: partTrimmed, direction: "asc" });
|
|
356
|
+
} else {
|
|
357
|
+
const field = partTrimmed.slice(0, colonIndex).trim();
|
|
358
|
+
const direction = partTrimmed.slice(colonIndex + 1).trim().toLowerCase();
|
|
359
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
360
|
+
return new Err(new ValidationError({
|
|
361
|
+
message: `Invalid sort direction: ${direction}. Must be 'asc' or 'desc'.`
|
|
362
|
+
}));
|
|
363
|
+
}
|
|
364
|
+
criteria.push({ field, direction });
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return new Ok(criteria);
|
|
368
|
+
}
|
|
369
|
+
function normalizeId(input, options) {
|
|
370
|
+
const {
|
|
371
|
+
trim = false,
|
|
372
|
+
lowercase = false,
|
|
373
|
+
minLength,
|
|
374
|
+
maxLength,
|
|
375
|
+
pattern
|
|
376
|
+
} = options ?? {};
|
|
377
|
+
let normalized = input;
|
|
378
|
+
if (trim) {
|
|
379
|
+
normalized = normalized.trim();
|
|
380
|
+
}
|
|
381
|
+
if (lowercase) {
|
|
382
|
+
normalized = normalized.toLowerCase();
|
|
383
|
+
}
|
|
384
|
+
if (minLength !== undefined && normalized.length < minLength) {
|
|
385
|
+
return new Err(new ValidationError({
|
|
386
|
+
message: `ID must be at least ${minLength} characters long`,
|
|
387
|
+
field: "id"
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
if (maxLength !== undefined && normalized.length > maxLength) {
|
|
391
|
+
return new Err(new ValidationError({
|
|
392
|
+
message: `ID must be at most ${maxLength} characters long`,
|
|
393
|
+
field: "id"
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
if (pattern && !pattern.test(normalized)) {
|
|
397
|
+
return new Err(new ValidationError({
|
|
398
|
+
message: `ID does not match required pattern: ${pattern.source}`,
|
|
399
|
+
field: "id"
|
|
400
|
+
}));
|
|
401
|
+
}
|
|
402
|
+
return new Ok(normalized);
|
|
403
|
+
}
|
|
404
|
+
async function confirmDestructive(options) {
|
|
405
|
+
const { message, bypassFlag = false, itemCount } = options;
|
|
406
|
+
if (bypassFlag) {
|
|
407
|
+
return new Ok(true);
|
|
408
|
+
}
|
|
409
|
+
const isTTY = process.stdout.isTTY;
|
|
410
|
+
const isDumbTerminal = process.env["TERM"] === "dumb";
|
|
411
|
+
if (!isTTY || isDumbTerminal) {
|
|
412
|
+
return new Err(new CancelledError({
|
|
413
|
+
message: "Cannot prompt for confirmation in non-interactive mode. Use --yes to bypass."
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
let promptMessage = message;
|
|
417
|
+
if (itemCount !== undefined) {
|
|
418
|
+
promptMessage = `${message} (${itemCount} items)`;
|
|
419
|
+
}
|
|
420
|
+
const { confirm, isCancel } = await import("@clack/prompts");
|
|
421
|
+
const response = await confirm({ message: promptMessage });
|
|
422
|
+
if (isCancel(response) || response === false) {
|
|
423
|
+
return new Err(new CancelledError({
|
|
424
|
+
message: "Operation cancelled by user."
|
|
425
|
+
}));
|
|
426
|
+
}
|
|
427
|
+
return new Ok(true);
|
|
428
|
+
}
|
|
429
|
+
export {
|
|
430
|
+
parseSortSpec,
|
|
431
|
+
parseRange,
|
|
432
|
+
parseKeyValue,
|
|
433
|
+
parseGlob,
|
|
434
|
+
parseFilter,
|
|
435
|
+
normalizeId,
|
|
436
|
+
expandFileArg,
|
|
437
|
+
confirmDestructive,
|
|
438
|
+
collectIds
|
|
439
|
+
};
|