@zinn-dev/cli 0.37.0 → 0.39.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.
@@ -1,22 +1,32 @@
1
- import type { TaskChange } from "@zinn-dev/core";
1
+ import type { TaskChange, TaskRelationType } from "@zinn-dev/core";
2
2
  import type { RouteDef } from "../types";
3
3
  import type { PresentedTask } from "../presentation/task-presentation";
4
4
 
5
5
  import { parseArgs } from "node:util";
6
- import { task, project, positiveIntegerPattern, searchLimitMessage } from "@zinn-dev/core";
6
+ import {
7
+ task,
8
+ project,
9
+ positiveIntegerPattern,
10
+ searchLimitMessage,
11
+ parseTaskKey,
12
+ } from "@zinn-dev/core";
7
13
 
8
14
  import {
9
15
  confirmAction,
10
16
  failureMessage,
11
17
  output,
12
18
  quit,
13
- validatePositionalCount,
19
+ UsageError,
20
+ validatePositionals,
14
21
  validateUniqueOptions,
15
22
  } from "../lib";
23
+ import { resolveTextEdit, textEditHelp, textEditOptions, textEditUsage } from "../text-edit";
16
24
  import { formatRelations, presentRelations, relationRouter } from "./relation-router";
17
25
  import { commentRouter, formatComments, presentComments } from "./comment-router";
18
26
  import { currentActor } from "../actor";
27
+ import { resolveProjectKey } from "../project";
19
28
  import { formatSection } from "../presentation/section";
29
+ import { formatActivityList, presentActivities } from "../presentation/activity-presentation";
20
30
  import {
21
31
  formatTaskList,
22
32
  formatTaskListWithStatus,
@@ -34,24 +44,23 @@ export const taskRouter = {
34
44
  description: { type: "string" },
35
45
  priority: { type: "string" },
36
46
  label: { type: "string", multiple: true },
47
+ column: { type: "string" },
48
+ parent: { type: "string" },
49
+ "depends-on": { type: "string", multiple: true },
50
+ "duplicate-of": { type: "string" },
51
+ related: { type: "string", multiple: true },
37
52
  },
38
53
  allowPositionals: true,
39
54
  strict: true,
40
55
  tokens: true,
41
56
  });
42
- const projectKey = positionals[0];
43
- if (projectKey == null) {
44
- quit("A task needs to belong to a project");
45
- }
46
- validatePositionalCount(
47
- positionals,
48
- 1,
49
- "Only one project key can be specified; use --title and --description",
50
- );
57
+ validateUniqueOptions(tokens, ["label", "depends-on", "related"]);
58
+ validatePositionals(positionals, ["[<project-key>]"]);
59
+
60
+ const projectKey = resolveProjectKey(positionals[0], { required: true })!;
51
61
  if (values.title === undefined) {
52
62
  quit("A task needs at least a title: use --title");
53
63
  }
54
- validateUniqueOptions(tokens, ["label"]);
55
64
  const projectMatch = project.getByKey(projectKey);
56
65
  if (projectMatch == null) {
57
66
  quit(`Project with key "${project.standardizeKey(projectKey)}" does not exist`);
@@ -63,20 +72,35 @@ export const taskRouter = {
63
72
  description: values.description ?? null,
64
73
  priority: parsePriority(values.priority),
65
74
  labelNames: values.label,
75
+ columnName: values.column,
76
+ relations: parseNewTaskRelations(values),
66
77
  },
67
78
  currentActor(),
68
79
  );
69
80
  output(presentTask(createdTask), formatTaskRow);
70
81
  },
71
- help: `Usage: zinn task create <project-key> --title <text> [--description <text>] [--priority {low|medium|high|urgent|none}] [--label <name>]
82
+ help: `Usage: zinn task create [<project-key>] --title <text> [--description <text>] [--priority {low|medium|high|urgent|none}] [--label <name>] [--column <name>] [--parent <task-key>] [--depends-on <task-key>] [--duplicate-of <task-key>] [--related <task-key>]
83
+
84
+ Create a task at the bottom of a column. Without --column, it starts in the project's first column.
72
85
 
73
- Create a task in the project's first column.
86
+ An omitted project key uses defaultProject from settings, if present.
87
+ If no default project is set, the key is required.
88
+
89
+ Column names are matched case-insensitively. An unknown column is an error and creates nothing.
74
90
  Priority defaults to none (unassigned).
75
91
  Repeat --label to attach multiple existing labels. Unknown labels are errors.
76
92
  Label names are matched case-insensitively.
77
- Titles cannot be blank. Use --title="--example" for text beginning with a dash.
93
+ Titles are trimmed and cannot be blank or contain line breaks or control characters. Use --title="--example" for text beginning with a dash.
94
+
95
+ Relations are set from the new task's perspective: it becomes a subtask of --parent, depends on --depends-on,
96
+ is a duplicate of --duplicate-of, and is related to --related.
97
+ Repeat --depends-on and --related to name several tasks.
98
+ A target task that does not exist causes an error, and task creation is aborted.
78
99
 
79
- Example: zinn task create MDR --title "Refine the numbers" --description "Meet the quarterly quota" --label urgent`,
100
+ Examples:
101
+ zinn task create MDR --title "Refine the numbers" --description "Meet the quarterly quota" --label urgent
102
+ zinn task create MDR --title "Review the numbers" --column "In Progress"
103
+ zinn task create MDR --title "Sort the files" --parent MDR-1 --depends-on MDR-2`,
80
104
  },
81
105
  list: {
82
106
  run: (args) => {
@@ -85,7 +109,9 @@ Example: zinn task create MDR --title "Refine the numbers" --description "Meet t
85
109
  options: {
86
110
  archived: { type: "boolean" },
87
111
  all: { type: "boolean" },
112
+ "all-projects": { type: "boolean" },
88
113
  label: { type: "string", multiple: true },
114
+ column: { type: "string" },
89
115
  },
90
116
  allowPositionals: true,
91
117
  strict: true,
@@ -93,30 +119,36 @@ Example: zinn task create MDR --title "Refine the numbers" --description "Meet t
93
119
  });
94
120
 
95
121
  validateUniqueOptions(tokens, ["label"]);
122
+ validatePositionals(positionals, ["[<project-key>]"]);
96
123
 
97
124
  if (values.archived && values.all) {
98
125
  quit('Use either "--archived" or "--all", not both');
99
126
  }
100
127
 
101
- const projectKey = positionals[0];
102
-
103
- if (positionals.length > 1) {
104
- quit("Only one project key can be specified");
105
- }
106
-
128
+ const projectKey = resolveProjectKey(positionals[0], { allProjects: values["all-projects"] });
107
129
  const archive = values.all ? "all" : values.archived ? "archived" : "active";
108
130
 
109
- const tasks = task.getAll({ projectKey, archive, labelNames: values.label });
131
+ const tasks = task.getAll({
132
+ projectKey,
133
+ archive,
134
+ labelNames: values.label,
135
+ columnName: values.column,
136
+ });
110
137
 
111
138
  output(tasks.map(presentTask), (presentedTasks) =>
112
139
  formatTaskList(presentedTasks, { showsStatus: values.all }),
113
140
  );
114
141
  },
115
- help: `Usage: zinn task list [<project-key>] [--archived|--all] [--label <name>]
142
+ help: `Usage: zinn task list [<project-key>] [--all-projects] [--archived|--all] [--label <name>] [--column <name>]
116
143
 
117
144
  List active tasks by default.
145
+ An omitted project key uses defaultProject from settings, if present.
146
+ If no default project is set, the command lists every project.
147
+ Use --all-projects to list every project, overriding the default. It cannot be combined with a typed project key.
118
148
  Use --archived to list archived tasks or --all to list both.
119
149
  Use --label to filter for tasks with a specific label, no case-sensitivity is applied for matching.
150
+ Use --column to filter by an exact column name without case sensitivty.
151
+ Column, label and archive filters can be combined together. No matches print nothing, or return [] for --json, and exit successfully.
120
152
 
121
153
  Each row shows a task's key, column, priority marker, title, and labels. --all adds an Active or Archived status after the key.
122
154
  Priority markers: - unassigned, L low, M medium, H high, U urgent.
@@ -131,6 +163,7 @@ Example: zinn task list MDR --label cli`,
131
163
  options: {
132
164
  archived: { type: "boolean" },
133
165
  all: { type: "boolean" },
166
+ "all-projects": { type: "boolean" },
134
167
  limit: { type: "string" },
135
168
  label: { type: "string", multiple: true },
136
169
  },
@@ -139,17 +172,10 @@ Example: zinn task list MDR --label cli`,
139
172
  tokens: true,
140
173
  });
141
174
 
142
- const [query, projectKey] = positionals;
143
-
144
- if (query == null) {
145
- quit("A search query must be specified");
146
- }
147
-
148
- if (positionals.length > 2) {
149
- quit("Only one query and one project key can be specified; quote multi-word queries");
150
- }
151
-
152
175
  validateUniqueOptions(tokens, ["label"]);
176
+ validatePositionals(positionals, ["<query>", "[<project-key>]"]);
177
+
178
+ const query = positionals[0]!;
153
179
 
154
180
  if (values.archived && values.all) {
155
181
  quit('Use either "--archived" or "--all", not both');
@@ -162,6 +188,11 @@ Example: zinn task list MDR --label cli`,
162
188
  quit(searchLimitMessage);
163
189
  }
164
190
 
191
+ const isTaskKeyQuery = parseTaskKey(query.trim()) !== null;
192
+ const projectKey = resolveProjectKey(positionals[1], {
193
+ allProjects: values["all-projects"],
194
+ useDefault: !isTaskKeyQuery,
195
+ });
165
196
  const limit = values.limit === undefined ? undefined : Number(values.limit);
166
197
  const result = task.search({
167
198
  query,
@@ -181,13 +212,17 @@ Example: zinn task list MDR --label cli`,
181
212
  console.error(`${count} archived ${noun} hidden, use --all to include them`);
182
213
  }
183
214
  },
184
- help: `Usage: zinn task search <query> [<project-key>] [--archived|--all] [--limit <n>] [--label <name>]
215
+ help: `Usage: zinn task search <query> [<project-key>] [--all-projects] [--archived|--all] [--limit <n>] [--label <name>]
185
216
 
186
217
  Search task titles, descriptions, and comments. A comment match lists the task it belongs to.
218
+ An omitted project key uses defaultProject from settings, if present.
219
+ If no default project is set, the command searches every project.
220
+ Use --all-projects to search every project, overriding the default. It cannot be combined with a typed project key.
187
221
  Each word matches the start of a word, ignoring case and accents: e.g. "auth" finds "Authentication".
188
222
  Every word must match. Quote a query with several words.
189
223
 
190
224
  A query that is exactly a task key, such as MDR-1, lists that task first.
225
+ If given, a task key query overrides the configured default, and a typed project key still filters the search.
191
226
  Other results are ordered by relevance, with title matches weighted highest.
192
227
  Equally relevant tasks are ordered by project key, then task number.
193
228
 
@@ -197,23 +232,35 @@ Use --limit to show at most <n> results.
197
232
  Use --label to filter for tasks with a specific label, no case-sensitivity is applied for matching.
198
233
  A search result's output matches that of 'task list'.
199
234
 
200
- Use -- before a query that begins with a dash.
201
-
202
235
  Example: zinn task search "refinement quota" MDR --limit 5`,
203
236
  },
204
237
  view: {
205
238
  run: (args) => {
206
- const taskKey = args[0];
207
-
208
- if (taskKey == null) {
209
- quit("Task key must be specified");
210
- }
239
+ const { values, positionals, tokens } = parseArgs({
240
+ args,
241
+ options: { history: { type: "boolean" } },
242
+ allowPositionals: true,
243
+ strict: true,
244
+ tokens: true,
245
+ });
246
+ validateUniqueOptions(tokens);
247
+ validatePositionals(positionals, ["<task-key>"]);
211
248
 
212
- const taskMatch = task.getByKey(taskKey);
249
+ const taskMatch = task.getByKey(positionals[0]!);
213
250
  const viewedTask = {
214
251
  ...presentTask(taskMatch),
215
252
  relations: presentRelations(taskMatch),
216
253
  comments: presentComments(taskMatch.key),
254
+ ...(values.history === true
255
+ ? {
256
+ activity: task
257
+ .activity(taskMatch.key)
258
+ .sort(
259
+ (left, right) =>
260
+ right.created_at - left.created_at || right.id.localeCompare(left.id),
261
+ ),
262
+ }
263
+ : {}),
217
264
  };
218
265
 
219
266
  output(viewedTask, (t) => {
@@ -228,7 +275,9 @@ Example: zinn task search "refinement quota" MDR --limit 5`,
228
275
  const relations = formatRelations(t.relations);
229
276
  const comments = formatComments(t.comments);
230
277
  const isArchived = t.status === "Archived";
231
- const sections = [formatTaskList([t], { showsStatus: isArchived })];
278
+ const sections = [
279
+ formatTaskList([t], { showsStatus: isArchived, showsFullPriority: true }),
280
+ ];
232
281
 
233
282
  if (t.description != null) {
234
283
  sections.push(formatSection("Description", t.description, sectionOptions));
@@ -242,15 +291,28 @@ Example: zinn task search "refinement quota" MDR --limit 5`,
242
291
  sections.push(formatSection("Comments", comments, sectionOptions));
243
292
  }
244
293
 
294
+ if (t.activity !== undefined) {
295
+ const history = formatActivityList(presentActivities(t.activity));
296
+ if (history.length > 0) {
297
+ sections.push(formatSection("History", history, sectionOptions));
298
+ }
299
+ }
300
+
245
301
  return sections.join("\n\n");
246
302
  });
247
303
  },
248
- help: `Usage: zinn task view <task-key>
304
+ help: `Usage: zinn task view <task-key> [--history]
249
305
 
250
306
  Show a task with its description, relations, and comments.
251
307
  A section is shown when the task has that content. The description prints in full, keeping its line breaks.
252
-
253
- Example: zinn task view MDR-1`,
308
+ Use --history to include this task's recorded activity, newest first, including reorders and comment changes.
309
+ Use activity view <activity-id> for full recorded changes. Text times are local with a UTC offset.
310
+ With --history --json, the activity field contains full activity records, or [] when none are recorded.
311
+ Without --history, JSON omits activity.
312
+
313
+ Examples:
314
+ zinn task view MDR-1
315
+ zinn task view MDR-1 --history`,
254
316
  },
255
317
  edit: {
256
318
  run: (args) => {
@@ -259,6 +321,7 @@ Example: zinn task view MDR-1`,
259
321
  options: {
260
322
  title: { type: "string" },
261
323
  description: { type: "string" },
324
+ ...textEditOptions,
262
325
  priority: { type: "string" },
263
326
  "add-label": { type: "string", multiple: true },
264
327
  "remove-label": { type: "string", multiple: true },
@@ -268,19 +331,15 @@ Example: zinn task view MDR-1`,
268
331
  tokens: true,
269
332
  });
270
333
 
271
- const taskKey = positionals[0];
272
-
273
- if (taskKey == null) {
274
- quit("Task key must be specified");
275
- }
276
-
277
- validatePositionalCount(positionals, 1, "Only one task key can be specified");
278
-
279
334
  validateUniqueOptions(tokens, ["add-label", "remove-label"]);
335
+ validatePositionals(positionals, ["<task-key>"]);
336
+
337
+ const taskKey = positionals[0]!;
338
+ const description = resolveTextEdit(values.description, values, "--description");
280
339
 
281
340
  const hasEditFlags =
282
341
  values.title !== undefined ||
283
- values.description !== undefined ||
342
+ description !== undefined ||
284
343
  values.priority !== undefined ||
285
344
  values["add-label"] !== undefined ||
286
345
  values["remove-label"] !== undefined;
@@ -295,7 +354,7 @@ Example: zinn task view MDR-1`,
295
354
  {
296
355
  taskKey,
297
356
  title: values.title,
298
- description: values.description,
357
+ description,
299
358
  priority: parsePriority(values.priority),
300
359
  addLabelNames: values["add-label"],
301
360
  removeLabelNames: values["remove-label"],
@@ -305,38 +364,35 @@ Example: zinn task view MDR-1`,
305
364
 
306
365
  output(presentTask(editedTask), formatTaskRow);
307
366
  },
308
- help: `Usage: zinn task edit <task-key> [--title <text>] [--description <text>] [--priority {low|medium|high|urgent|none}] [--add-label <name>] [--remove-label <name>]
367
+ help: `Usage: zinn task edit <task-key> [--title <text>] [--description <text> ${textEditUsage}] [--priority {low|medium|high|urgent|none}] [--add-label <name>] [--remove-label <name>]
309
368
 
310
369
  Edit a task's title, description, priority, or label attachments.
311
370
  Use --priority none to clear priority.
312
371
  Provide at least one edit flag. Use --description "" to clear the description.
313
372
 
373
+ ${textEditHelp("--description")}
374
+
314
375
  Repeat --add-label or --remove-label for multiple existing labels.
315
376
  Label names are matched case-insensitively.
316
377
  Adding an attached label or removing an unattached label does not do anything.
317
378
  Archived tasks can be edited.
318
379
 
319
380
  The same label cannot be both added and removed.
320
- Titles cannot be blank.
381
+ Titles are trimmed and cannot be blank or contain line breaks or control characters.
321
382
 
322
383
  Use --title="--example" for text beginning with a dash.
323
384
 
324
- Example: zinn task edit MDR-1 --title "Meet the quarterly refinement quota"`,
385
+ Examples:
386
+ zinn task edit MDR-1 --title "Meet the quarterly refinement quota"
387
+ zinn task edit MDR-1 --description "Bring waffles." --append`,
325
388
  },
326
389
  move: {
327
390
  run: (args) => {
328
391
  const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
329
- const taskKeys = positionals.slice(0, -1);
330
- const targetColumn = positionals.at(-1);
331
-
332
- if (targetColumn == null) {
333
- quit("Task key must be specified");
334
- }
335
-
336
- if (taskKeys.length === 0) {
337
- quit("Target column must be specified");
338
- }
392
+ validatePositionals(positionals, ["<task-key>...", "<target-column>"]);
339
393
 
394
+ const taskKeys = positionals.slice(0, -1);
395
+ const targetColumn = positionals.at(-1)!;
340
396
  const moves = task.moveMany({ taskKeys, targetColumn }, currentActor());
341
397
  outputTaskChanges(
342
398
  moves,
@@ -362,28 +418,26 @@ Example: zinn task move MDR-1 MDR-2 "In Progress"`,
362
418
  },
363
419
  order: {
364
420
  run: (args) => {
365
- const [taskKey, direction, targetTaskKey, ...extraArgs] = args;
366
-
367
- if (taskKey == null) {
368
- quit("Task key must be specified");
369
- }
421
+ const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
422
+ /**
423
+ * The words every direction needs. Only whether they are there is checked here,
424
+ * since the direction decides whether a target task follows.
425
+ */
426
+ const leadingWords = ["<task-key>", "{top|up|down|bottom|before|after}"];
427
+ validatePositionals(positionals.slice(0, leadingWords.length), leadingWords);
370
428
 
371
- if (direction == null) {
372
- quit("Order direction must be specified");
373
- }
429
+ const taskKey = positionals[0]!;
430
+ const direction = positionals[1]!;
374
431
 
375
432
  switch (direction) {
376
433
  case "before":
377
434
  case "after": {
378
- if (targetTaskKey == null) {
379
- quit(`Target task must be specified for "${direction}"`);
380
- }
435
+ validatePositionals(positionals, [...leadingWords, "<target-task-key>"]);
381
436
 
382
- if (extraArgs.length > 0) {
383
- quit("Only one target task can be specified");
384
- }
385
-
386
- const orderedTask = task.order({ taskKey, direction, targetTaskKey }, currentActor());
437
+ const orderedTask = task.order(
438
+ { taskKey, direction, targetTaskKey: positionals[2]! },
439
+ currentActor(),
440
+ );
387
441
  output(presentTask(orderedTask), formatTaskRow);
388
442
  return;
389
443
  }
@@ -391,16 +445,14 @@ Example: zinn task move MDR-1 MDR-2 "In Progress"`,
391
445
  case "up":
392
446
  case "down":
393
447
  case "bottom": {
394
- if (targetTaskKey != null) {
395
- quit(`Order direction "${direction}" does not accept a target task`);
396
- }
448
+ validatePositionals(positionals, leadingWords);
397
449
 
398
450
  const orderedTask = task.order({ taskKey, direction }, currentActor());
399
451
  output(presentTask(orderedTask), formatTaskRow);
400
452
  return;
401
453
  }
402
454
  default:
403
- quit(`Unknown order direction "${direction}"`);
455
+ throw new UsageError(`Unknown order direction "${direction}"`);
404
456
  }
405
457
  },
406
458
  help: `Usage: zinn task order <task-key> {top|up|down|bottom}
@@ -456,17 +508,17 @@ Example: zinn task unarchive MDR-1 MDR-2`,
456
508
  },
457
509
  delete: {
458
510
  run: (args: Array<string>) => {
459
- const { values, positionals } = parseArgs({
511
+ const { values, positionals, tokens } = parseArgs({
460
512
  args,
461
513
  options: { yes: { type: "boolean" } },
462
514
  allowPositionals: true,
463
515
  strict: true,
516
+ tokens: true,
464
517
  });
465
- const taskKey = positionals[0];
518
+ validateUniqueOptions(tokens);
519
+ validatePositionals(positionals, ["<task-key>"]);
466
520
 
467
- if (taskKey == null) {
468
- quit("Task key must be specified");
469
- }
521
+ const taskKey = positionals[0]!;
470
522
 
471
523
  try {
472
524
  const taskMatch = task.getByKey(taskKey);
@@ -520,10 +572,7 @@ Remove existing labels from a task. Label names are matched case-insensitively.`
520
572
 
521
573
  function parseTaskKeys(args: Array<string>) {
522
574
  const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
523
-
524
- if (positionals.length === 0) {
525
- quit("Task key must be specified");
526
- }
575
+ validatePositionals(positionals, ["<task-key>..."]);
527
576
 
528
577
  return positionals;
529
578
  }
@@ -557,12 +606,32 @@ function outputTaskChanges(
557
606
 
558
607
  function parseTaskLabelArgs(args: string[]) {
559
608
  const { positionals } = parseArgs({ args, strict: true, allowPositionals: true });
560
- if (positionals.length < 2) {
561
- quit("Provide a task key and at least one label name");
562
- }
609
+ validatePositionals(positionals, ["<task-key>", "<label-name>..."]);
610
+
563
611
  return { taskKey: positionals[0]!, labelNames: positionals.slice(1) };
564
612
  }
565
613
 
614
+ type NewTaskRelationFlags = {
615
+ parent?: string;
616
+ "depends-on"?: Array<string>;
617
+ "duplicate-of"?: string;
618
+ related?: Array<string>;
619
+ };
620
+
621
+ // The new task is the source of each relation, so a flag names the type from its point of view.
622
+ function parseNewTaskRelations(flags: NewTaskRelationFlags) {
623
+ const targetsByType: Array<[TaskRelationType, Array<string>]> = [
624
+ ["subtask", flags.parent === undefined ? [] : [flags.parent]],
625
+ ["dependency", flags["depends-on"] ?? []],
626
+ ["duplicate", flags["duplicate-of"] === undefined ? [] : [flags["duplicate-of"]]],
627
+ ["related", flags.related ?? []],
628
+ ];
629
+
630
+ return targetsByType.flatMap(([relation_type, targetTaskKeys]) =>
631
+ targetTaskKeys.map((targetTaskKey) => ({ relation_type, targetTaskKey })),
632
+ );
633
+ }
634
+
566
635
  function parsePriority(value: string | undefined) {
567
636
  switch (value) {
568
637
  case undefined:
@@ -0,0 +1,68 @@
1
+ import type { TextEdit } from "@zinn-dev/core";
2
+
3
+ import { quit } from "./lib";
4
+
5
+ /** The flags that change how a text flag (--description, --text) is applied to the stored text. */
6
+ export const textEditOptions = {
7
+ append: { type: "boolean" },
8
+ replacing: { type: "string" },
9
+ all: { type: "boolean" },
10
+ } as const;
11
+
12
+ type TextEditFlags = {
13
+ append?: boolean | undefined;
14
+ replacing?: string | undefined;
15
+ all?: boolean | undefined;
16
+ };
17
+
18
+ /** The usage form of the text-edit flags, to follow the text flag in a usage line. */
19
+ export const textEditUsage = "[--append|--replacing <old> [--all]]";
20
+
21
+ /** Help paragraph for the text-edit flags of a command whose text flag is `textFlag`. */
22
+ export function textEditHelp(textFlag: "--description" | "--text") {
23
+ return `With --append, the ${textFlag} text is added as a new paragraph.
24
+ With --replacing <old>, it replaces the one exact, case-sensitive match of <old>
25
+ --all replaces every match.
26
+
27
+ No match, or several matches without --all, fails without changing anything.
28
+ An empty ${textFlag} with --replacing deletes the match.`;
29
+ }
30
+
31
+ /** The value to send to core for a text flag: the full text, or one edit object. */
32
+ export function resolveTextEdit(
33
+ text: string | undefined,
34
+ flags: TextEditFlags,
35
+ textFlag: "--description" | "--text",
36
+ ): string | TextEdit | undefined {
37
+ const isAppending = flags.append === true;
38
+ const isReplacing = flags.replacing !== undefined;
39
+
40
+ if (isAppending && isReplacing) {
41
+ quit(`Option "--append" cannot be used with "--replacing"`);
42
+ }
43
+
44
+ if ((isAppending || isReplacing) && text === undefined) {
45
+ const modifier = isAppending ? "--append" : "--replacing";
46
+ quit(`Option "${modifier}" needs "${textFlag}"`);
47
+ }
48
+
49
+ if (flags.all === true && !isReplacing) {
50
+ quit(`Option "--all" needs "--replacing"`);
51
+ }
52
+
53
+ if (text === undefined) {
54
+ return undefined;
55
+ }
56
+
57
+ if (isAppending) {
58
+ return { append: text };
59
+ }
60
+
61
+ if (flags.replacing !== undefined) {
62
+ return flags.all === true
63
+ ? { find: flags.replacing, replace: text, all: true }
64
+ : { find: flags.replacing, replace: text };
65
+ }
66
+
67
+ return text;
68
+ }