-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
94 lines (87 loc) · 2.45 KB
/
common.ts
File metadata and controls
94 lines (87 loc) · 2.45 KB
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
import * as chokidar from "chokidar";
import * as fs from "fs";
import { from, Observable } from "rxjs";
import { map } from "rxjs/operators";
import { merge } from "rxjs";
import * as shell from "shelljs";
const silent = true;
const newLineRegex = /\r?\n/;
const trimTrailingSlash = (x: string) => x.replace(/\/$/, "");
const trimleadingSlash = (x: string) => x.replace(/^\//, "");
export type PairChangePayload = {
filename: string;
diff: string;
untracked?: true;
};
export const PAIR_FILE_CHANGE_EVENT = "pair-filechange";
export const BRANCH_EVENT = "branch";
export const localFileChange$ = new Observable<PairChangePayload>(
(subscriber) => {
const ignored = [
".git",
...(fs.existsSync(".gitignore")
? fs
.readFileSync(".gitignore")
.toString()
.split(newLineRegex)
.filter((x) => x && !x.startsWith("#"))
.map(trimTrailingSlash)
.map(trimleadingSlash)
: []),
];
const watcher = chokidar
.watch(".", { ignored, ignoreInitial: true })
.on("all", (event, filename) => {
const isNotTracked = shell.exec(
`git ls-files --error-unmatch ${filename}`,
{ silent }
).code;
if (isNotTracked) {
if (event === "add" || event === "change") {
subscriber.next({
filename,
diff: fs.readFileSync(filename).toString(),
untracked: true,
});
}
} else {
// if tracked
if (event === "change") {
const diff = shell.exec(`git diff -- ${filename}`, { silent });
subscriber.next({ filename, diff });
}
}
});
return () => {
watcher.close();
};
}
);
export const initialServerChangesStream = () =>
merge(
// untracked
from(
shell
.exec("git ls-files --others --exclude-standard", { silent })
.split(newLineRegex)
.filter(Boolean)
).pipe(
map<string, PairChangePayload>((filename) => ({
filename,
diff: fs.readFileSync(filename).toString(),
untracked: true,
}))
),
// unstaged
from(
shell
.exec("git diff HEAD --name-only", { silent })
.split(newLineRegex)
.filter(Boolean)
).pipe(
map<string, PairChangePayload>((filename) => ({
filename,
diff: shell.exec(`git diff HEAD -- ${filename}`, { silent }),
}))
)
);