@trama-dev/runtime 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/agent-backend.d.ts +51 -0
  2. package/dist/agent-backend.js +4 -0
  3. package/dist/agent-host.d.ts +46 -7
  4. package/dist/agent-host.js +59 -19
  5. package/dist/agent-hosts.js +122 -56
  6. package/dist/agent.d.ts +16 -5
  7. package/dist/agent.js +22 -64
  8. package/dist/bridge-client.d.ts +6 -2
  9. package/dist/bridge-client.js +6 -1
  10. package/dist/client.d.ts +2 -2
  11. package/dist/client.js +126 -2
  12. package/dist/context.js +3 -2
  13. package/dist/history.d.ts +9 -0
  14. package/dist/history.js +98 -0
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.js +70 -41
  17. package/dist/ipc-server.d.ts +2 -3
  18. package/dist/ipc-server.js +93 -37
  19. package/dist/journal-view.js +19 -9
  20. package/dist/journal.d.ts +10 -4
  21. package/dist/journal.js +28 -6
  22. package/dist/merge-deeds.d.ts +22 -0
  23. package/dist/merge-deeds.js +201 -0
  24. package/dist/merge-doing.d.ts +9 -0
  25. package/dist/merge-doing.js +410 -0
  26. package/dist/merge-settle.d.ts +14 -0
  27. package/dist/merge-settle.js +166 -0
  28. package/dist/merge-shared.d.ts +64 -0
  29. package/dist/merge-shared.js +159 -0
  30. package/dist/merge.d.ts +150 -0
  31. package/dist/merge.js +209 -0
  32. package/dist/model-relay.js +35 -5
  33. package/dist/own-tools.d.ts +60 -0
  34. package/dist/own-tools.js +209 -0
  35. package/dist/path-guard.js +8 -6
  36. package/dist/pi-adapter.d.ts +22 -0
  37. package/dist/pi-adapter.js +147 -46
  38. package/dist/project.d.ts +31 -0
  39. package/dist/project.js +253 -0
  40. package/dist/replace-file.d.ts +13 -2
  41. package/dist/replace-file.js +23 -6
  42. package/dist/runner-backend.d.ts +43 -0
  43. package/dist/runner-backend.js +125 -0
  44. package/dist/runner-types.d.ts +8 -0
  45. package/dist/runner.d.ts +8 -123
  46. package/dist/runner.js +57 -453
  47. package/dist/schema.d.ts +38 -0
  48. package/dist/schema.js +104 -0
  49. package/dist/stopping.js +2 -3
  50. package/dist/tools.js +31 -7
  51. package/dist/tree.d.ts +33 -149
  52. package/dist/tree.js +43 -1077
  53. package/dist/types.d.ts +54 -2
  54. package/dist/workspace.d.ts +20 -0
  55. package/dist/workspace.js +76 -12
  56. package/package.json +2 -2
@@ -0,0 +1,22 @@
1
+ import { type Entry } from "./tree.js";
2
+ import type { Changes, Given, Left, Opened, Told } from "./merge-shared.js";
3
+ /** One thing to do: at a place holding `there`, to have `want` (nothing, for what the run removed). */
4
+ export interface Deed {
5
+ path: string;
6
+ place: string;
7
+ want: Entry | undefined;
8
+ there: Entry | undefined;
9
+ }
10
+ /** Whether a deed is at a directory that is to be a directory: what it allows is all that changes there. */
11
+ export declare const staysADirectory: ({ want, there }: Pick<Deed, "want" | "there">) => boolean;
12
+ /** Whether a deed is at a directory that is to go — removed, or with something else in its place. */
13
+ export declare const goesAsADirectory: ({ want, there }: Pick<Deed, "want" | "there">) => boolean;
14
+ /**
15
+ * The deeds of a merge, a path before every path under it — and, of the parts
16
+ * of things the run left as they were, those that are not so in the project
17
+ * (`amiss`: by the path, why). What cannot be done is told as refused.
18
+ */
19
+ export declare function deedsOf({ real, before, after, seed }: Pick<Given, "real" | "before" | "after" | "seed">, { changed, isChanged, parts }: Pick<Changes, "changed" | "isChanged" | "parts">, { refused, leftAlone }: Pick<Told, "refused" | "leftAlone">, { wanted, holds }: Pick<Left, "wanted" | "holds">, { open }: Pick<Opened, "open">): {
20
+ deeds: Deed[];
21
+ amiss: Map<string, string>;
22
+ };
@@ -0,0 +1,201 @@
1
+ // What a merge is to do, found out before anything is done (see merge.ts).
2
+ //
3
+ // It is apart because it only looks. Of the real directory it asks what is
4
+ // there, and opens a directory to ask; it makes nothing, moves nothing and
5
+ // removes nothing. What it comes to is the deeds — each a place, what is
6
+ // there, and what is to be there — and, for what cannot be done, why: which
7
+ // is told, and is all that is done about it. What it has to remember while it
8
+ // looks — the directories still to be made, the ways and the names asked
9
+ // after — is its own, and is gone when it is done.
10
+ import { basename, dirname, join } from "path";
11
+ import { identityOf } from "./path-guard.js";
12
+ import { messageOf } from "./errors.js";
13
+ import { ITSELF, entryAt, inOrder, namesIn, noSuchPlace, same } from "./tree.js";
14
+ /** Whether a deed is at a directory that is to be a directory: what it allows is all that changes there. */
15
+ export const staysADirectory = ({ want, there }) => want?.kind === "dir" && there?.kind === "dir";
16
+ /** Whether a deed is at a directory that is to go — removed, or with something else in its place. */
17
+ export const goesAsADirectory = ({ want, there }) => there?.kind === "dir" && want?.kind !== "dir";
18
+ /**
19
+ * The deeds of a merge, a path before every path under it — and, of the parts
20
+ * of things the run left as they were, those that are not so in the project
21
+ * (`amiss`: by the path, why). What cannot be done is told as refused.
22
+ */
23
+ export function deedsOf({ real, before, after, seed }, { changed, isChanged, parts }, { refused, leftAlone }, { wanted, holds }, { open }) {
24
+ /** Directories the run made that are to be made here: they are not in the real directory yet. */
25
+ const toMake = new Set();
26
+ /**
27
+ * Whether the way to a directory of the tree is the real directory's own:
28
+ * every step of it a directory, none a link put there since. (Asked as far
29
+ * as the way exists: a directory still to be made is made by this merge.)
30
+ */
31
+ const own = new Map();
32
+ const ownWay = (dir) => {
33
+ if (dir === ITSELF)
34
+ return true;
35
+ if (toMake.has(dir))
36
+ return ownWay(dirname(dir));
37
+ let known = own.get(dir);
38
+ if (known === undefined) {
39
+ known = ownWay(dirname(dir)) && entryAt(join(real, dir))?.kind === "dir" && spells(dirname(dir), basename(dir));
40
+ own.set(dir, known);
41
+ }
42
+ return known;
43
+ };
44
+ /** Whether a directory the run did not change is as it was when the copy was made. Asked once, before it is opened. */
45
+ const asItWas = new Map();
46
+ const unchanged = (dir) => {
47
+ let known = asItWas.get(dir);
48
+ if (known === undefined)
49
+ asItWas.set(dir, known = same(entryAt(join(real, dir)), seed.get(dir)));
50
+ return known;
51
+ };
52
+ /** Whether a directory lists a name as it is spelled. */
53
+ const listed = new Map();
54
+ const spells = (dir, name) => {
55
+ let names = listed.get(dir);
56
+ if (!names)
57
+ listed.set(dir, names = new Set(namesIn(join(real, dir))));
58
+ return names.has(name);
59
+ };
60
+ /** What is in the real directory under a name that is not the directory's own for it: by what it is, the tree's path. */
61
+ const otherwise = new Map();
62
+ /**
63
+ * What is to be done at a path — nothing, if the place is as the run left
64
+ * it already. Throws if it cannot be done: with why, which is all that is
65
+ * done about it. It looks, and opens a directory to look; no more. (So
66
+ * every directory in which something is then done is open by then.)
67
+ */
68
+ const decide = (path) => {
69
+ const was = before.get(path);
70
+ const is = after.get(path);
71
+ const place = join(real, path);
72
+ const dir = dirname(path);
73
+ let there;
74
+ /** That somebody changed the directory it is in — which is said only once it is known that something needs doing there. */
75
+ let notInIt;
76
+ if (path === ITSELF) {
77
+ // The directory itself is in no directory of the tree: it is where it is, by the name it was given.
78
+ there = entryAt(place);
79
+ }
80
+ else if (!toMake.has(dir)) {
81
+ // (In a directory still to be made there is nothing — whatever is, for now, at the place of that directory.)
82
+ if (!ownWay(dir)) {
83
+ // No way that is the directory's own leads there any more. What the run removed is, then, not there —
84
+ // unless something is, by the way as it is now: under another spelling of a directory, or through a
85
+ // link put in its place. That is somebody's, and is left; but it is not gone, and that is said.
86
+ if (!is) {
87
+ let found;
88
+ try {
89
+ found = entryAt(place);
90
+ }
91
+ catch (err) {
92
+ if (!noSuchPlace(err))
93
+ throw err;
94
+ }
95
+ if (!found)
96
+ return undefined;
97
+ }
98
+ throw new Error("the directory it is in is no longer there in the project, by that name");
99
+ }
100
+ // The directory it is in. One the run changed has its own turn, before this; any other must be as it was.
101
+ if (!isChanged.has(dir) && !unchanged(dir)) {
102
+ notInIt = new Error(dir === ITSELF ? "what the project's own directory allows was changed meanwhile" : `what ${dir} allows was changed in the project meanwhile`);
103
+ }
104
+ let byItsName;
105
+ try {
106
+ // It is looked into as it will be changed: opened, if it is closed — unless that it is closed is somebody
107
+ // else's doing. (What the run left as it was is only looked at, which a directory that held it allows.)
108
+ if (!notInIt && isChanged.has(path))
109
+ open(dir);
110
+ there = entryAt(place);
111
+ byItsName = !there || spells(dir, basename(path));
112
+ }
113
+ catch (err) {
114
+ throw notInIt ?? err;
115
+ }
116
+ if (!byItsName) {
117
+ const what = identityOf(place);
118
+ if (what)
119
+ otherwise.set(what, path);
120
+ throw new Error("the project's file system takes that name for one that is there already, spelled otherwise: what is there is left as it was");
121
+ }
122
+ }
123
+ if (!is && !there)
124
+ return undefined;
125
+ // What the run left as it was, and is as it was in the project, needs nothing done. (It is looked at because
126
+ // it is part of what is one thing: see the loop below.)
127
+ if (!isChanged.has(path) && same(there, seed.get(path)))
128
+ return undefined;
129
+ const want = is && wanted(path, is);
130
+ // A place that holds what the run left needs nothing done — whoever made it so, and whatever became of its
131
+ // directory, as long as it can still be looked into: one that somebody closed is not opened to find out.
132
+ if (want && holds(there, place, want, path))
133
+ return undefined;
134
+ if (notInIt)
135
+ throw notInIt;
136
+ if (!was && there)
137
+ throw new Error("something else is at that place in the project");
138
+ if (was && !same(there, seed.get(path))) {
139
+ throw new Error(want ? "it was changed or removed in the project meanwhile" : "the run removed it, but it was changed in the project meanwhile");
140
+ }
141
+ if (goesAsADirectory({ want, there })) {
142
+ // It is to go — and can, if it holds nothing but what this merge removes from it.
143
+ const goes = (child) => before.has(child) && !after.has(child);
144
+ const kept = namesIn(place).find((name) => !goes(`${path}/${name}`));
145
+ if (kept !== undefined)
146
+ throw new Error(`the run removed it, but it holds something in the project that is not removed with it (${kept})`);
147
+ }
148
+ if (want?.kind === "dir" && there?.kind !== "dir")
149
+ toMake.add(path);
150
+ return { path, place, want, there };
151
+ };
152
+ const deeds = [];
153
+ /** Parts of things that the run left as they were, and that are not so in the project: by the path, why. */
154
+ const amiss = new Map();
155
+ // What the run changed — and the parts of what is one thing that it did not: nothing is to be done there, but
156
+ // the rest of the thing leans on their being there as they were.
157
+ for (const path of [...new Set([...changed, ...parts])].sort(inOrder)) {
158
+ if (leftAlone(path))
159
+ continue;
160
+ try {
161
+ const deed = decide(path);
162
+ if (deed)
163
+ deeds.push(deed);
164
+ }
165
+ catch (err) {
166
+ // (A part the run left as it was did not fail to arrive: nothing under it is left alone for its sake.)
167
+ (isChanged.has(path) ? refused : amiss).set(path, messageOf(err));
168
+ }
169
+ }
170
+ // What the real directory takes for one name is one thing there: nothing is done to it under any name — nor,
171
+ // if it is a directory, in it: a path through it is a path through the other.
172
+ if (otherwise.size > 0) {
173
+ /** The path the run left that is taken for a name of a directory, or of one it is in — if any is. */
174
+ const takenFor = new Map();
175
+ const through = (dir) => {
176
+ if (dir === ITSELF)
177
+ return undefined;
178
+ if (!takenFor.has(dir))
179
+ takenFor.set(dir, otherwise.get(identityOf(join(real, dir)) ?? "") ?? through(dirname(dir)));
180
+ return takenFor.get(dir);
181
+ };
182
+ for (const { path, place, there } of deeds) {
183
+ if (path === ITSELF || leftAlone(path))
184
+ continue;
185
+ try {
186
+ const other = there && otherwise.get(identityOf(place) ?? "");
187
+ const above = other === undefined ? through(dirname(path)) : undefined;
188
+ if (other !== undefined) {
189
+ refused.set(path, `the run left ${other}, which the project's file system takes for a name of this, or of what is one file with it: it is left as it was`);
190
+ }
191
+ else if (above !== undefined) {
192
+ refused.set(path, `the run left ${above}, which the project's file system takes for a name of a directory this is in: it is left as it was`);
193
+ }
194
+ }
195
+ catch (err) {
196
+ refused.set(path, messageOf(err));
197
+ }
198
+ }
199
+ }
200
+ return { deeds, amiss };
201
+ }
@@ -0,0 +1,9 @@
1
+ import type { Changes, Given, Left, Opened, Told } from "./merge-shared.js";
2
+ import { type Deed } from "./merge-deeds.js";
3
+ import type { Settling } from "./merge-settle.js";
4
+ /**
5
+ * Do the deeds of a merge, as far as the system will have them done. What it
6
+ * will not have done is told as refused, with what follows from that; what
7
+ * there is to say besides is told as that. Throws nothing.
8
+ */
9
+ export declare function carryOut(deeds: Deed[], { real, before, after }: Pick<Given, "real" | "before" | "after">, { partOf }: Pick<Changes, "partOf">, { refused, leftAlone, underOne, tell }: Pick<Told, "refused" | "leftAlone" | "underOne" | "tell">, { stagedFile, holdsBytes }: Pick<Left, "stagedFile" | "holdsBytes">, { open, toClose }: Opened, { settle, heldBack, deedAt }: Settling): void;
@@ -0,0 +1,410 @@
1
+ // The deeds of a merge, done (see merge.ts).
2
+ //
3
+ // It is apart because it is the part that writes: whatever a merge makes,
4
+ // moves or removes in the real directory is made, moved or removed here, and
5
+ // the order in which that is done is here in one piece. What it has to
6
+ // remember of what it has done so far — what was made ready, moved aside,
7
+ // put, and where a directory is by now — is its own. It decides nothing of
8
+ // what is to be done, and follows nothing up: after each round it asks that
9
+ // what was refused in it be followed up, and goes by what is then left alone.
10
+ import { accessSync, chmodSync, constants, copyFileSync, lstatSync, mkdirSync, renameSync, rmdirSync, rmSync, symlinkSync, unlinkSync, } from "fs";
11
+ import { basename, dirname, join, relative, sep } from "path";
12
+ import { identityOf } from "./path-guard.js";
13
+ import { stagedNameFor } from "./replace-file.js";
14
+ import { messageOf } from "./errors.js";
15
+ import { ITSELF, addTo, gone, idOf, inOrder, noSuchPlace } from "./tree.js";
16
+ import { staysADirectory } from "./merge-deeds.js";
17
+ /**
18
+ * Do the deeds of a merge, as far as the system will have them done. What it
19
+ * will not have done is told as refused, with what follows from that; what
20
+ * there is to say besides is told as that. Throws nothing.
21
+ */
22
+ export function carryOut(deeds, { real, before, after }, { partOf }, { refused, leftAlone, underOne, tell }, { stagedFile, holdsBytes }, { open, toClose }, { settle, heldBack, deedAt }) {
23
+ /** The deeds, by the directory each is in. */
24
+ const inDir = new Map();
25
+ for (const deed of deeds)
26
+ addTo(inDir, dirname(deed.path), deed);
27
+ /** A place of the real directory, as a path of it. */
28
+ const pathOf = (place) => relative(real, place).split(sep).join("/") || ITSELF;
29
+ const leftover = (path, failed) => `what was at ${path}, and was to go, is here: the system would not have it all removed (${messageOf(failed)})`;
30
+ /** Remove what was made beside a place and is not going to be moved in. What will not go is told of. Throws nothing. */
31
+ const discard = (made, madeFor) => {
32
+ try {
33
+ rmSync(made, { force: true });
34
+ }
35
+ catch (err) {
36
+ // It is told of if it is there — or cannot be known not to be.
37
+ let there = true;
38
+ try {
39
+ there = lstatSync(made, { throwIfNoEntry: false }) !== undefined;
40
+ }
41
+ catch (cannot) {
42
+ there = !noSuchPlace(cannot);
43
+ }
44
+ if (there)
45
+ tell(pathOf(made), `it was made ready for ${madeFor}, and the system would not have it removed (${messageOf(err)})`);
46
+ }
47
+ };
48
+ /** Make a file or a link the run left beside its place, under a name of its own, ready to be moved in. */
49
+ const beside = (path, want, place) => {
50
+ const made = stagedNameFor(place);
51
+ try {
52
+ if (want.kind === "link") {
53
+ symlinkSync(want.text, made);
54
+ }
55
+ else if (want.kind === "file") {
56
+ copyFileSync(stagedFile(path), made, constants.COPYFILE_EXCL);
57
+ if (modeAt(made) !== want.mode)
58
+ chmodSync(made, want.mode);
59
+ }
60
+ }
61
+ catch (err) {
62
+ discard(made, path);
63
+ throw err;
64
+ }
65
+ return made;
66
+ };
67
+ /** What has been made beside its place and is yet to be moved in. */
68
+ const ready = new Map();
69
+ /**
70
+ * What was at a place and has been moved aside, under a name of its own
71
+ * beside it, for what the run left to take the place: by the path, where
72
+ * it is now. It is kept until all that follows from its going is known to
73
+ * have arrived, and comes back if not.
74
+ */
75
+ const aside = new Map();
76
+ /** The places where this merge has put something in the place of what was there, or of nothing. */
77
+ const put = new Set();
78
+ /** What this merge has put where nothing was: by what it is, the path. */
79
+ const putAnew = new Map();
80
+ /** The directories that are not, now, where their path says: by the path, where they are — or were last, if they have gone since. */
81
+ const elsewhere = new Map();
82
+ const setAside = ({ path, place, there }) => {
83
+ const kept = stagedNameFor(place);
84
+ renameSync(place, kept);
85
+ if (there?.kind === "dir")
86
+ elsewhere.set(path, kept);
87
+ return kept;
88
+ };
89
+ /**
90
+ * Remove from a directory what the run removed from it — all it held when
91
+ * that was looked at; more than that is not removed — and say what the
92
+ * system would not have removed, if anything. What will go, goes, whatever
93
+ * would not go before it.
94
+ */
95
+ const emptied = (dir, at) => {
96
+ let failed;
97
+ for (const inIt of inDir.get(dir) ?? []) {
98
+ if (!inIt.there || aside.has(inIt.path))
99
+ continue; // (what was moved aside to go has gone, or is told of where it is)
100
+ const its = join(at, basename(inIt.path));
101
+ try {
102
+ if (inIt.there.kind === "dir") {
103
+ const inThat = emptied(inIt.path, its);
104
+ failed ??= inThat;
105
+ rmdirSync(its);
106
+ }
107
+ else {
108
+ unlinkSync(its);
109
+ }
110
+ }
111
+ catch (err) {
112
+ failed ??= err;
113
+ }
114
+ }
115
+ return failed;
116
+ };
117
+ /** The directories that were to go and have been seen to as one, with all that was under them. */
118
+ const seenTo = new Set();
119
+ /** Remove what the run removed from a directory, and the directory, where it is. Says what the system would not have removed, if anything. */
120
+ const removed = (deed, at) => {
121
+ seenTo.add(deed.path);
122
+ let failed = emptied(deed.path, at);
123
+ try {
124
+ rmdirSync(at);
125
+ }
126
+ catch (err) {
127
+ failed ??= err;
128
+ }
129
+ return failed;
130
+ };
131
+ /**
132
+ * The places where what is there stays and takes what the run left into
133
+ * itself: a directory that is to be a directory; or a file the run wrote
134
+ * into — the same file, afterwards, not another put in its place — which
135
+ * may be written into, or holds what the run left already. Anything else
136
+ * that is there is taken the place of. So is a file that is one thing with
137
+ * other paths: what is written into cannot be taken back, should the rest
138
+ * of the thing not arrive, and what is put in a place can.
139
+ */
140
+ const takes = new Set();
141
+ for (const { path, place, want, there } of deeds) {
142
+ if (!want || !there || leftAlone(path))
143
+ continue;
144
+ const itself = !partOf.has(path) && want.kind === "file" && there.kind === "file" && idOf(before.get(path)) === idOf(after.get(path))
145
+ && (mayBeWrittenTo(place) || holdsBytes(there, place, want, path));
146
+ if (itself || staysADirectory({ want, there }))
147
+ takes.add(path);
148
+ }
149
+ /** Do one step of each deed that still stands. What the system will not have done is left alone. */
150
+ const each = (list, step) => {
151
+ for (const deed of list) {
152
+ if (leftAlone(deed.path))
153
+ continue;
154
+ try {
155
+ step(deed);
156
+ }
157
+ catch (err) {
158
+ refused.set(deed.path, messageOf(err));
159
+ }
160
+ }
161
+ };
162
+ settle();
163
+ // What is to be put where something is, is made first, beside the place: if the system will not have it made,
164
+ // nothing has been touched for its sake. (Also what a file is to take into itself: see below.)
165
+ each(deeds, ({ path, place, want, there }) => {
166
+ if (!there || !want || want.kind === "dir")
167
+ return;
168
+ if (takes.has(path) && want.kind === "file" && holdsBytes(there, place, want, path))
169
+ return;
170
+ ready.set(path, beside(path, want, place));
171
+ });
172
+ settle(); // (so that what follows from what could not be made ready is not first done, and then undone)
173
+ // What the run made, or changed — a directory before what goes in it. What is in the place of it is moved aside.
174
+ each(deeds, (deed) => {
175
+ const { path, place, want, there } = deed;
176
+ if (!want) {
177
+ // What is to go, and is one thing with other paths, is moved aside like what is taken the place of: it
178
+ // comes back if the rest of the thing does not arrive. (Unless it has gone aside already, in its directory.)
179
+ if (there && partOf.has(path) && !underOne(path, elsewhere))
180
+ aside.set(path, setAside(deed));
181
+ return;
182
+ }
183
+ const made = ready.get(path);
184
+ if (takes.has(path)) {
185
+ if (want.kind === "dir") {
186
+ // What it allows is what changed: it is opened like any other, and closed to that. (Not one whose thing
187
+ // does not arrive: nothing is done to that for its own sake.)
188
+ if (heldBack.has(path))
189
+ return;
190
+ open(path);
191
+ const closing = toClose.get(path);
192
+ if (!closing)
193
+ throw new Error("it is no longer a directory in the project");
194
+ // Of what is one thing, whether the system will have it changed is to be known now, while the rest can
195
+ // still be taken back — not at the end, when it is closed. (It is changed now as far as leaves it open.)
196
+ if (partOf.has(path)) {
197
+ chmodSync(place, want.mode | 0o700);
198
+ closing.has = want.mode | 0o700;
199
+ }
200
+ closing.mode = want.mode;
201
+ return;
202
+ }
203
+ if (want.kind !== "file")
204
+ return;
205
+ if (made !== undefined) {
206
+ // It is written into, and stays the file it is. Should the system not have that — and it may have begun,
207
+ // and what the file held be gone — the new file, which is whole beside it, takes its place, and that is
208
+ // told of: it is another file then, and what was another name of the old one is none of this one.
209
+ try {
210
+ copyFileSync(stagedFile(path), place);
211
+ }
212
+ catch (err) {
213
+ try {
214
+ renameSync(made, place);
215
+ }
216
+ catch (again) {
217
+ ready.delete(path); // (it is not removed: it is the only whole one there is)
218
+ throw new Error(`${messageOf(err)} — and a new file could not be put in its place either (${messageOf(again)}): what it held may be damaged, and what it was to hold is beside it, as ${basename(made)}`);
219
+ }
220
+ ready.delete(path);
221
+ tell(path, `it could not be written into (${messageOf(err)}): a new file, holding what the run left, was put in its place`);
222
+ }
223
+ if (ready.delete(path))
224
+ discard(made, path);
225
+ }
226
+ try {
227
+ if (modeAt(place) !== want.mode)
228
+ chmodSync(place, want.mode);
229
+ }
230
+ catch (err) {
231
+ tell(path, `what it is to allow (${want.mode.toString(8)}) could not be set (${messageOf(err)}); what it holds arrived`);
232
+ }
233
+ return;
234
+ }
235
+ if (there)
236
+ aside.set(path, setAside(deed));
237
+ try {
238
+ if (want.kind === "dir") {
239
+ mkdirSync(place);
240
+ put.add(path);
241
+ const closing = { id: identityOf(place), mode: want.mode, has: -1 };
242
+ toClose.set(path, closing);
243
+ chmodSync(place, want.mode | 0o700);
244
+ closing.has = want.mode | 0o700;
245
+ }
246
+ else if (made !== undefined) {
247
+ renameSync(made, place);
248
+ ready.delete(path);
249
+ put.add(path);
250
+ }
251
+ else if (want.kind === "link") {
252
+ symlinkSync(want.text, place);
253
+ put.add(path);
254
+ }
255
+ else if (want.kind === "file") {
256
+ copyFileSync(stagedFile(path), place, constants.COPYFILE_EXCL);
257
+ put.add(path);
258
+ if (modeAt(place) !== want.mode)
259
+ chmodSync(place, want.mode);
260
+ }
261
+ }
262
+ catch (err) {
263
+ // Where nothing was, and something is in the way now that this merge put there itself, under another path:
264
+ // the real directory takes the two names for one. That could not be seen while neither was there. Neither
265
+ // is put there — the other is taken back, with what follows from that.
266
+ if (!there && !put.has(path)) {
267
+ let other;
268
+ try {
269
+ other = putAnew.get(identityOf(place) ?? "");
270
+ }
271
+ catch {
272
+ // (what cannot be looked at is not known to be that)
273
+ }
274
+ if (other !== undefined) {
275
+ refused.set(other, `the run left ${path} too, which the project's file system takes for the same name: neither is put there`);
276
+ throw new Error(`the run left ${other} too, which the project's file system takes for the same name: neither is put there`);
277
+ }
278
+ }
279
+ throw err;
280
+ }
281
+ if (!there) {
282
+ const what = identityOf(place);
283
+ if (what !== undefined)
284
+ putAnew.set(what, path);
285
+ }
286
+ });
287
+ settle();
288
+ // What was moved aside: where what took its place stands, it goes; where that did not stand after all — the
289
+ // system would not have it put there, or what was moved from there did not arrive where the run moved it — what
290
+ // was put there goes, and it comes back. The deepest first.
291
+ for (const deed of [...deeds].reverse()) {
292
+ const { path, place, want, there } = deed;
293
+ const kept = aside.get(path);
294
+ if (!leftAlone(path)) {
295
+ if (kept === undefined)
296
+ continue;
297
+ let failed;
298
+ try {
299
+ if (there?.kind === "dir")
300
+ failed = removed(deed, kept);
301
+ else
302
+ unlinkSync(kept);
303
+ }
304
+ catch (err) {
305
+ failed = err;
306
+ }
307
+ if (failed !== undefined)
308
+ tell(pathOf(kept), leftover(path, failed));
309
+ }
310
+ else if (kept !== undefined || put.has(path)) {
311
+ try {
312
+ // Only what this merge put there goes — of a directory, the directory, once what was put in it has gone.
313
+ if (put.has(path)) {
314
+ if (want?.kind === "dir") {
315
+ rmdirSync(place);
316
+ toClose.delete(path); // (it is gone: there is nothing of it to close)
317
+ }
318
+ else {
319
+ unlinkSync(place);
320
+ }
321
+ }
322
+ if (kept !== undefined) {
323
+ renameSync(kept, place);
324
+ elsewhere.delete(path);
325
+ }
326
+ }
327
+ catch (err) {
328
+ const where = kept === undefined ? "" : `: what was there is beside it, as ${basename(kept)}`;
329
+ refused.set(path, `${refused.get(path) ?? "it is in what did not arrive"} — and it could not be put back as it was (${messageOf(err)})${where}`);
330
+ }
331
+ }
332
+ }
333
+ // And what the run removed, where it is: nothing waits for it any more, and nothing is to take its place. Of a
334
+ // directory the system will not have removed, or not with all that was in it, what is left is there — as it
335
+ // would be had the run removed it there.
336
+ each(deeds, (deed) => {
337
+ const { path, place, want, there } = deed;
338
+ if (want || !there || aside.has(path) || underOne(path, seenTo))
339
+ return;
340
+ if (there.kind !== "dir")
341
+ return void unlinkSync(place);
342
+ const failed = removed(deed, place);
343
+ if (failed !== undefined)
344
+ tell(path, `the run removed it, and the system would not have it removed, or not all that was in it (${messageOf(failed)}): what is left is there`);
345
+ });
346
+ // What was made ready and never moved in is not left lying there.
347
+ for (const [path, made] of ready)
348
+ discard(made, path);
349
+ // What a directory was to be left allowing, of a thing that did not arrive, does not arrive: it is closed to
350
+ // what it allowed.
351
+ for (const [path, what] of heldBack) {
352
+ const { want, there } = deedAt.get(path);
353
+ if (leftAlone(path) || want?.kind !== "dir" || there?.kind !== "dir")
354
+ continue;
355
+ const closing = toClose.get(path);
356
+ if (closing)
357
+ closing.mode = there.mode;
358
+ tell(path, `what it was to allow (${want.mode.toString(8)}) is one thing with ${what}: it is left allowing what it allowed`);
359
+ }
360
+ // The directories that this merge has allowing something else than they are to be left allowing are closed — the
361
+ // deepest first, each where it is now, and only if it is that directory: at the place of one that is gone there
362
+ // may be something else by now.
363
+ const placeNow = (path) => {
364
+ for (let end = path.length; end !== -1; end = path.lastIndexOf("/", end - 1)) {
365
+ const moved = elsewhere.get(path.slice(0, end));
366
+ if (moved !== undefined)
367
+ return moved + path.slice(end);
368
+ }
369
+ return join(real, path);
370
+ };
371
+ for (const [path, { id, mode, has }] of [...toClose].sort(([a], [b]) => inOrder(b, a))) {
372
+ if (has === mode)
373
+ continue;
374
+ const place = placeNow(path);
375
+ try {
376
+ if (id === undefined || identityOf(place) !== id)
377
+ continue;
378
+ }
379
+ catch (err) {
380
+ // Where there is no such place there is nothing to close. Where it cannot be come to, it may be there, open.
381
+ if (!gone(err) && err.code !== "ENOTDIR") {
382
+ tell(pathOf(place), `it was opened, and cannot be come to to be given back what it is to allow (${mode.toString(8)}): ${messageOf(err)}`);
383
+ }
384
+ continue;
385
+ }
386
+ try {
387
+ if (modeAt(place) !== mode)
388
+ chmodSync(place, mode);
389
+ }
390
+ catch (err) {
391
+ // What it was to be left allowing did not arrive, or could not be put back. What is in it is another matter.
392
+ const why = `what it is to allow (${mode.toString(8)}) could not be set (${messageOf(err)})`;
393
+ const at = pathOf(place);
394
+ if (refused.has(at))
395
+ refused.set(at, `${refused.get(at)} — and ${why}`);
396
+ else
397
+ tell(at, `${why}; what is in it is told of by itself, if it did not arrive`);
398
+ }
399
+ }
400
+ }
401
+ const mayBeWrittenTo = (path) => {
402
+ try {
403
+ accessSync(path, constants.W_OK);
404
+ return true;
405
+ }
406
+ catch {
407
+ return false;
408
+ }
409
+ };
410
+ const modeAt = (path) => lstatSync(path).mode & 0o7777;
@@ -0,0 +1,14 @@
1
+ import type { Changes, Given, Told } from "./merge-shared.js";
2
+ import { type Deed } from "./merge-deeds.js";
3
+ /**
4
+ * The following up of a merge: `settle`, which is called whenever something
5
+ * may have been refused since it was last; the directories whose thing did
6
+ * not arrive (`heldBack`); and the deeds, by the path.
7
+ */
8
+ export interface Settling {
9
+ settle(): void;
10
+ heldBack: Map<string, string>;
11
+ deedAt: Map<string, Deed>;
12
+ }
13
+ /** The following up of some deeds. Nothing is followed up until `settle` is called. */
14
+ export declare function followingUp(deeds: Deed[], amiss: Map<string, string>, { before, after }: Pick<Given, "before" | "after">, { partOf, parts }: Pick<Changes, "partOf" | "parts">, { refused, leftAlone, tell }: Pick<Told, "refused" | "leftAlone" | "tell">): Settling;