-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
168 lines (148 loc) · 4.63 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { io } from "socket.io-client";
import { of, fromEvent } from "rxjs";
import { map, switchMap, takeUntil, tap } from "rxjs/operators";
import * as shell from "shelljs";
import {
BRANCH_EVENT,
localFileChange$,
PairChangePayload,
PAIR_FILE_CHANGE_EVENT,
} from "./common";
import * as yargs from "yargs";
import { writeFileSync } from "fs";
const silent = true;
if (!shell.which("git")) {
shell.echo("Sorry, this script requires git");
shell.exit(1);
}
const repoPath = shell.exec("git rev-parse --show-toplevel", { silent });
if (repoPath.code) {
shell.echo("Run this inside a git repo!");
shell.exit(1);
}
process.chdir(repoPath.trim());
const argv = yargs
.option("force", {
alias: "f",
description: "Cleans up the working directory and puts it into stash",
type: "boolean",
})
.help()
.alias("help", "h")
.usage("$0 [<url>] [-f]", "Connects to a running server", (yargs) =>
yargs.positional("url", {
describe: `The address the server logged when started. Should look like 'some-random-string.loca.lt'.
If not provided will attach to localhost:3000 which is used locally by the server`,
type: "string",
default: "http://localhost:3000/",
})
)
.parseSync(process.argv.slice(2));
// prepare working directory
const workingDirectoryDirty = shell
.exec("git status --porcelain")
.toString()
.trim();
if (workingDirectoryDirty) {
if (argv.force) {
console.log("forcing to stash...");
shell.exec("git stash --include-untracked");
} else {
console.log("Working directory not clean! Use --force to stash it?");
process.exit(1);
}
}
const { url } = argv;
const socket$ = of(io(url));
const connection$ = socket$.pipe(
switchMap((socket) => fromEvent(socket, "connect").pipe(map(() => socket))),
switchMap((socket) =>
fromEvent(socket, BRANCH_EVENT).pipe(
map(([branch, sha]) => ({ socket, branch, sha }))
)
)
);
const fileChangeReceived$ = connection$.pipe(
switchMap(({ socket }) =>
fromEvent<PairChangePayload>(socket, PAIR_FILE_CHANGE_EVENT)
)
);
const onConnectAndThenLocalFileChange$ = connection$.pipe(
switchMap(({ socket, branch }) =>
localFileChange$.pipe(
map((x) => ({ socket, ...x, branch })),
takeUntil(
fromEvent(socket, "disconnect").pipe(
tap(() => console.log("disconnect"))
)
)
)
)
);
// consumes
let lastChangeReceived: string;
let lastChangeSent: string;
// initial sync
connection$.subscribe(({ branch, sha }) => {
console.log(
`Connected to server. Server is streaming on branch ${branch}:${sha}`
);
const currentSha = shell.exec("git rev-parse HEAD", { silent }).trim();
const currentBranch = shell
.exec("git rev-parse --abbrev-ref HEAD", { silent })
.trim();
console.log(`This client is using branch ${currentBranch}:${currentSha}`);
if (sha === currentSha && branch === currentBranch) {
console.log("Good! No revision syncing needed!");
return;
}
if (shell.exec(`git show ${sha}`, { silent }).code) {
console.log("Revision not found locally, tring to fetch");
shell.exec(`git fetch`, { silent });
if (shell.exec(`git show ${sha}`, { silent }).code) {
console.log(
"Revision ${sha} not found even after fetching, please make sure server has pushed his work to the remote!"
);
process.exit(1);
}
}
if (branch !== currentBranch) {
if (shell.exec(`git checkout ${branch}`, { silent }).code) {
console.log(`Branch not found creating new one named ${branch}`);
if (shell.exec(`git checkout -b ${branch}`).code) {
console.log(`Problem creating new branch ${branch}`);
process.exit(1);
}
}
}
if (shell.exec(`git reset --hard ${sha}`, { silent }).code) {
console.log(`Failed checking out ${branch}:${sha}`);
}
console.log(`Successfully synced to track ${branch}:${sha}`);
});
onConnectAndThenLocalFileChange$.subscribe(
({ socket, filename, diff: d, untracked }) => {
const diff = d.toString();
if (lastChangeReceived !== diff && lastChangeSent !== diff) {
console.log("emitting change", filename);
socket.emit(PAIR_FILE_CHANGE_EVENT, { filename, diff, untracked });
lastChangeSent = diff;
}
}
);
fileChangeReceived$.subscribe(({ filename, diff, untracked }) => {
console.log(
untracked
? "received untracked file update"
: "received tracked file update",
filename
);
lastChangeReceived = diff;
if (untracked) {
writeFileSync(filename, diff);
} else {
shell.exec(`git checkout ${filename}`, { silent });
shell.ShellString(diff).exec("git apply");
}
});
console.log(`Trying to connect to server ${url}`);