@yaks/mirror 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +50 -2
- package/_dist/mod.d.ts +55 -0
- package/_dist/mod.d.ts.map +1 -0
- package/deno.json +1 -0
- package/mod.js +198 -0
- package/mod.js.map +1 -0
- package/mod.ts +240 -0
- package/package.json +17 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/mirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sync files and a graph. A mirror is a list of bindings; each binding names the
|
|
4
|
+
files it covers and carries either direction or both:
|
|
5
|
+
|
|
6
|
+
- `read(paths, gone)`: files → graph, for the paths whose file moved.
|
|
7
|
+
- `values()`: graph → files, the text the graph says each path should hold.
|
|
8
|
+
|
|
9
|
+
Code is read-only (the files own it, see [@yaks/code](../code)), a generated
|
|
10
|
+
persona file is write-only (the graph owns it), and a markdown document may go
|
|
11
|
+
both ways.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { assertEquals } from '@std/assert'
|
|
15
|
+
import { type Binding, blobOf, memo, sync } from '@yaks/mirror'
|
|
16
|
+
|
|
17
|
+
let dir = await Deno.makeTempDir()
|
|
18
|
+
let agents = `${dir}/AGENTS.md`
|
|
19
|
+
let personas: Binding = {
|
|
20
|
+
name: 'personas',
|
|
21
|
+
files: async () => new Map([[agents, await blobOf('old\n')]]),
|
|
22
|
+
values: () => Promise.resolve(new Map([[agents, 'new\n']])),
|
|
23
|
+
...memo(`${dir}/mirror.json`),
|
|
24
|
+
}
|
|
25
|
+
let report = await sync(personas)
|
|
26
|
+
assertEquals(report.wrote, [agents])
|
|
27
|
+
assertEquals(await Deno.readTextFile(agents), 'new\n')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Agreement and conflicts
|
|
31
|
+
|
|
32
|
+
Each path remembers the pair it last agreed on: the file's Git blob id and the
|
|
33
|
+
hash of the graph's value (for a written file, the blob id of the text it
|
|
34
|
+
rendered). `decide()` compares both sides with that memory:
|
|
35
|
+
|
|
36
|
+
| file moved | graph moved | read-only | write-only | both ways |
|
|
37
|
+
| ---------- | ----------- | --------- | ---------- | --------- |
|
|
38
|
+
| no | no | same | same | same |
|
|
39
|
+
| yes | no | read | write back | read |
|
|
40
|
+
| no | yes | — | write | write |
|
|
41
|
+
| yes | yes | read | conflict | conflict |
|
|
42
|
+
|
|
43
|
+
A conflict is reported and neither side is touched. Where the graph owns the
|
|
44
|
+
file, deleting the file resolves it: a missing file is always written. A
|
|
45
|
+
read-only binding's memory is whatever the graph recorded when it read the file
|
|
46
|
+
(@yaks/code keeps the blob on each module); `memo(path)` keeps it in a JSON file
|
|
47
|
+
for a binding whose graph side cannot hold it.
|
|
48
|
+
|
|
49
|
+
`plan(binding)` returns every decision without acting, which is what a `--check`
|
|
50
|
+
reports. Writes go through a temporary file and a rename, so a reader never sees
|
|
51
|
+
a half-written file, and a file whose contents already match is not rewritten.
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** What a path last agreed on: the file's Git blob id, and the hash of the
|
|
2
|
+
* graph's value — for a written file, the blob id of the text it rendered. */ export type Agreed = {
|
|
3
|
+
blob: string;
|
|
4
|
+
hash?: string;
|
|
5
|
+
};
|
|
6
|
+
/** What a sync does with one path. */ export type Act = 'same' | 'read' | 'write' | 'conflict';
|
|
7
|
+
/** One path's two sides now, and what they last agreed on. `file` is absent
|
|
8
|
+
* when there is no file, `value` when the graph holds nothing for it. */ export type Sides = {
|
|
9
|
+
file?: string;
|
|
10
|
+
value?: string;
|
|
11
|
+
agreed?: Agreed;
|
|
12
|
+
};
|
|
13
|
+
/** A set of files and the graph's side of them. */ export type Binding = {
|
|
14
|
+
/** what reports and memory call it */ name: string;
|
|
15
|
+
/** the covered files that exist now: path → Git blob id */ files: () => Promise<Map<string, string>>;
|
|
16
|
+
/** graph → files: the text the graph says each path should hold; a path
|
|
17
|
+
* absent here is a file the graph no longer has */ values?: () => Promise<Map<string, string>>;
|
|
18
|
+
/** files → graph: read the paths that moved; `gone` are paths whose file is
|
|
19
|
+
* gone. The graph's own record of what it read is its side of the memory. */ read?: (paths: string[], gone: string[]) => Promise<void>;
|
|
20
|
+
/** what each path last agreed on */ agreed: () => Promise<Map<string, Agreed>>;
|
|
21
|
+
/** record new agreements; `null` forgets a path */ remember?: (changed: Map<string, Agreed | null>) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
/** What one sync did. */ export type Report = {
|
|
24
|
+
read: string[];
|
|
25
|
+
wrote: string[];
|
|
26
|
+
removed: string[];
|
|
27
|
+
conflicts: string[];
|
|
28
|
+
failed: string[];
|
|
29
|
+
};
|
|
30
|
+
/** A path's decision, from its two sides and what they last agreed on.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* decide({ file: 'a', agreed: { blob: 'a' } }, { read: true }) // 'same'
|
|
34
|
+
* decide({ file: 'b', agreed: { blob: 'a' } }, { read: true }) // 'read'
|
|
35
|
+
* decide({ file: 'b', value: 'c', agreed: { blob: 'a', hash: 'a' } },
|
|
36
|
+
* { write: true }) // 'conflict'
|
|
37
|
+
* ```
|
|
38
|
+
*/ export declare let decide: (s: Sides, can: {
|
|
39
|
+
read?: boolean;
|
|
40
|
+
write?: boolean;
|
|
41
|
+
}) => Act;
|
|
42
|
+
/** The Git blob id of a text: what `git hash-object` prints for it. */ export declare let blobOf: (text: string) => Promise<string>;
|
|
43
|
+
/** Every path's decision, without doing anything: what a `--check` reports. */ export declare let plan: (b: Binding) => Promise<{
|
|
44
|
+
path: string;
|
|
45
|
+
act: Act;
|
|
46
|
+
sides: Sides;
|
|
47
|
+
text?: string;
|
|
48
|
+
}[]>;
|
|
49
|
+
/** Sync one binding: read what moved on the file side, write what moved on
|
|
50
|
+
* the graph side, and report what moved on both. A failure on one path never
|
|
51
|
+
* stops the others. */ export declare let sync: (b: Binding) => Promise<Report>;
|
|
52
|
+
/** The covered files among `paths` that exist, with their blob ids. */ export declare let present: (paths: string[]) => Promise<Map<string, string>>;
|
|
53
|
+
/** A memory kept as one JSON file — for a binding whose graph side cannot
|
|
54
|
+
* hold it. A missing or unreadable file remembers nothing. */ export declare let memo: (file: string) => Pick<Binding, 'agreed' | 'remember'>;
|
|
55
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AA4BA;4EAC4E,GAC5E,YAAY;EAAW,MAAM,MAAM;EAAE,OAAO,MAAM;;AAElD,oCAAoC,GACpC,YAAY,MAAM,SAAS,SAAS,UAAU;AAE9C;uEACuE,GACvE,YAAY;EAAU,OAAO,MAAM;EAAE,QAAQ,MAAM;EAAE,SAAS;;AAE9D,iDAAiD,GACjD,YAAY;EACV,oCAAoC,GACpC,MAAM,MAAM;EACZ,yDAAyD,GACzD,aAAa,QAAQ,IAAI,MAAM,EAAE,MAAM;EACvC;mDACiD,GACjD,eAAe,QAAQ,IAAI,MAAM,EAAE,MAAM;EACzC;6EAC2E,GAC3E,QAAQ,OAAO,MAAM,IAAI,MAAM,MAAM,OAAO,QAAQ,IAAI;EACxD,kCAAkC,GAClC,cAAc,QAAQ,IAAI,MAAM,EAAE;EAClC,iDAAiD,GACjD,YAAY,SAAS,IAAI,MAAM,EAAE,SAAS,IAAI,MAAM,QAAQ,IAAI;;AAGlE,uBAAuB,GACvB,YAAY;EACV,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,SAAS,MAAM;EACf,WAAW,MAAM;EACjB,QAAQ,MAAM;;AAGhB;;;;;;;;CAQC,GACD,OAAO,YAAI,SACT,GAAG,OACH;EAAO,OAAO,OAAO;EAAE,QAAQ,OAAO;MACrC,IAqBF;AAID,qEAAqE,GACrE,OAAO,YAAI,SAAU,MAAM,MAAM,KAAG,QAAQ,MAAM,EAClB;AAEhC,6EAA6E,GAC7E,OAAO,YAAI,OACT,GAAG,YACF;EAAU,MAAM,MAAM;EAAE,KAAK;EAAK,OAAO;EAAO,OAAO,MAAM;KAmB/D;AAqBD;;qBAEqB,GACrB,OAAO,YAAI,OAAc,GAAG,YAAU,QAAQ,QA8C7C;AAED,qEAAqE,GACrE,OAAO,YAAI,UAAiB,OAAO,MAAM,OAAK,QAAQ,IAAI,MAAM,EAAE,MAAM,GAYvE;AAED;4DAC4D,GAC5D,OAAO,YAAI,OACT,MAAM,MAAM,KACX,KAAK,SAAS,WAAW,YAmB3B"}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/mirror","version":"0.2.36","license":"Apache-2.0","description":"Syncs files and a graph through bindings, each direction optional, reporting a path changed on both sides as a conflict instead of overwriting it.","exports":"./mod.ts","imports":{"@yaks/mirror":"./mod.ts"}}
|
package/mod.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import * as io from "@yaks/file/io";
|
|
2
|
+
/**
|
|
3
|
+
* @yaks/mirror — one sync between files and a graph, each direction optional.
|
|
4
|
+
*
|
|
5
|
+
* A mirror is a list of bindings. A binding names the files it covers and
|
|
6
|
+
* carries a `read` (files → graph), a `values` (graph → the text each file
|
|
7
|
+
* should hold), or both. Code is read-only: the files own it. A persona is
|
|
8
|
+
* write-only: the graph owns it. A markdown document may go both ways.
|
|
9
|
+
*
|
|
10
|
+
* Each synced path remembers the pair it last agreed on: the file's Git blob
|
|
11
|
+
* id and the hash of the graph's value. Against that memory a path is one of
|
|
12
|
+
* four things — the same, moved on the file side (read it), moved on the graph
|
|
13
|
+
* side (write it), or moved on both since they last agreed: a conflict,
|
|
14
|
+
* reported and never overwritten, the same promise a `was` precondition makes
|
|
15
|
+
* a write.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { sync } from '@yaks/mirror'
|
|
19
|
+
*
|
|
20
|
+
* // let report = await sync(binding)
|
|
21
|
+
* // report.conflicts -> paths changed on both sides, left as they are
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/ import { oid } from "@yaks/git";
|
|
26
|
+
/** A path's decision, from its two sides and what they last agreed on.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* decide({ file: 'a', agreed: { blob: 'a' } }, { read: true }) // 'same'
|
|
30
|
+
* decide({ file: 'b', agreed: { blob: 'a' } }, { read: true }) // 'read'
|
|
31
|
+
* decide({ file: 'b', value: 'c', agreed: { blob: 'a', hash: 'a' } },
|
|
32
|
+
* { write: true }) // 'conflict'
|
|
33
|
+
* ```
|
|
34
|
+
*/ export let decide = (s, can)=>{
|
|
35
|
+
if (s.file != null && s.file == s.value) return 'same';
|
|
36
|
+
let fileMoved = s.file != s.agreed?.blob;
|
|
37
|
+
if (!can.write) return fileMoved ? 'read' : 'same';
|
|
38
|
+
if (s.file == null && s.value == null) return 'same';
|
|
39
|
+
// Where the graph owns the file, a missing file loses nothing by being
|
|
40
|
+
// written: deleting it is how somebody resolves a conflict.
|
|
41
|
+
if (!can.read && s.file == null) return 'write';
|
|
42
|
+
if (!s.agreed) {
|
|
43
|
+
// Nothing remembered: the owning side wins, and with two owners only an
|
|
44
|
+
// absent side can yield without losing anything.
|
|
45
|
+
if (!can.read || s.file == null) return 'write';
|
|
46
|
+
return s.value == null ? 'read' : 'conflict';
|
|
47
|
+
}
|
|
48
|
+
let valueMoved = s.value != s.agreed.hash;
|
|
49
|
+
if (fileMoved && valueMoved) return 'conflict';
|
|
50
|
+
if (valueMoved) return 'write';
|
|
51
|
+
if (!fileMoved) return 'same';
|
|
52
|
+
// Only the file moved: read it back, or, where the graph owns the file,
|
|
53
|
+
// put back what the graph says.
|
|
54
|
+
return can.read ? 'read' : 'write';
|
|
55
|
+
};
|
|
56
|
+
let utf8 = new TextEncoder();
|
|
57
|
+
/** The Git blob id of a text: what `git hash-object` prints for it. */ export let blobOf = (text)=>oid('blob', utf8.encode(text));
|
|
58
|
+
/** Every path's decision, without doing anything: what a `--check` reports. */ export let plan = async (b)=>{
|
|
59
|
+
let [files, texts, agreed] = await Promise.all([
|
|
60
|
+
b.files(),
|
|
61
|
+
b.values?.() ?? new Map(),
|
|
62
|
+
b.agreed()
|
|
63
|
+
]);
|
|
64
|
+
let values = new Map();
|
|
65
|
+
for (let [path, text] of texts)values.set(path, await blobOf(text));
|
|
66
|
+
let paths = [
|
|
67
|
+
...new Set([
|
|
68
|
+
...files.keys(),
|
|
69
|
+
...texts.keys(),
|
|
70
|
+
...agreed.keys()
|
|
71
|
+
])
|
|
72
|
+
].sort();
|
|
73
|
+
let can = {
|
|
74
|
+
read: !!b.read,
|
|
75
|
+
write: !!b.values
|
|
76
|
+
};
|
|
77
|
+
return paths.map((path)=>{
|
|
78
|
+
let sides = {
|
|
79
|
+
file: files.get(path),
|
|
80
|
+
value: values.get(path),
|
|
81
|
+
agreed: agreed.get(path)
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
path,
|
|
85
|
+
act: decide(sides, can),
|
|
86
|
+
sides,
|
|
87
|
+
text: texts.get(path)
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
// A write goes to a temp file beside the target and is renamed over it: a
|
|
92
|
+
// reader mid-write — a harness loading its instructions — sees the old file or
|
|
93
|
+
// the new one, never an empty one.
|
|
94
|
+
let put = async (path, text)=>{
|
|
95
|
+
await io.mkdir(path.slice(0, path.lastIndexOf('/')) || '.', {
|
|
96
|
+
recursive: true
|
|
97
|
+
});
|
|
98
|
+
let tmp = `${path}.${crypto.randomUUID()}.tmp`;
|
|
99
|
+
try {
|
|
100
|
+
await io.writeTextFile(tmp, text);
|
|
101
|
+
await io.rename(tmp, path);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
try {
|
|
104
|
+
await io.remove(tmp);
|
|
105
|
+
} catch {}
|
|
106
|
+
throw e;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
/** Sync one binding: read what moved on the file side, write what moved on
|
|
110
|
+
* the graph side, and report what moved on both. A failure on one path never
|
|
111
|
+
* stops the others. */ export let sync = async (b)=>{
|
|
112
|
+
let out = {
|
|
113
|
+
read: [],
|
|
114
|
+
wrote: [],
|
|
115
|
+
removed: [],
|
|
116
|
+
conflicts: [],
|
|
117
|
+
failed: []
|
|
118
|
+
};
|
|
119
|
+
let learned = new Map();
|
|
120
|
+
let gone = [];
|
|
121
|
+
for (let { path, act, sides, text } of (await plan(b))){
|
|
122
|
+
if (act == 'conflict') out.conflicts.push(path);
|
|
123
|
+
else if (act == 'read') {
|
|
124
|
+
out.read.push(path);
|
|
125
|
+
if (sides.file == null) gone.push(path);
|
|
126
|
+
} else if (act == 'write') {
|
|
127
|
+
try {
|
|
128
|
+
if (text == null) {
|
|
129
|
+
await io.remove(path);
|
|
130
|
+
out.removed.push(path);
|
|
131
|
+
learned.set(path, null);
|
|
132
|
+
} else {
|
|
133
|
+
await put(path, text);
|
|
134
|
+
out.wrote.push(path);
|
|
135
|
+
learned.set(path, {
|
|
136
|
+
blob: sides.value,
|
|
137
|
+
hash: sides.value
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
} catch (e) {
|
|
141
|
+
out.failed.push(`${path}: ${e.message}`);
|
|
142
|
+
}
|
|
143
|
+
} else if (!b.values) continue;
|
|
144
|
+
else if (sides.file == null && sides.value == null) learned.set(path, null);
|
|
145
|
+
else if (sides.agreed?.blob != sides.file) {
|
|
146
|
+
// Both sides already say the same thing: remember that they agree.
|
|
147
|
+
learned.set(path, {
|
|
148
|
+
blob: sides.file,
|
|
149
|
+
hash: sides.value
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (out.read.length) {
|
|
154
|
+
try {
|
|
155
|
+
await b.read(out.read, gone);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
out.failed.push(`${b.name}: ${e.message}`);
|
|
158
|
+
out.read = [];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (learned.size) await b.remember?.(learned);
|
|
162
|
+
return out;
|
|
163
|
+
};
|
|
164
|
+
/** The covered files among `paths` that exist, with their blob ids. */ export let present = async (paths)=>{
|
|
165
|
+
let out = new Map();
|
|
166
|
+
for (let path of paths){
|
|
167
|
+
let text;
|
|
168
|
+
try {
|
|
169
|
+
text = await io.readTextFile(path);
|
|
170
|
+
} catch {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
out.set(path, await blobOf(text));
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
};
|
|
177
|
+
/** A memory kept as one JSON file — for a binding whose graph side cannot
|
|
178
|
+
* hold it. A missing or unreadable file remembers nothing. */ export let memo = (file)=>{
|
|
179
|
+
let load = async ()=>{
|
|
180
|
+
try {
|
|
181
|
+
return JSON.parse(await io.readTextFile(file));
|
|
182
|
+
} catch {
|
|
183
|
+
return {};
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
agreed: async ()=>new Map(Object.entries(await load())),
|
|
188
|
+
remember: async (changed)=>{
|
|
189
|
+
let all = await load();
|
|
190
|
+
for (let [path, a] of changed){
|
|
191
|
+
if (a) all[path] = a;
|
|
192
|
+
else delete all[path];
|
|
193
|
+
}
|
|
194
|
+
await put(file, JSON.stringify(all, null, 1) + '\n');
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198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AC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,AAAC,EAAY,OAAO,EAAE;MACpD,IAAI,IAAI,GAAG,EAAE;IACf;EACF;EACA,IAAI,QAAQ,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAG;EACrC,OAAO;AACT,EAAC;AAED,qEAAqE,GACrE,OAAO,IAAI,UAAU,OAAO;EAC1B,IAAI,MAAM,IAAI;EACd,KAAK,IAAI,QAAQ,MAAO;IACtB,IAAI;IACJ,IAAI;MACF,OAAO,MAAM,GAAG,YAAY,CAAC;IAC/B,EAAE,OAAM;MACN;IACF;IACA,IAAI,GAAG,CAAC,MAAM,MAAM,OAAO;EAC7B;EACA,OAAO;AACT,EAAC;AAED;4DAC4D,GAC5D,OAAO,IAAI,OAAO,CAChB;EAEA,IAAI,OAAO;IACT,IAAI;MACF,OAAO,KAAK,KAAK,CAAC,MAAM,GAAG,YAAY,CAAC;IAC1C,EAAE,OAAM;MACN,OAAO,CAAC;IACV;EACF;EACA,OAAO;IACL,QAAQ,UAAY,IAAI,IAAI,OAAO,OAAO,CAAC,MAAM;IACjD,UAAU,OAAO;MACf,IAAI,MAAM,MAAM;MAChB,KAAK,IAAI,CAAC,MAAM,EAAE,IAAI,QAAS;QAC7B,IAAI,GAAG,GAAG,CAAC,KAAK,GAAG;aACd,OAAO,GAAG,CAAC,KAAK;MACvB;MACA,MAAM,IAAI,MAAM,KAAK,SAAS,CAAC,KAAK,MAAM,KAAK;IACjD;EACF;AACF,EAAC"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
/**
|
|
3
|
+
* @yaks/mirror — one sync between files and a graph, each direction optional.
|
|
4
|
+
*
|
|
5
|
+
* A mirror is a list of bindings. A binding names the files it covers and
|
|
6
|
+
* carries a `read` (files → graph), a `values` (graph → the text each file
|
|
7
|
+
* should hold), or both. Code is read-only: the files own it. A persona is
|
|
8
|
+
* write-only: the graph owns it. A markdown document may go both ways.
|
|
9
|
+
*
|
|
10
|
+
* Each synced path remembers the pair it last agreed on: the file's Git blob
|
|
11
|
+
* id and the hash of the graph's value. Against that memory a path is one of
|
|
12
|
+
* four things — the same, moved on the file side (read it), moved on the graph
|
|
13
|
+
* side (write it), or moved on both since they last agreed: a conflict,
|
|
14
|
+
* reported and never overwritten, the same promise a `was` precondition makes
|
|
15
|
+
* a write.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { sync } from '@yaks/mirror'
|
|
19
|
+
*
|
|
20
|
+
* // let report = await sync(binding)
|
|
21
|
+
* // report.conflicts -> paths changed on both sides, left as they are
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { oid } from '@yaks/git'
|
|
28
|
+
|
|
29
|
+
/** What a path last agreed on: the file's Git blob id, and the hash of the
|
|
30
|
+
* graph's value — for a written file, the blob id of the text it rendered. */
|
|
31
|
+
export type Agreed = { blob: string; hash?: string }
|
|
32
|
+
|
|
33
|
+
/** What a sync does with one path. */
|
|
34
|
+
export type Act = 'same' | 'read' | 'write' | 'conflict'
|
|
35
|
+
|
|
36
|
+
/** One path's two sides now, and what they last agreed on. `file` is absent
|
|
37
|
+
* when there is no file, `value` when the graph holds nothing for it. */
|
|
38
|
+
export type Sides = { file?: string; value?: string; agreed?: Agreed }
|
|
39
|
+
|
|
40
|
+
/** A set of files and the graph's side of them. */
|
|
41
|
+
export type Binding = {
|
|
42
|
+
/** what reports and memory call it */
|
|
43
|
+
name: string
|
|
44
|
+
/** the covered files that exist now: path → Git blob id */
|
|
45
|
+
files: () => Promise<Map<string, string>>
|
|
46
|
+
/** graph → files: the text the graph says each path should hold; a path
|
|
47
|
+
* absent here is a file the graph no longer has */
|
|
48
|
+
values?: () => Promise<Map<string, string>>
|
|
49
|
+
/** files → graph: read the paths that moved; `gone` are paths whose file is
|
|
50
|
+
* gone. The graph's own record of what it read is its side of the memory. */
|
|
51
|
+
read?: (paths: string[], gone: string[]) => Promise<void>
|
|
52
|
+
/** what each path last agreed on */
|
|
53
|
+
agreed: () => Promise<Map<string, Agreed>>
|
|
54
|
+
/** record new agreements; `null` forgets a path */
|
|
55
|
+
remember?: (changed: Map<string, Agreed | null>) => Promise<void>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What one sync did. */
|
|
59
|
+
export type Report = {
|
|
60
|
+
read: string[]
|
|
61
|
+
wrote: string[]
|
|
62
|
+
removed: string[]
|
|
63
|
+
conflicts: string[]
|
|
64
|
+
failed: string[]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A path's decision, from its two sides and what they last agreed on.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* decide({ file: 'a', agreed: { blob: 'a' } }, { read: true }) // 'same'
|
|
71
|
+
* decide({ file: 'b', agreed: { blob: 'a' } }, { read: true }) // 'read'
|
|
72
|
+
* decide({ file: 'b', value: 'c', agreed: { blob: 'a', hash: 'a' } },
|
|
73
|
+
* { write: true }) // 'conflict'
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export let decide = (
|
|
77
|
+
s: Sides,
|
|
78
|
+
can: { read?: boolean; write?: boolean },
|
|
79
|
+
): Act => {
|
|
80
|
+
if (s.file != null && s.file == s.value) return 'same'
|
|
81
|
+
let fileMoved = s.file != s.agreed?.blob
|
|
82
|
+
if (!can.write) return fileMoved ? 'read' : 'same'
|
|
83
|
+
if (s.file == null && s.value == null) return 'same'
|
|
84
|
+
// Where the graph owns the file, a missing file loses nothing by being
|
|
85
|
+
// written: deleting it is how somebody resolves a conflict.
|
|
86
|
+
if (!can.read && s.file == null) return 'write'
|
|
87
|
+
if (!s.agreed) {
|
|
88
|
+
// Nothing remembered: the owning side wins, and with two owners only an
|
|
89
|
+
// absent side can yield without losing anything.
|
|
90
|
+
if (!can.read || s.file == null) return 'write'
|
|
91
|
+
return s.value == null ? 'read' : 'conflict'
|
|
92
|
+
}
|
|
93
|
+
let valueMoved = s.value != s.agreed.hash
|
|
94
|
+
if (fileMoved && valueMoved) return 'conflict'
|
|
95
|
+
if (valueMoved) return 'write'
|
|
96
|
+
if (!fileMoved) return 'same'
|
|
97
|
+
// Only the file moved: read it back, or, where the graph owns the file,
|
|
98
|
+
// put back what the graph says.
|
|
99
|
+
return can.read ? 'read' : 'write'
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let utf8 = new TextEncoder()
|
|
103
|
+
|
|
104
|
+
/** The Git blob id of a text: what `git hash-object` prints for it. */
|
|
105
|
+
export let blobOf = (text: string): Promise<string> =>
|
|
106
|
+
oid('blob', utf8.encode(text))
|
|
107
|
+
|
|
108
|
+
/** Every path's decision, without doing anything: what a `--check` reports. */
|
|
109
|
+
export let plan = async (
|
|
110
|
+
b: Binding,
|
|
111
|
+
): Promise<{ path: string; act: Act; sides: Sides; text?: string }[]> => {
|
|
112
|
+
let [files, texts, agreed] = await Promise.all([
|
|
113
|
+
b.files(),
|
|
114
|
+
b.values?.() ?? new Map<string, string>(),
|
|
115
|
+
b.agreed(),
|
|
116
|
+
])
|
|
117
|
+
let values = new Map<string, string>()
|
|
118
|
+
for (let [path, text] of texts) values.set(path, await blobOf(text))
|
|
119
|
+
let paths = [...new Set([...files.keys(), ...texts.keys(), ...agreed.keys()])]
|
|
120
|
+
.sort()
|
|
121
|
+
let can = { read: !!b.read, write: !!b.values }
|
|
122
|
+
return paths.map((path) => {
|
|
123
|
+
let sides = {
|
|
124
|
+
file: files.get(path),
|
|
125
|
+
value: values.get(path),
|
|
126
|
+
agreed: agreed.get(path),
|
|
127
|
+
}
|
|
128
|
+
return { path, act: decide(sides, can), sides, text: texts.get(path) }
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// A write goes to a temp file beside the target and is renamed over it: a
|
|
133
|
+
// reader mid-write — a harness loading its instructions — sees the old file or
|
|
134
|
+
// the new one, never an empty one.
|
|
135
|
+
let put = async (path: string, text: string) => {
|
|
136
|
+
await io.mkdir(path.slice(0, path.lastIndexOf('/')) || '.', {
|
|
137
|
+
recursive: true,
|
|
138
|
+
})
|
|
139
|
+
let tmp = `${path}.${crypto.randomUUID()}.tmp`
|
|
140
|
+
try {
|
|
141
|
+
await io.writeTextFile(tmp, text)
|
|
142
|
+
await io.rename(tmp, path)
|
|
143
|
+
} catch (e) {
|
|
144
|
+
try {
|
|
145
|
+
await io.remove(tmp)
|
|
146
|
+
} catch { /* nothing to clean */ }
|
|
147
|
+
throw e
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Sync one binding: read what moved on the file side, write what moved on
|
|
152
|
+
* the graph side, and report what moved on both. A failure on one path never
|
|
153
|
+
* stops the others. */
|
|
154
|
+
export let sync = async (b: Binding): Promise<Report> => {
|
|
155
|
+
let out: Report = {
|
|
156
|
+
read: [],
|
|
157
|
+
wrote: [],
|
|
158
|
+
removed: [],
|
|
159
|
+
conflicts: [],
|
|
160
|
+
failed: [],
|
|
161
|
+
}
|
|
162
|
+
let learned = new Map<string, Agreed | null>()
|
|
163
|
+
let gone: string[] = []
|
|
164
|
+
for (let { path, act, sides, text } of await plan(b)) {
|
|
165
|
+
if (act == 'conflict') out.conflicts.push(path)
|
|
166
|
+
else if (act == 'read') {
|
|
167
|
+
out.read.push(path)
|
|
168
|
+
if (sides.file == null) gone.push(path)
|
|
169
|
+
} else if (act == 'write') {
|
|
170
|
+
try {
|
|
171
|
+
if (text == null) {
|
|
172
|
+
await io.remove(path)
|
|
173
|
+
out.removed.push(path)
|
|
174
|
+
learned.set(path, null)
|
|
175
|
+
} else {
|
|
176
|
+
await put(path, text)
|
|
177
|
+
out.wrote.push(path)
|
|
178
|
+
learned.set(path, { blob: sides.value!, hash: sides.value })
|
|
179
|
+
}
|
|
180
|
+
} catch (e) {
|
|
181
|
+
out.failed.push(`${path}: ${(e as Error).message}`)
|
|
182
|
+
}
|
|
183
|
+
} else if (!b.values) continue
|
|
184
|
+
else if (sides.file == null && sides.value == null) learned.set(path, null)
|
|
185
|
+
else if (sides.agreed?.blob != sides.file) {
|
|
186
|
+
// Both sides already say the same thing: remember that they agree.
|
|
187
|
+
learned.set(path, { blob: sides.file!, hash: sides.value })
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (out.read.length) {
|
|
191
|
+
try {
|
|
192
|
+
await b.read!(out.read, gone)
|
|
193
|
+
} catch (e) {
|
|
194
|
+
out.failed.push(`${b.name}: ${(e as Error).message}`)
|
|
195
|
+
out.read = []
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (learned.size) await b.remember?.(learned)
|
|
199
|
+
return out
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The covered files among `paths` that exist, with their blob ids. */
|
|
203
|
+
export let present = async (paths: string[]): Promise<Map<string, string>> => {
|
|
204
|
+
let out = new Map<string, string>()
|
|
205
|
+
for (let path of paths) {
|
|
206
|
+
let text: string
|
|
207
|
+
try {
|
|
208
|
+
text = await io.readTextFile(path)
|
|
209
|
+
} catch {
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
out.set(path, await blobOf(text))
|
|
213
|
+
}
|
|
214
|
+
return out
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A memory kept as one JSON file — for a binding whose graph side cannot
|
|
218
|
+
* hold it. A missing or unreadable file remembers nothing. */
|
|
219
|
+
export let memo = (
|
|
220
|
+
file: string,
|
|
221
|
+
): Pick<Binding, 'agreed' | 'remember'> => {
|
|
222
|
+
let load = async (): Promise<Record<string, Agreed>> => {
|
|
223
|
+
try {
|
|
224
|
+
return JSON.parse(await io.readTextFile(file))
|
|
225
|
+
} catch {
|
|
226
|
+
return {}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
agreed: async () => new Map(Object.entries(await load())),
|
|
231
|
+
remember: async (changed) => {
|
|
232
|
+
let all = await load()
|
|
233
|
+
for (let [path, a] of changed) {
|
|
234
|
+
if (a) all[path] = a
|
|
235
|
+
else delete all[path]
|
|
236
|
+
}
|
|
237
|
+
await put(file, JSON.stringify(all, null, 1) + '\n')
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/mirror",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/mirror",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@yaks/file": "0.2.36",
|
|
15
|
+
"@yaks/git": "0.2.36"
|
|
16
|
+
},
|
|
17
|
+
"description": "Syncs files and a graph through bindings, each direction optional, reporting a path changed on both sides as a conflict instead of overwriting it.",
|
|
18
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
19
|
+
}
|