@wirecat/cli-core 0.722.0 → 0.724.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/cli/documents/history.js +2 -2
  3. package/dist/cli/documents/words.d.ts +1 -1
  4. package/dist/cli/service-api-call.d.ts +6 -0
  5. package/dist/cli/service-api-call.js +21 -9
  6. package/dist/cli/service-api.d.ts +5 -0
  7. package/dist/cli/service-api.js +11 -0
  8. package/dist/content/writes.d.ts +3 -9
  9. package/dist/content/writes.js +3 -13
  10. package/dist/crm/index.d.ts +3 -2
  11. package/dist/crm/index.js +1 -0
  12. package/dist/crm/manifest/manifest.d.ts +60 -1
  13. package/dist/crm/manifest/manifest.js +88 -0
  14. package/dist/crm/manifest/source.d.ts +22 -3
  15. package/dist/crm/manifest/source.js +10 -3
  16. package/dist/crm/manifest/writer.d.ts +39 -0
  17. package/dist/crm/manifest/writer.js +298 -0
  18. package/dist/crm/testing/index.d.ts +1 -1
  19. package/dist/crm/testing/index.js +1 -1
  20. package/dist/crm/testing/manifest-fake.d.ts +14 -0
  21. package/dist/crm/testing/manifest-fake.js +252 -27
  22. package/dist/crm/writes.d.ts +4 -13
  23. package/dist/crm/writes.js +2 -9
  24. package/dist/errors/reasons.js +5 -0
  25. package/dist/ports/wirecat-tools.js +7 -0
  26. package/dist/sends/profile-writes.d.ts +22 -0
  27. package/dist/sends/profile-writes.js +20 -0
  28. package/dist/tracker/cli/index.d.ts +10 -1
  29. package/dist/tracker/cli/index.js +20 -1
  30. package/dist/tracker/cli/writes.d.ts +27 -0
  31. package/dist/tracker/cli/writes.js +74 -0
  32. package/dist/tracker/index.d.ts +5 -2
  33. package/dist/tracker/index.js +5 -2
  34. package/dist/tracker/manifest/manifest.d.ts +25 -0
  35. package/dist/tracker/manifest/manifest.js +59 -8
  36. package/dist/tracker/manifest/source.d.ts +10 -1
  37. package/dist/tracker/manifest/source.js +12 -9
  38. package/dist/tracker/manifest/writer.d.ts +20 -0
  39. package/dist/tracker/manifest/writer.js +72 -0
  40. package/dist/tracker/sync.d.ts +5 -0
  41. package/dist/tracker/sync.js +12 -0
  42. package/dist/tracker/testing/index.d.ts +3 -1
  43. package/dist/tracker/testing/index.js +3 -1
  44. package/dist/tracker/testing/manifest-fake.d.ts +2 -0
  45. package/dist/tracker/testing/manifest-fake.js +123 -41
  46. package/dist/tracker/testing/manifest-writes.d.ts +16 -0
  47. package/dist/tracker/testing/manifest-writes.js +70 -0
  48. package/dist/tracker/testing/source.d.ts +9 -1
  49. package/dist/tracker/testing/source.js +63 -0
  50. package/dist/tracker/testing/write-contract.d.ts +20 -0
  51. package/dist/tracker/testing/write-contract.js +94 -0
  52. package/dist/tracker/writer.d.ts +57 -0
  53. package/dist/tracker/writer.js +25 -0
  54. package/dist/tracker/writes.d.ts +53 -0
  55. package/dist/tracker/writes.js +226 -0
  56. package/package.json +1 -1
@@ -0,0 +1,226 @@
1
+ import { CliError, isCliError } from "@wirecat/cli-base";
2
+ import { foldName } from "../resolve.js";
3
+ import { guardedWrite } from "../sends/guarded.js";
4
+ import { askedKeys, profileWriteGuard } from "../sends/profile-writes.js";
5
+ import { newOperationId } from "../sends/send-id.js";
6
+ import { trackerSelfOf } from "./sync.js";
7
+ import { TRACKER_ASSIGN_OTHERS_KEY, TRACKER_WRITE_DEFAULTS, TRACKER_WRITE_KEYS, } from "./writer.js";
8
+ const PAGE = 500;
9
+ const PRIORITIES = { none: 0, urgent: 1, high: 2, medium: 3, low: 4 };
10
+ const PLURALS = { team: "teams", user: "users", state: "states", label: "labels", project: "projects" };
11
+ const invalid = (message, param, details = {}) => new CliError("validation_error", message, { reason: "tracker.field", ...(param ? { param } : {}), ...details });
12
+ const supported = (writer, write) => {
13
+ if (!writer.writes.includes(write))
14
+ throw new CliError("validation_error", `this tracker takes no issues ${write}`, {
15
+ reason: "tracker.write-unsupported",
16
+ sent: false,
17
+ });
18
+ };
19
+ const namesOf = (kind, record) => {
20
+ const { name } = record;
21
+ if (kind === "team")
22
+ return [name, record.key ?? ""];
23
+ if (kind === "user")
24
+ return [name, record.email ?? ""];
25
+ return [name];
26
+ };
27
+ /** Every live row of the kind, page by page: a uniqueness check over a capped page could miss the second match. */
28
+ const allOf = async (context, kind, parent) => {
29
+ const all = [];
30
+ for (let offset = 0;; offset += PAGE) {
31
+ const page = await context.store.tracker.list(context.account, kind, {
32
+ limit: PAGE,
33
+ offset,
34
+ ...(parent === undefined ? {} : { parent }),
35
+ });
36
+ for (const item of page.items)
37
+ all.push(item);
38
+ if (!page.hasMore)
39
+ return all;
40
+ }
41
+ };
42
+ /** A team's states are its own; its labels are its own and the workspace's, which belong to no team. */
43
+ const withinTeam = (kind, item, team) => {
44
+ if (team === undefined || (kind !== "state" && kind !== "label"))
45
+ return true;
46
+ const owner = item.record.team ?? null;
47
+ return owner === team || (kind === "label" && owner === null);
48
+ };
49
+ /**
50
+ * A record of this account's copy by the tracker's id, or by a name only one of them has, in any letter case. A state
51
+ * or a label is looked for among the issue's team's. Anything else is refused before a write is sent: a reference the
52
+ * copy does not hold would reach the tracker as typed.
53
+ */
54
+ const referenceOf = async (context, kind, given, param, team) => {
55
+ const byId = await context.store.tracker.get(context.account, kind, given);
56
+ if (byId && byId.deletedAt === null && withinTeam(kind, byId, team))
57
+ return given;
58
+ const want = foldName(given.trim());
59
+ const found = (await allOf(context, kind, kind === "state" ? team : undefined)).filter((one) => withinTeam(kind, one, team) && namesOf(kind, one.record).some((name) => name && foldName(name) === want));
60
+ if (found.length === 1)
61
+ return found[0].record.externalId;
62
+ const candidates = found.map((one) => ({ id: one.record.externalId, name: one.record.name }));
63
+ const scoped = (kind === "state" || kind === "label") && team ? " for the issue's team" : "";
64
+ throw invalid(found.length
65
+ ? `${found.length} ${PLURALS[kind]} are named ${given}${scoped}; name one by its id`
66
+ : `no ${kind} ${given} in the copy${scoped}; see ${PLURALS[kind]} list, or run sync`, param, { reason: "tracker.reference", ...(candidates.length ? { candidates } : {}) });
67
+ };
68
+ /** The team a new issue goes to: the one named, or the copy's only team; several and none named is refused. */
69
+ const teamOf = async (context, given) => {
70
+ if (given !== undefined)
71
+ return referenceOf(context, "team", given, "--team");
72
+ const all = await allOf(context, "team");
73
+ if (all.length === 1)
74
+ return all[0].record.externalId;
75
+ // Sent without a team, the issue would be refused by the tracker or land where the owner did not choose.
76
+ if (all.length === 0)
77
+ throw invalid("the copy holds no team for a new issue; run sync, then try again", "--team", {
78
+ reason: "tracker.team-required",
79
+ });
80
+ throw invalid(`this tracker has ${all.length} teams; name one with --team: ` +
81
+ all.map(({ record }) => `${record.name} (${record.key ?? record.externalId})`).join(", "), "--team", { reason: "tracker.team-required", allowed: all.map(({ record }) => record.externalId) });
82
+ };
83
+ /** An issue of this account's copy by its key (`ENG-12`, any case) or id. */
84
+ export const trackerIssueOf = async (context, reference, param = "<issue>") => {
85
+ const found = (await context.store.tracker.get(context.account, "issue", reference)) ??
86
+ (await context.store.tracker.issueByKey(context.account, reference));
87
+ if (!found || found.deletedAt !== null)
88
+ throw new CliError("not_found", `no issue ${reference} in the copy; run sync, then try again`, { param });
89
+ return found;
90
+ };
91
+ /** An empty value clears it, as for every other field: 0 is the tracker's "no priority". */
92
+ const priorityOf = (typed) => {
93
+ if (typed.trim() === "")
94
+ return 0;
95
+ const word = PRIORITIES[typed.trim().toLowerCase()];
96
+ const number = word ?? Number(typed);
97
+ if (!Number.isInteger(number) || number < 0 || number > 4)
98
+ throw invalid("--priority is 0 to 4, or none, urgent, high, medium, low", "--priority");
99
+ return number;
100
+ };
101
+ /** Typed fields into the port's, each reference resolved in the copy; `team` is the issue's, for its states. */
102
+ const fieldsOf = async (context, input, team, self) => {
103
+ const fields = {};
104
+ const cleared = (value) => value.trim() === "";
105
+ if (input.title !== undefined) {
106
+ if (cleared(input.title))
107
+ throw invalid("a title is never empty", "--title");
108
+ fields.title = input.title.trim();
109
+ }
110
+ if (input.description !== undefined)
111
+ fields.description = cleared(input.description) ? null : input.description;
112
+ if (team !== undefined && input.team !== undefined)
113
+ fields.team = team;
114
+ if (input.state !== undefined) {
115
+ if (cleared(input.state))
116
+ throw invalid("every issue is in a state; name one", "--state");
117
+ fields.state = await referenceOf(context, "state", input.state, "--state", team);
118
+ }
119
+ if (input.assignee !== undefined)
120
+ fields.assignee = cleared(input.assignee) ? null : await referenceOf(context, "user", input.assignee, "--assignee");
121
+ if (input.priority !== undefined)
122
+ fields.priority = priorityOf(input.priority);
123
+ if (input.labels !== undefined)
124
+ fields.labels =
125
+ input.labels.length === 1 && cleared(input.labels[0])
126
+ ? []
127
+ : await Promise.all(input.labels.map((one) => referenceOf(context, "label", one, "--label", team)));
128
+ if (input.project !== undefined)
129
+ fields.project = cleared(input.project) ? null : await referenceOf(context, "project", input.project, "--project");
130
+ if (input.parent !== undefined) {
131
+ fields.parent = cleared(input.parent)
132
+ ? null
133
+ : (await trackerIssueOf(context, input.parent, "--parent")).record.externalId;
134
+ if (fields.parent !== null && fields.parent === self)
135
+ throw invalid("an issue is not its own parent", "--parent");
136
+ }
137
+ return fields;
138
+ };
139
+ /**
140
+ * Checked and journaled once. A write the tracker made whose answer then held no record is journaled as sent, and the
141
+ * failure answered after; a lost answer is `outcome_unknown`, and the owner syncs before writing again.
142
+ */
143
+ const written = async (context, write, operationId, act, extra = []) => {
144
+ const key = TRACKER_WRITE_KEYS[write];
145
+ try {
146
+ const confirmed = askedKeys(context.writes, TRACKER_WRITE_DEFAULTS, [key, ...extra]);
147
+ const done = await guardedWrite(profileWriteGuard(context.writes, context.ask, TRACKER_WRITE_DEFAULTS, extra), { chatId: null, kind: "edit", key, operationId }, async () => {
148
+ try {
149
+ return { record: await act({ operationId, confirmed }), unread: null };
150
+ }
151
+ catch (error) {
152
+ if (isCliError(error) && error.details.sent === true)
153
+ return { record: null, unread: error };
154
+ throw error;
155
+ }
156
+ });
157
+ if (done.unread)
158
+ throw new CliError(done.unread.code, `${done.unread.message} — sync to store it, do not write it again`, {
159
+ ...done.unread.details,
160
+ operation_id: operationId,
161
+ });
162
+ return done.record;
163
+ }
164
+ catch (error) {
165
+ if (!isCliError(error) || error.code !== "outcome_unknown")
166
+ throw error;
167
+ throw new CliError("outcome_unknown", `${error.message} — sync and look at the tracker before writing again: a repeat may write it twice`, { ...error.details, operation_id: operationId });
168
+ }
169
+ };
170
+ /** The tracker's answer into the copy, as a sync saves it; a failure names the record, which the tracker has written. */
171
+ const kept = async (context, kind, record, operationId) => {
172
+ try {
173
+ await context.store.tracker.save(context.account, kind, [record], context.now?.() ?? Date.now());
174
+ const found = await context.store.tracker.get(context.account, kind, record.externalId);
175
+ if (!found)
176
+ throw new Error("it is not found after the save");
177
+ return found;
178
+ }
179
+ catch (error) {
180
+ throw new CliError("invalid_response", `the tracker wrote ${kind} ${record.externalId}, but the local store did not keep it ` +
181
+ `(${error instanceof Error ? error.message : String(error)}); sync to store it — do not write it again`, { operation_id: operationId, external_id: record.externalId, sent: true });
182
+ }
183
+ };
184
+ /** An assignee other than the sign-in's own user checks `issues.assign-others` too (WC-PERM-22). */
185
+ const assignKeys = async (context, fields) => {
186
+ if (fields.assignee == null)
187
+ return [];
188
+ const self = await trackerSelfOf(context.store, context.account, context.source?.());
189
+ return fields.assignee === self ? [] : [TRACKER_ASSIGN_OTHERS_KEY];
190
+ };
191
+ /** A new issue in the tracker, through the guarded path; the tracker's answer is stored as a sync stores it. */
192
+ export const createTrackerIssue = async (context, input) => {
193
+ const { writer } = context.writes;
194
+ supported(writer, "create");
195
+ if (input.title === undefined)
196
+ throw invalid("a new issue needs --title", "--title");
197
+ const team = await teamOf(context, input.team);
198
+ const fields = await fieldsOf(context, { ...input, team }, team);
199
+ const operationId = newOperationId();
200
+ const issue = await written(context, "create", operationId, (options) => writer.createIssue({ ...fields, title: fields.title }, options), await assignKeys(context, fields));
201
+ return { operationId, issue: await kept(context, "issue", issue, operationId) };
202
+ };
203
+ /** A change to an issue by its key or id; what is left out keeps its value. */
204
+ export const updateTrackerIssue = async (context, reference, input) => {
205
+ const { writer } = context.writes;
206
+ supported(writer, "update");
207
+ const copy = await trackerIssueOf(context, reference);
208
+ const team = input.team === undefined ? (copy.record.team ?? undefined) : await teamOf(context, input.team);
209
+ const fields = await fieldsOf(context, input, team, copy.record.externalId);
210
+ if (Object.keys(fields).length === 0)
211
+ throw invalid("nothing to change: name a field");
212
+ const operationId = newOperationId();
213
+ const issue = await written(context, "update", operationId, (options) => writer.updateIssue(copy.record.externalId, fields, options), await assignKeys(context, fields));
214
+ return { operationId, issue: await kept(context, "issue", issue, operationId) };
215
+ };
216
+ /** A comment on an issue by its key or id, in the owner's words. */
217
+ export const commentTrackerIssue = async (context, reference, body) => {
218
+ const { writer } = context.writes;
219
+ supported(writer, "comment");
220
+ if (body.trim() === "")
221
+ throw invalid("a comment is never empty", "--body");
222
+ const copy = await trackerIssueOf(context, reference);
223
+ const operationId = newOperationId();
224
+ const comment = await written(context, "comment", operationId, (options) => writer.comment(copy.record.externalId, body, options));
225
+ return { operationId, comment: await kept(context, "comment", comment, operationId) };
226
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.722.0",
3
+ "version": "0.724.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",