@captainsafia/stitch 0.0.1 → 1.0.0-preview.ae4f088

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/dist/api.js ADDED
@@ -0,0 +1,1837 @@
1
+ // @bun
2
+ // src/api.ts
3
+ import { spawn } from "child_process";
4
+
5
+ // src/core/store.ts
6
+ import { readdir, readFile, writeFile, mkdir } from "fs/promises";
7
+ import { join } from "path";
8
+ import { existsSync } from "fs";
9
+
10
+ // src/core/errors.ts
11
+ class StitchError extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ this.name = "StitchError";
15
+ }
16
+ }
17
+
18
+ class RepoNotFoundError extends StitchError {
19
+ constructor(path) {
20
+ super(path ? `Not a git repository: ${path}` : "Not a git repository (or any parent up to mount point)");
21
+ this.name = "RepoNotFoundError";
22
+ }
23
+ }
24
+
25
+ class NotInitializedError extends StitchError {
26
+ constructor() {
27
+ super("Stitch is not initialized in this repository. Run 'stitch init' first.");
28
+ this.name = "NotInitializedError";
29
+ }
30
+ }
31
+
32
+ class NoCurrentStitchError extends StitchError {
33
+ constructor() {
34
+ super("No current stitch. Start a new stitch with 'stitch start <title>' or switch to an existing one with 'stitch switch <id>'.");
35
+ this.name = "NoCurrentStitchError";
36
+ }
37
+ }
38
+
39
+ class StitchNotFoundError extends StitchError {
40
+ constructor(id) {
41
+ super(`Stitch not found: ${id}`);
42
+ this.name = "StitchNotFoundError";
43
+ }
44
+ }
45
+
46
+ class GitError extends StitchError {
47
+ command;
48
+ exitCode;
49
+ constructor(message, command, exitCode) {
50
+ super(`Git error: ${message}`);
51
+ this.command = command;
52
+ this.exitCode = exitCode;
53
+ this.name = "GitError";
54
+ }
55
+ }
56
+
57
+ class ValidationError extends StitchError {
58
+ constructor(message) {
59
+ super(`Validation error: ${message}`);
60
+ this.name = "ValidationError";
61
+ }
62
+ }
63
+
64
+ // node_modules/smol-toml/dist/error.js
65
+ /*!
66
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
67
+ * SPDX-License-Identifier: BSD-3-Clause
68
+ *
69
+ * Redistribution and use in source and binary forms, with or without
70
+ * modification, are permitted provided that the following conditions are met:
71
+ *
72
+ * 1. Redistributions of source code must retain the above copyright notice, this
73
+ * list of conditions and the following disclaimer.
74
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
75
+ * this list of conditions and the following disclaimer in the
76
+ * documentation and/or other materials provided with the distribution.
77
+ * 3. Neither the name of the copyright holder nor the names of its contributors
78
+ * may be used to endorse or promote products derived from this software without
79
+ * specific prior written permission.
80
+ *
81
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
82
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
83
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
84
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
85
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
87
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
88
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
90
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91
+ */
92
+ function getLineColFromPtr(string, ptr) {
93
+ let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
94
+ return [lines.length, lines.pop().length + 1];
95
+ }
96
+ function makeCodeBlock(string, line, column) {
97
+ let lines = string.split(/\r\n|\n|\r/g);
98
+ let codeblock = "";
99
+ let numberLen = (Math.log10(line + 1) | 0) + 1;
100
+ for (let i = line - 1;i <= line + 1; i++) {
101
+ let l = lines[i - 1];
102
+ if (!l)
103
+ continue;
104
+ codeblock += i.toString().padEnd(numberLen, " ");
105
+ codeblock += ": ";
106
+ codeblock += l;
107
+ codeblock += `
108
+ `;
109
+ if (i === line) {
110
+ codeblock += " ".repeat(numberLen + column + 2);
111
+ codeblock += `^
112
+ `;
113
+ }
114
+ }
115
+ return codeblock;
116
+ }
117
+
118
+ class TomlError extends Error {
119
+ line;
120
+ column;
121
+ codeblock;
122
+ constructor(message, options) {
123
+ const [line, column] = getLineColFromPtr(options.toml, options.ptr);
124
+ const codeblock = makeCodeBlock(options.toml, line, column);
125
+ super(`Invalid TOML document: ${message}
126
+
127
+ ${codeblock}`, options);
128
+ this.line = line;
129
+ this.column = column;
130
+ this.codeblock = codeblock;
131
+ }
132
+ }
133
+
134
+ // node_modules/smol-toml/dist/util.js
135
+ /*!
136
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
137
+ * SPDX-License-Identifier: BSD-3-Clause
138
+ *
139
+ * Redistribution and use in source and binary forms, with or without
140
+ * modification, are permitted provided that the following conditions are met:
141
+ *
142
+ * 1. Redistributions of source code must retain the above copyright notice, this
143
+ * list of conditions and the following disclaimer.
144
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
145
+ * this list of conditions and the following disclaimer in the
146
+ * documentation and/or other materials provided with the distribution.
147
+ * 3. Neither the name of the copyright holder nor the names of its contributors
148
+ * may be used to endorse or promote products derived from this software without
149
+ * specific prior written permission.
150
+ *
151
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
152
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
153
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
154
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
155
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
156
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
157
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
158
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
159
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
160
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
161
+ */
162
+ function isEscaped(str, ptr) {
163
+ let i = 0;
164
+ while (str[ptr - ++i] === "\\")
165
+ ;
166
+ return --i && i % 2;
167
+ }
168
+ function indexOfNewline(str, start = 0, end = str.length) {
169
+ let idx = str.indexOf(`
170
+ `, start);
171
+ if (str[idx - 1] === "\r")
172
+ idx--;
173
+ return idx <= end ? idx : -1;
174
+ }
175
+ function skipComment(str, ptr) {
176
+ for (let i = ptr;i < str.length; i++) {
177
+ let c = str[i];
178
+ if (c === `
179
+ `)
180
+ return i;
181
+ if (c === "\r" && str[i + 1] === `
182
+ `)
183
+ return i + 1;
184
+ if (c < " " && c !== "\t" || c === "\x7F") {
185
+ throw new TomlError("control characters are not allowed in comments", {
186
+ toml: str,
187
+ ptr
188
+ });
189
+ }
190
+ }
191
+ return str.length;
192
+ }
193
+ function skipVoid(str, ptr, banNewLines, banComments) {
194
+ let c;
195
+ while ((c = str[ptr]) === " " || c === "\t" || !banNewLines && (c === `
196
+ ` || c === "\r" && str[ptr + 1] === `
197
+ `))
198
+ ptr++;
199
+ return banComments || c !== "#" ? ptr : skipVoid(str, skipComment(str, ptr), banNewLines);
200
+ }
201
+ function skipUntil(str, ptr, sep, end, banNewLines = false) {
202
+ if (!end) {
203
+ ptr = indexOfNewline(str, ptr);
204
+ return ptr < 0 ? str.length : ptr;
205
+ }
206
+ for (let i = ptr;i < str.length; i++) {
207
+ let c = str[i];
208
+ if (c === "#") {
209
+ i = indexOfNewline(str, i);
210
+ } else if (c === sep) {
211
+ return i + 1;
212
+ } else if (c === end || banNewLines && (c === `
213
+ ` || c === "\r" && str[i + 1] === `
214
+ `)) {
215
+ return i;
216
+ }
217
+ }
218
+ throw new TomlError("cannot find end of structure", {
219
+ toml: str,
220
+ ptr
221
+ });
222
+ }
223
+ function getStringEnd(str, seek) {
224
+ let first = str[seek];
225
+ let target = first === str[seek + 1] && str[seek + 1] === str[seek + 2] ? str.slice(seek, seek + 3) : first;
226
+ seek += target.length - 1;
227
+ do
228
+ seek = str.indexOf(target, ++seek);
229
+ while (seek > -1 && first !== "'" && isEscaped(str, seek));
230
+ if (seek > -1) {
231
+ seek += target.length;
232
+ if (target.length > 1) {
233
+ if (str[seek] === first)
234
+ seek++;
235
+ if (str[seek] === first)
236
+ seek++;
237
+ }
238
+ }
239
+ return seek;
240
+ }
241
+
242
+ // node_modules/smol-toml/dist/date.js
243
+ /*!
244
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
245
+ * SPDX-License-Identifier: BSD-3-Clause
246
+ *
247
+ * Redistribution and use in source and binary forms, with or without
248
+ * modification, are permitted provided that the following conditions are met:
249
+ *
250
+ * 1. Redistributions of source code must retain the above copyright notice, this
251
+ * list of conditions and the following disclaimer.
252
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
253
+ * this list of conditions and the following disclaimer in the
254
+ * documentation and/or other materials provided with the distribution.
255
+ * 3. Neither the name of the copyright holder nor the names of its contributors
256
+ * may be used to endorse or promote products derived from this software without
257
+ * specific prior written permission.
258
+ *
259
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
260
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
261
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
262
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
263
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
265
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
266
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
267
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
268
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269
+ */
270
+ var DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;
271
+
272
+ class TomlDate extends Date {
273
+ #hasDate = false;
274
+ #hasTime = false;
275
+ #offset = null;
276
+ constructor(date) {
277
+ let hasDate = true;
278
+ let hasTime = true;
279
+ let offset = "Z";
280
+ if (typeof date === "string") {
281
+ let match = date.match(DATE_TIME_RE);
282
+ if (match) {
283
+ if (!match[1]) {
284
+ hasDate = false;
285
+ date = `0000-01-01T${date}`;
286
+ }
287
+ hasTime = !!match[2];
288
+ hasTime && date[10] === " " && (date = date.replace(" ", "T"));
289
+ if (match[2] && +match[2] > 23) {
290
+ date = "";
291
+ } else {
292
+ offset = match[3] || null;
293
+ date = date.toUpperCase();
294
+ if (!offset && hasTime)
295
+ date += "Z";
296
+ }
297
+ } else {
298
+ date = "";
299
+ }
300
+ }
301
+ super(date);
302
+ if (!isNaN(this.getTime())) {
303
+ this.#hasDate = hasDate;
304
+ this.#hasTime = hasTime;
305
+ this.#offset = offset;
306
+ }
307
+ }
308
+ isDateTime() {
309
+ return this.#hasDate && this.#hasTime;
310
+ }
311
+ isLocal() {
312
+ return !this.#hasDate || !this.#hasTime || !this.#offset;
313
+ }
314
+ isDate() {
315
+ return this.#hasDate && !this.#hasTime;
316
+ }
317
+ isTime() {
318
+ return this.#hasTime && !this.#hasDate;
319
+ }
320
+ isValid() {
321
+ return this.#hasDate || this.#hasTime;
322
+ }
323
+ toISOString() {
324
+ let iso = super.toISOString();
325
+ if (this.isDate())
326
+ return iso.slice(0, 10);
327
+ if (this.isTime())
328
+ return iso.slice(11, 23);
329
+ if (this.#offset === null)
330
+ return iso.slice(0, -1);
331
+ if (this.#offset === "Z")
332
+ return iso;
333
+ let offset = +this.#offset.slice(1, 3) * 60 + +this.#offset.slice(4, 6);
334
+ offset = this.#offset[0] === "-" ? offset : -offset;
335
+ let offsetDate = new Date(this.getTime() - offset * 60000);
336
+ return offsetDate.toISOString().slice(0, -1) + this.#offset;
337
+ }
338
+ static wrapAsOffsetDateTime(jsDate, offset = "Z") {
339
+ let date = new TomlDate(jsDate);
340
+ date.#offset = offset;
341
+ return date;
342
+ }
343
+ static wrapAsLocalDateTime(jsDate) {
344
+ let date = new TomlDate(jsDate);
345
+ date.#offset = null;
346
+ return date;
347
+ }
348
+ static wrapAsLocalDate(jsDate) {
349
+ let date = new TomlDate(jsDate);
350
+ date.#hasTime = false;
351
+ date.#offset = null;
352
+ return date;
353
+ }
354
+ static wrapAsLocalTime(jsDate) {
355
+ let date = new TomlDate(jsDate);
356
+ date.#hasDate = false;
357
+ date.#offset = null;
358
+ return date;
359
+ }
360
+ }
361
+
362
+ // node_modules/smol-toml/dist/primitive.js
363
+ /*!
364
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
365
+ * SPDX-License-Identifier: BSD-3-Clause
366
+ *
367
+ * Redistribution and use in source and binary forms, with or without
368
+ * modification, are permitted provided that the following conditions are met:
369
+ *
370
+ * 1. Redistributions of source code must retain the above copyright notice, this
371
+ * list of conditions and the following disclaimer.
372
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
373
+ * this list of conditions and the following disclaimer in the
374
+ * documentation and/or other materials provided with the distribution.
375
+ * 3. Neither the name of the copyright holder nor the names of its contributors
376
+ * may be used to endorse or promote products derived from this software without
377
+ * specific prior written permission.
378
+ *
379
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
380
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
381
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
382
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
383
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
384
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
385
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
386
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
387
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
388
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
389
+ */
390
+ var INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/;
391
+ var FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
392
+ var LEADING_ZERO = /^[+-]?0[0-9_]/;
393
+ var ESCAPE_REGEX = /^[0-9a-f]{2,8}$/i;
394
+ var ESC_MAP = {
395
+ b: "\b",
396
+ t: "\t",
397
+ n: `
398
+ `,
399
+ f: "\f",
400
+ r: "\r",
401
+ e: "\x1B",
402
+ '"': '"',
403
+ "\\": "\\"
404
+ };
405
+ function parseString(str, ptr = 0, endPtr = str.length) {
406
+ let isLiteral = str[ptr] === "'";
407
+ let isMultiline = str[ptr++] === str[ptr] && str[ptr] === str[ptr + 1];
408
+ if (isMultiline) {
409
+ endPtr -= 2;
410
+ if (str[ptr += 2] === "\r")
411
+ ptr++;
412
+ if (str[ptr] === `
413
+ `)
414
+ ptr++;
415
+ }
416
+ let tmp = 0;
417
+ let isEscape;
418
+ let parsed = "";
419
+ let sliceStart = ptr;
420
+ while (ptr < endPtr - 1) {
421
+ let c = str[ptr++];
422
+ if (c === `
423
+ ` || c === "\r" && str[ptr] === `
424
+ `) {
425
+ if (!isMultiline) {
426
+ throw new TomlError("newlines are not allowed in strings", {
427
+ toml: str,
428
+ ptr: ptr - 1
429
+ });
430
+ }
431
+ } else if (c < " " && c !== "\t" || c === "\x7F") {
432
+ throw new TomlError("control characters are not allowed in strings", {
433
+ toml: str,
434
+ ptr: ptr - 1
435
+ });
436
+ }
437
+ if (isEscape) {
438
+ isEscape = false;
439
+ if (c === "x" || c === "u" || c === "U") {
440
+ let code = str.slice(ptr, ptr += c === "x" ? 2 : c === "u" ? 4 : 8);
441
+ if (!ESCAPE_REGEX.test(code)) {
442
+ throw new TomlError("invalid unicode escape", {
443
+ toml: str,
444
+ ptr: tmp
445
+ });
446
+ }
447
+ try {
448
+ parsed += String.fromCodePoint(parseInt(code, 16));
449
+ } catch {
450
+ throw new TomlError("invalid unicode escape", {
451
+ toml: str,
452
+ ptr: tmp
453
+ });
454
+ }
455
+ } else if (isMultiline && (c === `
456
+ ` || c === " " || c === "\t" || c === "\r")) {
457
+ ptr = skipVoid(str, ptr - 1, true);
458
+ if (str[ptr] !== `
459
+ ` && str[ptr] !== "\r") {
460
+ throw new TomlError("invalid escape: only line-ending whitespace may be escaped", {
461
+ toml: str,
462
+ ptr: tmp
463
+ });
464
+ }
465
+ ptr = skipVoid(str, ptr);
466
+ } else if (c in ESC_MAP) {
467
+ parsed += ESC_MAP[c];
468
+ } else {
469
+ throw new TomlError("unrecognized escape sequence", {
470
+ toml: str,
471
+ ptr: tmp
472
+ });
473
+ }
474
+ sliceStart = ptr;
475
+ } else if (!isLiteral && c === "\\") {
476
+ tmp = ptr - 1;
477
+ isEscape = true;
478
+ parsed += str.slice(sliceStart, tmp);
479
+ }
480
+ }
481
+ return parsed + str.slice(sliceStart, endPtr - 1);
482
+ }
483
+ function parseValue(value, toml, ptr, integersAsBigInt) {
484
+ if (value === "true")
485
+ return true;
486
+ if (value === "false")
487
+ return false;
488
+ if (value === "-inf")
489
+ return -Infinity;
490
+ if (value === "inf" || value === "+inf")
491
+ return Infinity;
492
+ if (value === "nan" || value === "+nan" || value === "-nan")
493
+ return NaN;
494
+ if (value === "-0")
495
+ return integersAsBigInt ? 0n : 0;
496
+ let isInt = INT_REGEX.test(value);
497
+ if (isInt || FLOAT_REGEX.test(value)) {
498
+ if (LEADING_ZERO.test(value)) {
499
+ throw new TomlError("leading zeroes are not allowed", {
500
+ toml,
501
+ ptr
502
+ });
503
+ }
504
+ value = value.replace(/_/g, "");
505
+ let numeric = +value;
506
+ if (isNaN(numeric)) {
507
+ throw new TomlError("invalid number", {
508
+ toml,
509
+ ptr
510
+ });
511
+ }
512
+ if (isInt) {
513
+ if ((isInt = !Number.isSafeInteger(numeric)) && !integersAsBigInt) {
514
+ throw new TomlError("integer value cannot be represented losslessly", {
515
+ toml,
516
+ ptr
517
+ });
518
+ }
519
+ if (isInt || integersAsBigInt === true)
520
+ numeric = BigInt(value);
521
+ }
522
+ return numeric;
523
+ }
524
+ const date = new TomlDate(value);
525
+ if (!date.isValid()) {
526
+ throw new TomlError("invalid value", {
527
+ toml,
528
+ ptr
529
+ });
530
+ }
531
+ return date;
532
+ }
533
+
534
+ // node_modules/smol-toml/dist/extract.js
535
+ /*!
536
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
537
+ * SPDX-License-Identifier: BSD-3-Clause
538
+ *
539
+ * Redistribution and use in source and binary forms, with or without
540
+ * modification, are permitted provided that the following conditions are met:
541
+ *
542
+ * 1. Redistributions of source code must retain the above copyright notice, this
543
+ * list of conditions and the following disclaimer.
544
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
545
+ * this list of conditions and the following disclaimer in the
546
+ * documentation and/or other materials provided with the distribution.
547
+ * 3. Neither the name of the copyright holder nor the names of its contributors
548
+ * may be used to endorse or promote products derived from this software without
549
+ * specific prior written permission.
550
+ *
551
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
552
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
553
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
554
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
555
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
556
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
557
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
558
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
559
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
560
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
561
+ */
562
+ function sliceAndTrimEndOf(str, startPtr, endPtr) {
563
+ let value = str.slice(startPtr, endPtr);
564
+ let commentIdx = value.indexOf("#");
565
+ if (commentIdx > -1) {
566
+ skipComment(str, commentIdx);
567
+ value = value.slice(0, commentIdx);
568
+ }
569
+ return [value.trimEnd(), commentIdx];
570
+ }
571
+ function extractValue(str, ptr, end, depth, integersAsBigInt) {
572
+ if (depth === 0) {
573
+ throw new TomlError("document contains excessively nested structures. aborting.", {
574
+ toml: str,
575
+ ptr
576
+ });
577
+ }
578
+ let c = str[ptr];
579
+ if (c === "[" || c === "{") {
580
+ let [value, endPtr2] = c === "[" ? parseArray(str, ptr, depth, integersAsBigInt) : parseInlineTable(str, ptr, depth, integersAsBigInt);
581
+ if (end) {
582
+ endPtr2 = skipVoid(str, endPtr2);
583
+ if (str[endPtr2] === ",")
584
+ endPtr2++;
585
+ else if (str[endPtr2] !== end) {
586
+ throw new TomlError("expected comma or end of structure", {
587
+ toml: str,
588
+ ptr: endPtr2
589
+ });
590
+ }
591
+ }
592
+ return [value, endPtr2];
593
+ }
594
+ let endPtr;
595
+ if (c === '"' || c === "'") {
596
+ endPtr = getStringEnd(str, ptr);
597
+ let parsed = parseString(str, ptr, endPtr);
598
+ if (end) {
599
+ endPtr = skipVoid(str, endPtr);
600
+ if (str[endPtr] && str[endPtr] !== "," && str[endPtr] !== end && str[endPtr] !== `
601
+ ` && str[endPtr] !== "\r") {
602
+ throw new TomlError("unexpected character encountered", {
603
+ toml: str,
604
+ ptr: endPtr
605
+ });
606
+ }
607
+ endPtr += +(str[endPtr] === ",");
608
+ }
609
+ return [parsed, endPtr];
610
+ }
611
+ endPtr = skipUntil(str, ptr, ",", end);
612
+ let slice = sliceAndTrimEndOf(str, ptr, endPtr - +(str[endPtr - 1] === ","));
613
+ if (!slice[0]) {
614
+ throw new TomlError("incomplete key-value declaration: no value specified", {
615
+ toml: str,
616
+ ptr
617
+ });
618
+ }
619
+ if (end && slice[1] > -1) {
620
+ endPtr = skipVoid(str, ptr + slice[1]);
621
+ endPtr += +(str[endPtr] === ",");
622
+ }
623
+ return [
624
+ parseValue(slice[0], str, ptr, integersAsBigInt),
625
+ endPtr
626
+ ];
627
+ }
628
+
629
+ // node_modules/smol-toml/dist/struct.js
630
+ /*!
631
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
632
+ * SPDX-License-Identifier: BSD-3-Clause
633
+ *
634
+ * Redistribution and use in source and binary forms, with or without
635
+ * modification, are permitted provided that the following conditions are met:
636
+ *
637
+ * 1. Redistributions of source code must retain the above copyright notice, this
638
+ * list of conditions and the following disclaimer.
639
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
640
+ * this list of conditions and the following disclaimer in the
641
+ * documentation and/or other materials provided with the distribution.
642
+ * 3. Neither the name of the copyright holder nor the names of its contributors
643
+ * may be used to endorse or promote products derived from this software without
644
+ * specific prior written permission.
645
+ *
646
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
647
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
648
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
649
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
650
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
651
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
652
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
653
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
654
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
655
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
656
+ */
657
+ var KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
658
+ function parseKey(str, ptr, end = "=") {
659
+ let dot = ptr - 1;
660
+ let parsed = [];
661
+ let endPtr = str.indexOf(end, ptr);
662
+ if (endPtr < 0) {
663
+ throw new TomlError("incomplete key-value: cannot find end of key", {
664
+ toml: str,
665
+ ptr
666
+ });
667
+ }
668
+ do {
669
+ let c = str[ptr = ++dot];
670
+ if (c !== " " && c !== "\t") {
671
+ if (c === '"' || c === "'") {
672
+ if (c === str[ptr + 1] && c === str[ptr + 2]) {
673
+ throw new TomlError("multiline strings are not allowed in keys", {
674
+ toml: str,
675
+ ptr
676
+ });
677
+ }
678
+ let eos = getStringEnd(str, ptr);
679
+ if (eos < 0) {
680
+ throw new TomlError("unfinished string encountered", {
681
+ toml: str,
682
+ ptr
683
+ });
684
+ }
685
+ dot = str.indexOf(".", eos);
686
+ let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
687
+ let newLine = indexOfNewline(strEnd);
688
+ if (newLine > -1) {
689
+ throw new TomlError("newlines are not allowed in keys", {
690
+ toml: str,
691
+ ptr: ptr + dot + newLine
692
+ });
693
+ }
694
+ if (strEnd.trimStart()) {
695
+ throw new TomlError("found extra tokens after the string part", {
696
+ toml: str,
697
+ ptr: eos
698
+ });
699
+ }
700
+ if (endPtr < eos) {
701
+ endPtr = str.indexOf(end, eos);
702
+ if (endPtr < 0) {
703
+ throw new TomlError("incomplete key-value: cannot find end of key", {
704
+ toml: str,
705
+ ptr
706
+ });
707
+ }
708
+ }
709
+ parsed.push(parseString(str, ptr, eos));
710
+ } else {
711
+ dot = str.indexOf(".", ptr);
712
+ let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
713
+ if (!KEY_PART_RE.test(part)) {
714
+ throw new TomlError("only letter, numbers, dashes and underscores are allowed in keys", {
715
+ toml: str,
716
+ ptr
717
+ });
718
+ }
719
+ parsed.push(part.trimEnd());
720
+ }
721
+ }
722
+ } while (dot + 1 && dot < endPtr);
723
+ return [parsed, skipVoid(str, endPtr + 1, true, true)];
724
+ }
725
+ function parseInlineTable(str, ptr, depth, integersAsBigInt) {
726
+ let res = {};
727
+ let seen = new Set;
728
+ let c;
729
+ ptr++;
730
+ while ((c = str[ptr++]) !== "}" && c) {
731
+ if (c === ",") {
732
+ throw new TomlError("expected value, found comma", {
733
+ toml: str,
734
+ ptr: ptr - 1
735
+ });
736
+ } else if (c === "#")
737
+ ptr = skipComment(str, ptr);
738
+ else if (c !== " " && c !== "\t" && c !== `
739
+ ` && c !== "\r") {
740
+ let k;
741
+ let t = res;
742
+ let hasOwn = false;
743
+ let [key, keyEndPtr] = parseKey(str, ptr - 1);
744
+ for (let i = 0;i < key.length; i++) {
745
+ if (i)
746
+ t = hasOwn ? t[k] : t[k] = {};
747
+ k = key[i];
748
+ if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== "object" || seen.has(t[k]))) {
749
+ throw new TomlError("trying to redefine an already defined value", {
750
+ toml: str,
751
+ ptr
752
+ });
753
+ }
754
+ if (!hasOwn && k === "__proto__") {
755
+ Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
756
+ }
757
+ }
758
+ if (hasOwn) {
759
+ throw new TomlError("trying to redefine an already defined value", {
760
+ toml: str,
761
+ ptr
762
+ });
763
+ }
764
+ let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}", depth - 1, integersAsBigInt);
765
+ seen.add(value);
766
+ t[k] = value;
767
+ ptr = valueEndPtr;
768
+ }
769
+ }
770
+ if (!c) {
771
+ throw new TomlError("unfinished table encountered", {
772
+ toml: str,
773
+ ptr
774
+ });
775
+ }
776
+ return [res, ptr];
777
+ }
778
+ function parseArray(str, ptr, depth, integersAsBigInt) {
779
+ let res = [];
780
+ let c;
781
+ ptr++;
782
+ while ((c = str[ptr++]) !== "]" && c) {
783
+ if (c === ",") {
784
+ throw new TomlError("expected value, found comma", {
785
+ toml: str,
786
+ ptr: ptr - 1
787
+ });
788
+ } else if (c === "#")
789
+ ptr = skipComment(str, ptr);
790
+ else if (c !== " " && c !== "\t" && c !== `
791
+ ` && c !== "\r") {
792
+ let e = extractValue(str, ptr - 1, "]", depth - 1, integersAsBigInt);
793
+ res.push(e[0]);
794
+ ptr = e[1];
795
+ }
796
+ }
797
+ if (!c) {
798
+ throw new TomlError("unfinished array encountered", {
799
+ toml: str,
800
+ ptr
801
+ });
802
+ }
803
+ return [res, ptr];
804
+ }
805
+
806
+ // node_modules/smol-toml/dist/parse.js
807
+ /*!
808
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
809
+ * SPDX-License-Identifier: BSD-3-Clause
810
+ *
811
+ * Redistribution and use in source and binary forms, with or without
812
+ * modification, are permitted provided that the following conditions are met:
813
+ *
814
+ * 1. Redistributions of source code must retain the above copyright notice, this
815
+ * list of conditions and the following disclaimer.
816
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
817
+ * this list of conditions and the following disclaimer in the
818
+ * documentation and/or other materials provided with the distribution.
819
+ * 3. Neither the name of the copyright holder nor the names of its contributors
820
+ * may be used to endorse or promote products derived from this software without
821
+ * specific prior written permission.
822
+ *
823
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
824
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
825
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
826
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
827
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
828
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
829
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
830
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
831
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
832
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
833
+ */
834
+ function peekTable(key, table, meta, type) {
835
+ let t = table;
836
+ let m = meta;
837
+ let k;
838
+ let hasOwn = false;
839
+ let state;
840
+ for (let i = 0;i < key.length; i++) {
841
+ if (i) {
842
+ t = hasOwn ? t[k] : t[k] = {};
843
+ m = (state = m[k]).c;
844
+ if (type === 0 && (state.t === 1 || state.t === 2)) {
845
+ return null;
846
+ }
847
+ if (state.t === 2) {
848
+ let l = t.length - 1;
849
+ t = t[l];
850
+ m = m[l].c;
851
+ }
852
+ }
853
+ k = key[i];
854
+ if ((hasOwn = Object.hasOwn(t, k)) && m[k]?.t === 0 && m[k]?.d) {
855
+ return null;
856
+ }
857
+ if (!hasOwn) {
858
+ if (k === "__proto__") {
859
+ Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
860
+ Object.defineProperty(m, k, { enumerable: true, configurable: true, writable: true });
861
+ }
862
+ m[k] = {
863
+ t: i < key.length - 1 && type === 2 ? 3 : type,
864
+ d: false,
865
+ i: 0,
866
+ c: {}
867
+ };
868
+ }
869
+ }
870
+ state = m[k];
871
+ if (state.t !== type && !(type === 1 && state.t === 3)) {
872
+ return null;
873
+ }
874
+ if (type === 2) {
875
+ if (!state.d) {
876
+ state.d = true;
877
+ t[k] = [];
878
+ }
879
+ t[k].push(t = {});
880
+ state.c[state.i++] = state = { t: 1, d: false, i: 0, c: {} };
881
+ }
882
+ if (state.d) {
883
+ return null;
884
+ }
885
+ state.d = true;
886
+ if (type === 1) {
887
+ t = hasOwn ? t[k] : t[k] = {};
888
+ } else if (type === 0 && hasOwn) {
889
+ return null;
890
+ }
891
+ return [k, t, state.c];
892
+ }
893
+ function parse(toml, { maxDepth = 1000, integersAsBigInt } = {}) {
894
+ let res = {};
895
+ let meta = {};
896
+ let tbl = res;
897
+ let m = meta;
898
+ for (let ptr = skipVoid(toml, 0);ptr < toml.length; ) {
899
+ if (toml[ptr] === "[") {
900
+ let isTableArray = toml[++ptr] === "[";
901
+ let k = parseKey(toml, ptr += +isTableArray, "]");
902
+ if (isTableArray) {
903
+ if (toml[k[1] - 1] !== "]") {
904
+ throw new TomlError("expected end of table declaration", {
905
+ toml,
906
+ ptr: k[1] - 1
907
+ });
908
+ }
909
+ k[1]++;
910
+ }
911
+ let p = peekTable(k[0], res, meta, isTableArray ? 2 : 1);
912
+ if (!p) {
913
+ throw new TomlError("trying to redefine an already defined table or value", {
914
+ toml,
915
+ ptr
916
+ });
917
+ }
918
+ m = p[2];
919
+ tbl = p[1];
920
+ ptr = k[1];
921
+ } else {
922
+ let k = parseKey(toml, ptr);
923
+ let p = peekTable(k[0], tbl, m, 0);
924
+ if (!p) {
925
+ throw new TomlError("trying to redefine an already defined table or value", {
926
+ toml,
927
+ ptr
928
+ });
929
+ }
930
+ let v = extractValue(toml, k[1], undefined, maxDepth, integersAsBigInt);
931
+ p[1][p[0]] = v[0];
932
+ ptr = v[1];
933
+ }
934
+ ptr = skipVoid(toml, ptr, true);
935
+ if (toml[ptr] && toml[ptr] !== `
936
+ ` && toml[ptr] !== "\r") {
937
+ throw new TomlError("each key-value declaration must be followed by an end-of-line", {
938
+ toml,
939
+ ptr
940
+ });
941
+ }
942
+ ptr = skipVoid(toml, ptr);
943
+ }
944
+ return res;
945
+ }
946
+
947
+ // node_modules/smol-toml/dist/stringify.js
948
+ /*!
949
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
950
+ * SPDX-License-Identifier: BSD-3-Clause
951
+ *
952
+ * Redistribution and use in source and binary forms, with or without
953
+ * modification, are permitted provided that the following conditions are met:
954
+ *
955
+ * 1. Redistributions of source code must retain the above copyright notice, this
956
+ * list of conditions and the following disclaimer.
957
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
958
+ * this list of conditions and the following disclaimer in the
959
+ * documentation and/or other materials provided with the distribution.
960
+ * 3. Neither the name of the copyright holder nor the names of its contributors
961
+ * may be used to endorse or promote products derived from this software without
962
+ * specific prior written permission.
963
+ *
964
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
965
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
966
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
967
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
968
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
969
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
970
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
971
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
972
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
973
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
974
+ */
975
+ var BARE_KEY = /^[a-z0-9-_]+$/i;
976
+ function extendedTypeOf(obj) {
977
+ let type = typeof obj;
978
+ if (type === "object") {
979
+ if (Array.isArray(obj))
980
+ return "array";
981
+ if (obj instanceof Date)
982
+ return "date";
983
+ }
984
+ return type;
985
+ }
986
+ function isArrayOfTables(obj) {
987
+ for (let i = 0;i < obj.length; i++) {
988
+ if (extendedTypeOf(obj[i]) !== "object")
989
+ return false;
990
+ }
991
+ return obj.length != 0;
992
+ }
993
+ function formatString(s) {
994
+ return JSON.stringify(s).replace(/\x7f/g, "\\u007f");
995
+ }
996
+ function stringifyValue(val, type, depth, numberAsFloat) {
997
+ if (depth === 0) {
998
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
999
+ }
1000
+ if (type === "number") {
1001
+ if (isNaN(val))
1002
+ return "nan";
1003
+ if (val === Infinity)
1004
+ return "inf";
1005
+ if (val === -Infinity)
1006
+ return "-inf";
1007
+ if (numberAsFloat && Number.isInteger(val))
1008
+ return val.toFixed(1);
1009
+ return val.toString();
1010
+ }
1011
+ if (type === "bigint" || type === "boolean") {
1012
+ return val.toString();
1013
+ }
1014
+ if (type === "string") {
1015
+ return formatString(val);
1016
+ }
1017
+ if (type === "date") {
1018
+ if (isNaN(val.getTime())) {
1019
+ throw new TypeError("cannot serialize invalid date");
1020
+ }
1021
+ return val.toISOString();
1022
+ }
1023
+ if (type === "object") {
1024
+ return stringifyInlineTable(val, depth, numberAsFloat);
1025
+ }
1026
+ if (type === "array") {
1027
+ return stringifyArray(val, depth, numberAsFloat);
1028
+ }
1029
+ }
1030
+ function stringifyInlineTable(obj, depth, numberAsFloat) {
1031
+ let keys = Object.keys(obj);
1032
+ if (keys.length === 0)
1033
+ return "{}";
1034
+ let res = "{ ";
1035
+ for (let i = 0;i < keys.length; i++) {
1036
+ let k = keys[i];
1037
+ if (i)
1038
+ res += ", ";
1039
+ res += BARE_KEY.test(k) ? k : formatString(k);
1040
+ res += " = ";
1041
+ res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1, numberAsFloat);
1042
+ }
1043
+ return res + " }";
1044
+ }
1045
+ function stringifyArray(array, depth, numberAsFloat) {
1046
+ if (array.length === 0)
1047
+ return "[]";
1048
+ let res = "[ ";
1049
+ for (let i = 0;i < array.length; i++) {
1050
+ if (i)
1051
+ res += ", ";
1052
+ if (array[i] === null || array[i] === undefined) {
1053
+ throw new TypeError("arrays cannot contain null or undefined values");
1054
+ }
1055
+ res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1, numberAsFloat);
1056
+ }
1057
+ return res + " ]";
1058
+ }
1059
+ function stringifyArrayTable(array, key, depth, numberAsFloat) {
1060
+ if (depth === 0) {
1061
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
1062
+ }
1063
+ let res = "";
1064
+ for (let i = 0;i < array.length; i++) {
1065
+ res += `${res && `
1066
+ `}[[${key}]]
1067
+ `;
1068
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
1069
+ }
1070
+ return res;
1071
+ }
1072
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
1073
+ if (depth === 0) {
1074
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
1075
+ }
1076
+ let preamble = "";
1077
+ let tables = "";
1078
+ let keys = Object.keys(obj);
1079
+ for (let i = 0;i < keys.length; i++) {
1080
+ let k = keys[i];
1081
+ if (obj[k] !== null && obj[k] !== undefined) {
1082
+ let type = extendedTypeOf(obj[k]);
1083
+ if (type === "symbol" || type === "function") {
1084
+ throw new TypeError(`cannot serialize values of type '${type}'`);
1085
+ }
1086
+ let key = BARE_KEY.test(k) ? k : formatString(k);
1087
+ if (type === "array" && isArrayOfTables(obj[k])) {
1088
+ tables += (tables && `
1089
+ `) + stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
1090
+ } else if (type === "object") {
1091
+ let tblKey = prefix ? `${prefix}.${key}` : key;
1092
+ tables += (tables && `
1093
+ `) + stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
1094
+ } else {
1095
+ preamble += key;
1096
+ preamble += " = ";
1097
+ preamble += stringifyValue(obj[k], type, depth, numberAsFloat);
1098
+ preamble += `
1099
+ `;
1100
+ }
1101
+ }
1102
+ }
1103
+ if (tableKey && (preamble || !tables))
1104
+ preamble = preamble ? `[${tableKey}]
1105
+ ${preamble}` : `[${tableKey}]`;
1106
+ return preamble && tables ? `${preamble}
1107
+ ${tables}` : preamble || tables;
1108
+ }
1109
+ function stringify(obj, { maxDepth = 1000, numbersAsFloat = false } = {}) {
1110
+ if (extendedTypeOf(obj) !== "object") {
1111
+ throw new TypeError("stringify can only be called with an object");
1112
+ }
1113
+ let str = stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
1114
+ if (str[str.length - 1] !== `
1115
+ `)
1116
+ return str + `
1117
+ `;
1118
+ return str;
1119
+ }
1120
+
1121
+ // node_modules/smol-toml/dist/index.js
1122
+ /*!
1123
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
1124
+ * SPDX-License-Identifier: BSD-3-Clause
1125
+ *
1126
+ * Redistribution and use in source and binary forms, with or without
1127
+ * modification, are permitted provided that the following conditions are met:
1128
+ *
1129
+ * 1. Redistributions of source code must retain the above copyright notice, this
1130
+ * list of conditions and the following disclaimer.
1131
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
1132
+ * this list of conditions and the following disclaimer in the
1133
+ * documentation and/or other materials provided with the distribution.
1134
+ * 3. Neither the name of the copyright holder nor the names of its contributors
1135
+ * may be used to endorse or promote products derived from this software without
1136
+ * specific prior written permission.
1137
+ *
1138
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1139
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1140
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1141
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
1142
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1143
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1144
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1145
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
1146
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1147
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1148
+ */
1149
+
1150
+ // src/core/frontmatter.ts
1151
+ var FRONTMATTER_DELIMITER = "+++";
1152
+ function parseStitchFile(content) {
1153
+ const lines = content.split(`
1154
+ `);
1155
+ if (lines[0] !== FRONTMATTER_DELIMITER) {
1156
+ throw new ValidationError("Invalid stitch file: missing frontmatter start");
1157
+ }
1158
+ let endIndex = -1;
1159
+ for (let i = 1;i < lines.length; i++) {
1160
+ if (lines[i] === FRONTMATTER_DELIMITER) {
1161
+ endIndex = i;
1162
+ break;
1163
+ }
1164
+ }
1165
+ if (endIndex === -1) {
1166
+ throw new ValidationError("Invalid stitch file: missing frontmatter end");
1167
+ }
1168
+ const frontmatterContent = lines.slice(1, endIndex).join(`
1169
+ `);
1170
+ const body = lines.slice(endIndex + 1).join(`
1171
+ `).trim();
1172
+ const parsed = parse(frontmatterContent);
1173
+ const frontmatter = toStitchFrontmatter(parsed);
1174
+ return { frontmatter, body };
1175
+ }
1176
+ function toStitchFrontmatter(obj) {
1177
+ if (typeof obj["id"] !== "string") {
1178
+ throw new ValidationError("Missing or invalid 'id' field");
1179
+ }
1180
+ if (typeof obj["title"] !== "string") {
1181
+ throw new ValidationError("Missing or invalid 'title' field");
1182
+ }
1183
+ if (typeof obj["status"] !== "string") {
1184
+ throw new ValidationError("Missing or invalid 'status' field");
1185
+ }
1186
+ if (typeof obj["created_at"] !== "string") {
1187
+ throw new ValidationError("Missing or invalid 'created_at' field");
1188
+ }
1189
+ if (typeof obj["updated_at"] !== "string") {
1190
+ throw new ValidationError("Missing or invalid 'updated_at' field");
1191
+ }
1192
+ const frontmatter = {
1193
+ id: obj["id"],
1194
+ title: obj["title"],
1195
+ status: obj["status"],
1196
+ created_at: obj["created_at"],
1197
+ updated_at: obj["updated_at"]
1198
+ };
1199
+ if (typeof obj["provenance"] === "string") {
1200
+ frontmatter.provenance = obj["provenance"];
1201
+ }
1202
+ if (typeof obj["confidence"] === "string") {
1203
+ frontmatter.confidence = obj["confidence"];
1204
+ }
1205
+ if (Array.isArray(obj["tags"])) {
1206
+ frontmatter.tags = obj["tags"];
1207
+ }
1208
+ const scope = obj["scope"];
1209
+ if (scope && typeof scope === "object" && !Array.isArray(scope)) {
1210
+ const scopeObj = scope;
1211
+ frontmatter.scope = {};
1212
+ if (Array.isArray(scopeObj["paths"])) {
1213
+ frontmatter.scope.paths = scopeObj["paths"];
1214
+ }
1215
+ }
1216
+ const relations = obj["relations"];
1217
+ if (relations && typeof relations === "object" && !Array.isArray(relations)) {
1218
+ const relObj = relations;
1219
+ frontmatter.relations = {};
1220
+ if (typeof relObj["parent"] === "string") {
1221
+ frontmatter.relations.parent = relObj["parent"];
1222
+ }
1223
+ if (Array.isArray(relObj["depends_on"])) {
1224
+ frontmatter.relations.depends_on = relObj["depends_on"];
1225
+ }
1226
+ }
1227
+ const git = obj["git"];
1228
+ if (git && typeof git === "object" && !Array.isArray(git)) {
1229
+ const gitObj = git;
1230
+ frontmatter.git = {};
1231
+ if (Array.isArray(gitObj["links"])) {
1232
+ frontmatter.git.links = gitObj["links"].map(parseGitLink);
1233
+ }
1234
+ if (Array.isArray(gitObj["fingerprints"])) {
1235
+ frontmatter.git.fingerprints = gitObj["fingerprints"].map(parseFingerprint);
1236
+ }
1237
+ }
1238
+ return frontmatter;
1239
+ }
1240
+ function parseGitLink(obj) {
1241
+ if (obj["kind"] === "commit" && typeof obj["sha"] === "string") {
1242
+ return { kind: "commit", sha: obj["sha"] };
1243
+ }
1244
+ if (obj["kind"] === "range" && typeof obj["range"] === "string") {
1245
+ return { kind: "range", range: obj["range"] };
1246
+ }
1247
+ throw new ValidationError(`Invalid git link: ${JSON.stringify(obj)}`);
1248
+ }
1249
+ function parseFingerprint(obj) {
1250
+ if (obj["algo"] === "sha256" && (obj["kind"] === "staged-diff" || obj["kind"] === "unified-diff") && typeof obj["value"] === "string") {
1251
+ return {
1252
+ algo: obj["algo"],
1253
+ kind: obj["kind"],
1254
+ value: obj["value"]
1255
+ };
1256
+ }
1257
+ throw new ValidationError(`Invalid fingerprint: ${JSON.stringify(obj)}`);
1258
+ }
1259
+ function serializeStitchFile(frontmatter, body) {
1260
+ const tomlObj = frontmatterToToml(frontmatter);
1261
+ const tomlStr = stringify(tomlObj);
1262
+ return `${FRONTMATTER_DELIMITER}
1263
+ ${tomlStr}${FRONTMATTER_DELIMITER}
1264
+
1265
+ ${body}
1266
+ `;
1267
+ }
1268
+ function frontmatterToToml(fm) {
1269
+ const obj = {
1270
+ id: fm.id,
1271
+ title: fm.title,
1272
+ status: fm.status,
1273
+ created_at: fm.created_at,
1274
+ updated_at: fm.updated_at
1275
+ };
1276
+ if (fm.provenance)
1277
+ obj["provenance"] = fm.provenance;
1278
+ if (fm.confidence)
1279
+ obj["confidence"] = fm.confidence;
1280
+ if (fm.tags && fm.tags.length > 0)
1281
+ obj["tags"] = fm.tags;
1282
+ if (fm.scope && Object.keys(fm.scope).length > 0) {
1283
+ obj["scope"] = fm.scope;
1284
+ }
1285
+ if (fm.relations) {
1286
+ const relations = {};
1287
+ if (fm.relations.parent)
1288
+ relations["parent"] = fm.relations.parent;
1289
+ if (fm.relations.depends_on && fm.relations.depends_on.length > 0) {
1290
+ relations["depends_on"] = fm.relations.depends_on;
1291
+ }
1292
+ if (Object.keys(relations).length > 0) {
1293
+ obj["relations"] = relations;
1294
+ }
1295
+ }
1296
+ if (fm.git) {
1297
+ const git = {};
1298
+ if (fm.git.links && fm.git.links.length > 0) {
1299
+ git["links"] = fm.git.links;
1300
+ }
1301
+ if (fm.git.fingerprints && fm.git.fingerprints.length > 0) {
1302
+ git["fingerprints"] = fm.git.fingerprints;
1303
+ }
1304
+ if (Object.keys(git).length > 0) {
1305
+ obj["git"] = git;
1306
+ }
1307
+ }
1308
+ return obj;
1309
+ }
1310
+ function updateTimestamp(frontmatter) {
1311
+ return {
1312
+ ...frontmatter,
1313
+ updated_at: new Date().toISOString()
1314
+ };
1315
+ }
1316
+
1317
+ // src/core/ids.ts
1318
+ function generateStitchId() {
1319
+ const now = new Date;
1320
+ const dateStr = formatDate(now);
1321
+ const suffix = generateHexSuffix(4);
1322
+ return `S-${dateStr}-${suffix}`;
1323
+ }
1324
+ function formatDate(date) {
1325
+ const year = date.getFullYear();
1326
+ const month = String(date.getMonth() + 1).padStart(2, "0");
1327
+ const day = String(date.getDate()).padStart(2, "0");
1328
+ return `${year}${month}${day}`;
1329
+ }
1330
+ function generateHexSuffix(length) {
1331
+ const bytes = new Uint8Array(Math.ceil(length / 2));
1332
+ crypto.getRandomValues(bytes);
1333
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, length);
1334
+ }
1335
+
1336
+ // src/core/model.ts
1337
+ var DEFAULT_STITCH_BODY = `## Intent
1338
+
1339
+ [Describe the goal or purpose of this change]
1340
+
1341
+ ## Constraints
1342
+
1343
+ - [List any constraints or requirements]
1344
+
1345
+ ## Alternatives
1346
+
1347
+ - [Document alternative approaches considered]
1348
+
1349
+ ## Notes
1350
+
1351
+ [Additional context or information]
1352
+ `;
1353
+
1354
+ // src/core/store.ts
1355
+ var STITCH_DIR = ".stitch";
1356
+ var STITCHES_SUBDIR = "stitches";
1357
+ var CURRENT_FILE = "current";
1358
+ function getStitchDir(repoRoot) {
1359
+ return join(repoRoot, STITCH_DIR);
1360
+ }
1361
+ function getStitchesDir(repoRoot) {
1362
+ return join(repoRoot, STITCH_DIR, STITCHES_SUBDIR);
1363
+ }
1364
+ function getCurrentFilePath(repoRoot) {
1365
+ return join(repoRoot, STITCH_DIR, CURRENT_FILE);
1366
+ }
1367
+ function isInitialized(repoRoot) {
1368
+ return existsSync(getStitchDir(repoRoot));
1369
+ }
1370
+ async function initializeStitch(repoRoot) {
1371
+ const stitchesDir = getStitchesDir(repoRoot);
1372
+ const currentPath = getCurrentFilePath(repoRoot);
1373
+ await mkdir(stitchesDir, { recursive: true });
1374
+ if (!existsSync(currentPath)) {
1375
+ await writeFile(currentPath, "", "utf-8");
1376
+ }
1377
+ }
1378
+ async function getCurrentStitchId(repoRoot) {
1379
+ if (!isInitialized(repoRoot)) {
1380
+ throw new NotInitializedError;
1381
+ }
1382
+ const currentPath = getCurrentFilePath(repoRoot);
1383
+ const content = await readFile(currentPath, "utf-8");
1384
+ const trimmed = content.trim();
1385
+ return trimmed || null;
1386
+ }
1387
+ async function setCurrentStitchId(repoRoot, id) {
1388
+ if (!isInitialized(repoRoot)) {
1389
+ throw new NotInitializedError;
1390
+ }
1391
+ const currentPath = getCurrentFilePath(repoRoot);
1392
+ await writeFile(currentPath, id ?? "", "utf-8");
1393
+ }
1394
+ function getStitchFilePath(repoRoot, id) {
1395
+ return join(getStitchesDir(repoRoot), `${id}.md`);
1396
+ }
1397
+ async function createStitch(repoRoot, title, parentId) {
1398
+ if (!isInitialized(repoRoot)) {
1399
+ throw new NotInitializedError;
1400
+ }
1401
+ const id = generateStitchId();
1402
+ const now = new Date().toISOString();
1403
+ const frontmatter = {
1404
+ id,
1405
+ title,
1406
+ status: "open",
1407
+ created_at: now,
1408
+ updated_at: now,
1409
+ provenance: "human",
1410
+ confidence: "medium"
1411
+ };
1412
+ if (parentId) {
1413
+ frontmatter.relations = { parent: parentId };
1414
+ }
1415
+ const filePath = getStitchFilePath(repoRoot, id);
1416
+ const content = serializeStitchFile(frontmatter, DEFAULT_STITCH_BODY);
1417
+ await writeFile(filePath, content, "utf-8");
1418
+ return {
1419
+ frontmatter,
1420
+ body: DEFAULT_STITCH_BODY,
1421
+ filePath
1422
+ };
1423
+ }
1424
+ async function loadStitch(repoRoot, id) {
1425
+ if (!isInitialized(repoRoot)) {
1426
+ throw new NotInitializedError;
1427
+ }
1428
+ const filePath = getStitchFilePath(repoRoot, id);
1429
+ if (!existsSync(filePath)) {
1430
+ throw new StitchNotFoundError(id);
1431
+ }
1432
+ const content = await readFile(filePath, "utf-8");
1433
+ const { frontmatter, body } = parseStitchFile(content);
1434
+ return { frontmatter, body, filePath };
1435
+ }
1436
+ async function saveStitch(repoRoot, doc) {
1437
+ if (!isInitialized(repoRoot)) {
1438
+ throw new NotInitializedError;
1439
+ }
1440
+ const updatedFrontmatter = updateTimestamp(doc.frontmatter);
1441
+ const content = serializeStitchFile(updatedFrontmatter, doc.body);
1442
+ await writeFile(doc.filePath, content, "utf-8");
1443
+ return {
1444
+ ...doc,
1445
+ frontmatter: updatedFrontmatter
1446
+ };
1447
+ }
1448
+ async function listStitches(repoRoot, filter) {
1449
+ if (!isInitialized(repoRoot)) {
1450
+ throw new NotInitializedError;
1451
+ }
1452
+ const stitchesDir = getStitchesDir(repoRoot);
1453
+ if (!existsSync(stitchesDir)) {
1454
+ return [];
1455
+ }
1456
+ const files = await readdir(stitchesDir);
1457
+ const mdFiles = files.filter((f) => f.endsWith(".md"));
1458
+ const docs = [];
1459
+ for (const file of mdFiles) {
1460
+ const filePath = join(stitchesDir, file);
1461
+ try {
1462
+ const content = await readFile(filePath, "utf-8");
1463
+ const { frontmatter, body } = parseStitchFile(content);
1464
+ if (filter?.status && frontmatter.status !== filter.status) {
1465
+ continue;
1466
+ }
1467
+ docs.push({ frontmatter, body, filePath });
1468
+ } catch {
1469
+ continue;
1470
+ }
1471
+ }
1472
+ docs.sort((a, b) => new Date(b.frontmatter.updated_at).getTime() - new Date(a.frontmatter.updated_at).getTime());
1473
+ return docs;
1474
+ }
1475
+ async function getLineage(repoRoot, id) {
1476
+ const lineage = [];
1477
+ let currentId = id;
1478
+ const visited = new Set;
1479
+ while (currentId) {
1480
+ if (visited.has(currentId)) {
1481
+ break;
1482
+ }
1483
+ visited.add(currentId);
1484
+ try {
1485
+ const doc = await loadStitch(repoRoot, currentId);
1486
+ lineage.push(currentId);
1487
+ currentId = doc.frontmatter.relations?.parent;
1488
+ } catch {
1489
+ break;
1490
+ }
1491
+ }
1492
+ return lineage;
1493
+ }
1494
+ async function requireCurrentStitchId(repoRoot) {
1495
+ const current = await getCurrentStitchId(repoRoot);
1496
+ if (!current) {
1497
+ throw new NoCurrentStitchError;
1498
+ }
1499
+ return current;
1500
+ }
1501
+
1502
+ // src/core/git.ts
1503
+ var {$ } = globalThis.Bun;
1504
+ async function getRepoRoot(cwd) {
1505
+ try {
1506
+ const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? process.cwd()).quiet().text();
1507
+ return result.trim();
1508
+ } catch {
1509
+ throw new RepoNotFoundError(cwd);
1510
+ }
1511
+ }
1512
+ async function commitExists(sha, repoRoot) {
1513
+ try {
1514
+ await $`git cat-file -e ${sha}^{commit}`.cwd(repoRoot).quiet();
1515
+ return true;
1516
+ } catch {
1517
+ return false;
1518
+ }
1519
+ }
1520
+ async function resolveRef(ref, repoRoot) {
1521
+ try {
1522
+ const result = await $`git rev-parse ${ref}`.cwd(repoRoot).quiet().text();
1523
+ return result.trim();
1524
+ } catch (error) {
1525
+ throw new GitError(`Failed to resolve ref: ${ref}`, `git rev-parse ${ref}`, 1);
1526
+ }
1527
+ }
1528
+ async function getStagedDiff(repoRoot) {
1529
+ try {
1530
+ const result = await $`git diff --staged`.cwd(repoRoot).quiet().text();
1531
+ return result;
1532
+ } catch (error) {
1533
+ throw new GitError("Failed to get staged diff", "git diff --staged", 1);
1534
+ }
1535
+ }
1536
+ async function hashDiff(diff) {
1537
+ const encoder = new TextEncoder;
1538
+ const data = encoder.encode(diff);
1539
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
1540
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
1541
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
1542
+ }
1543
+ function parseBlameOutput(output) {
1544
+ const entries = [];
1545
+ const lines = output.split(`
1546
+ `);
1547
+ let currentSha = null;
1548
+ let currentLineNumber = null;
1549
+ for (let i = 0;i < lines.length; i++) {
1550
+ const line = lines[i];
1551
+ if (line === undefined)
1552
+ continue;
1553
+ const shaMatch = line.match(/^([a-f0-9]{40})\s+\d+\s+(\d+)/);
1554
+ if (shaMatch) {
1555
+ currentSha = shaMatch[1];
1556
+ currentLineNumber = parseInt(shaMatch[2], 10);
1557
+ continue;
1558
+ }
1559
+ if (line.startsWith("\t") && currentSha && currentLineNumber) {
1560
+ entries.push({
1561
+ sha: currentSha,
1562
+ lineNumber: currentLineNumber,
1563
+ lineText: line.slice(1)
1564
+ });
1565
+ currentSha = null;
1566
+ currentLineNumber = null;
1567
+ }
1568
+ }
1569
+ return entries;
1570
+ }
1571
+ async function blameFile(filePath, repoRoot) {
1572
+ try {
1573
+ const result = await $`git blame --line-porcelain ${filePath}`.cwd(repoRoot).quiet().text();
1574
+ return parseBlameOutput(result);
1575
+ } catch (error) {
1576
+ throw new GitError(`Failed to blame file: ${filePath}`, `git blame --line-porcelain ${filePath}`, 1);
1577
+ }
1578
+ }
1579
+ async function getCommitsInRange(range, repoRoot) {
1580
+ try {
1581
+ const result = await $`git rev-list ${range}`.cwd(repoRoot).quiet().text();
1582
+ return result.trim().split(`
1583
+ `).filter((s) => s.length > 0);
1584
+ } catch {
1585
+ return [];
1586
+ }
1587
+ }
1588
+
1589
+ // src/core/link.ts
1590
+ async function addCommitLink(repoRoot, doc, sha) {
1591
+ const exists = await commitExists(sha, repoRoot);
1592
+ if (!exists) {
1593
+ throw new ValidationError(`Commit not found: ${sha}`);
1594
+ }
1595
+ const fullSha = await resolveRef(sha, repoRoot);
1596
+ const link = { kind: "commit", sha: fullSha };
1597
+ return addLink(repoRoot, doc, link);
1598
+ }
1599
+ async function addRangeLink(repoRoot, doc, range) {
1600
+ const link = { kind: "range", range };
1601
+ return addLink(repoRoot, doc, link);
1602
+ }
1603
+ async function addStagedDiffFingerprint(repoRoot, doc) {
1604
+ const diff = await getStagedDiff(repoRoot);
1605
+ if (!diff.trim()) {
1606
+ throw new ValidationError("No staged changes to fingerprint");
1607
+ }
1608
+ const hash = await hashDiff(diff);
1609
+ const fingerprint = {
1610
+ algo: "sha256",
1611
+ kind: "staged-diff",
1612
+ value: hash
1613
+ };
1614
+ const updatedDoc = addFingerprint(doc, fingerprint);
1615
+ const savedDoc = await saveStitch(repoRoot, updatedDoc);
1616
+ return { doc: savedDoc, fingerprint };
1617
+ }
1618
+ function addLink(repoRoot, doc, link) {
1619
+ const git = doc.frontmatter.git ?? {};
1620
+ const links = git.links ?? [];
1621
+ const isDuplicate = links.some((existing) => {
1622
+ if (existing.kind !== link.kind)
1623
+ return false;
1624
+ if (existing.kind === "commit" && link.kind === "commit") {
1625
+ return existing.sha === link.sha;
1626
+ }
1627
+ if (existing.kind === "range" && link.kind === "range") {
1628
+ return existing.range === link.range;
1629
+ }
1630
+ return false;
1631
+ });
1632
+ if (isDuplicate) {
1633
+ return Promise.resolve(doc);
1634
+ }
1635
+ const updatedDoc = {
1636
+ ...doc,
1637
+ frontmatter: {
1638
+ ...doc.frontmatter,
1639
+ git: {
1640
+ ...git,
1641
+ links: [...links, link]
1642
+ }
1643
+ }
1644
+ };
1645
+ return saveStitch(repoRoot, updatedDoc);
1646
+ }
1647
+ function addFingerprint(doc, fingerprint) {
1648
+ const git = doc.frontmatter.git ?? {};
1649
+ const fingerprints = git.fingerprints ?? [];
1650
+ const isDuplicate = fingerprints.some((existing) => existing.algo === fingerprint.algo && existing.kind === fingerprint.kind && existing.value === fingerprint.value);
1651
+ if (isDuplicate) {
1652
+ return doc;
1653
+ }
1654
+ return {
1655
+ ...doc,
1656
+ frontmatter: {
1657
+ ...doc.frontmatter,
1658
+ git: {
1659
+ ...git,
1660
+ fingerprints: [...fingerprints, fingerprint]
1661
+ }
1662
+ }
1663
+ };
1664
+ }
1665
+
1666
+ // src/core/blame.ts
1667
+ async function buildCommitToStitchMap(repoRoot, stitches) {
1668
+ const map = new Map;
1669
+ for (const doc of stitches) {
1670
+ const links = doc.frontmatter.git?.links ?? [];
1671
+ for (const link of links) {
1672
+ if (link.kind === "commit") {
1673
+ const existing = map.get(link.sha) ?? [];
1674
+ if (!existing.includes(doc.frontmatter.id)) {
1675
+ map.set(link.sha, [...existing, doc.frontmatter.id]);
1676
+ }
1677
+ } else if (link.kind === "range") {
1678
+ const commits = await getCommitsInRange(link.range, repoRoot);
1679
+ for (const sha of commits) {
1680
+ const existing = map.get(sha) ?? [];
1681
+ if (!existing.includes(doc.frontmatter.id)) {
1682
+ map.set(sha, [...existing, doc.frontmatter.id]);
1683
+ }
1684
+ }
1685
+ }
1686
+ }
1687
+ }
1688
+ return map;
1689
+ }
1690
+ async function stitchBlame(repoRoot, filePath) {
1691
+ const blameEntries = await blameFile(filePath, repoRoot);
1692
+ const stitches = await listStitches(repoRoot);
1693
+ const commitMap = await buildCommitToStitchMap(repoRoot, stitches);
1694
+ const result = blameEntries.map((entry) => ({
1695
+ line: entry.lineNumber,
1696
+ sha: entry.sha,
1697
+ stitchIds: commitMap.get(entry.sha) ?? [],
1698
+ text: entry.lineText
1699
+ }));
1700
+ return result;
1701
+ }
1702
+
1703
+ // src/platform/paths.ts
1704
+ function getEditor() {
1705
+ return process.env["VISUAL"] ?? process.env["EDITOR"] ?? (process.platform === "win32" ? "notepad" : "vi");
1706
+ }
1707
+
1708
+ // src/api.ts
1709
+ class StitchClient {
1710
+ repoRoot = null;
1711
+ repoRootOverride;
1712
+ constructor(options) {
1713
+ this.repoRootOverride = options?.repoRoot;
1714
+ }
1715
+ async getRepoRoot() {
1716
+ if (this.repoRoot) {
1717
+ return this.repoRoot;
1718
+ }
1719
+ if (this.repoRootOverride) {
1720
+ this.repoRoot = this.repoRootOverride;
1721
+ } else {
1722
+ this.repoRoot = await getRepoRoot();
1723
+ }
1724
+ return this.repoRoot;
1725
+ }
1726
+ async init() {
1727
+ const root = await this.getRepoRoot();
1728
+ await initializeStitch(root);
1729
+ }
1730
+ async isInitialized() {
1731
+ const root = await this.getRepoRoot();
1732
+ return isInitialized(root);
1733
+ }
1734
+ async start(title) {
1735
+ const root = await this.getRepoRoot();
1736
+ const doc = await createStitch(root, title);
1737
+ await setCurrentStitchId(root, doc.frontmatter.id);
1738
+ return doc;
1739
+ }
1740
+ async child(title) {
1741
+ const root = await this.getRepoRoot();
1742
+ const parentId = await requireCurrentStitchId(root);
1743
+ const doc = await createStitch(root, title, parentId);
1744
+ await setCurrentStitchId(root, doc.frontmatter.id);
1745
+ return doc;
1746
+ }
1747
+ async switch(id) {
1748
+ const root = await this.getRepoRoot();
1749
+ await loadStitch(root, id);
1750
+ await setCurrentStitchId(root, id);
1751
+ }
1752
+ async status() {
1753
+ const root = await this.getRepoRoot();
1754
+ if (!isInitialized(root)) {
1755
+ throw new NotInitializedError;
1756
+ }
1757
+ const current = await getCurrentStitchId(root);
1758
+ if (!current) {
1759
+ return { lineage: [] };
1760
+ }
1761
+ const lineage = await getLineage(root, current);
1762
+ return { current, lineage };
1763
+ }
1764
+ async list(filter) {
1765
+ const root = await this.getRepoRoot();
1766
+ return listStitches(root, filter);
1767
+ }
1768
+ async get(id) {
1769
+ const root = await this.getRepoRoot();
1770
+ return loadStitch(root, id);
1771
+ }
1772
+ async requireCurrentId() {
1773
+ const root = await this.getRepoRoot();
1774
+ return requireCurrentStitchId(root);
1775
+ }
1776
+ async openInEditor(id) {
1777
+ const root = await this.getRepoRoot();
1778
+ const stitchId = id ?? await requireCurrentStitchId(root);
1779
+ const filePath = getStitchFilePath(root, stitchId);
1780
+ const editor = getEditor();
1781
+ return new Promise((resolve, reject) => {
1782
+ const child = spawn(editor, [filePath], {
1783
+ stdio: "inherit",
1784
+ shell: true
1785
+ });
1786
+ child.on("close", (code) => {
1787
+ if (code === 0) {
1788
+ resolve();
1789
+ } else {
1790
+ reject(new Error(`Editor exited with code ${code}`));
1791
+ }
1792
+ });
1793
+ child.on("error", (err) => {
1794
+ reject(err);
1795
+ });
1796
+ });
1797
+ }
1798
+ async linkCommit(sha, id) {
1799
+ const root = await this.getRepoRoot();
1800
+ const stitchId = id ?? await requireCurrentStitchId(root);
1801
+ const doc = await loadStitch(root, stitchId);
1802
+ await addCommitLink(root, doc, sha);
1803
+ }
1804
+ async linkRange(range, id) {
1805
+ const root = await this.getRepoRoot();
1806
+ const stitchId = id ?? await requireCurrentStitchId(root);
1807
+ const doc = await loadStitch(root, stitchId);
1808
+ await addRangeLink(root, doc, range);
1809
+ }
1810
+ async linkStagedDiff(id) {
1811
+ const root = await this.getRepoRoot();
1812
+ const stitchId = id ?? await requireCurrentStitchId(root);
1813
+ const doc = await loadStitch(root, stitchId);
1814
+ const { fingerprint } = await addStagedDiffFingerprint(root, doc);
1815
+ return fingerprint;
1816
+ }
1817
+ async blame(path) {
1818
+ const root = await this.getRepoRoot();
1819
+ return stitchBlame(root, path);
1820
+ }
1821
+ [Symbol.dispose]() {
1822
+ this.close();
1823
+ }
1824
+ close() {
1825
+ this.repoRoot = null;
1826
+ }
1827
+ }
1828
+ export {
1829
+ ValidationError,
1830
+ StitchNotFoundError,
1831
+ StitchError,
1832
+ StitchClient,
1833
+ RepoNotFoundError,
1834
+ NotInitializedError,
1835
+ NoCurrentStitchError,
1836
+ GitError
1837
+ };