Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/watch/watch-win32.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import path from 'path';
7
import cp from 'child_process';
8
import fs from 'fs';
9
import File from 'vinyl';
10
import es from 'event-stream';
11
import filter from 'gulp-filter';
12
import { Stream } from 'stream';
13
14
const watcherPath = path.join(import.meta.dirname, 'watcher.exe');
15
16
function toChangeType(type: '0' | '1' | '2'): 'change' | 'add' | 'unlink' {
17
switch (type) {
18
case '0': return 'change';
19
case '1': return 'add';
20
default: return 'unlink';
21
}
22
}
23
24
function watch(root: string): Stream {
25
const result = es.through();
26
let child: cp.ChildProcess | null = cp.spawn(watcherPath, [root]);
27
28
child.stdout!.on('data', function (data) {
29
const lines: string[] = data.toString('utf8').split('\n');
30
for (let i = 0; i < lines.length; i++) {
31
const line = lines[i].trim();
32
if (line.length === 0) {
33
continue;
34
}
35
36
const changeType = line[0] as '0' | '1' | '2';
37
const changePath = line.substr(2);
38
39
// filter as early as possible
40
if (/^\.git/.test(changePath) || /(^|\\)out($|\\)/.test(changePath)) {
41
continue;
42
}
43
44
const changePathFull = path.join(root, changePath);
45
46
const file = new File({
47
path: changePathFull,
48
base: root
49
});
50
file.event = toChangeType(changeType);
51
result.emit('data', file);
52
}
53
});
54
55
child.stderr!.on('data', function (data) {
56
result.emit('error', data);
57
});
58
59
child.on('exit', function (code) {
60
result.emit('error', 'Watcher died with code ' + code);
61
child = null;
62
});
63
64
process.once('SIGTERM', function () { process.exit(0); });
65
process.once('SIGTERM', function () { process.exit(0); });
66
process.once('exit', function () { if (child) { child.kill(); } });
67
68
return result;
69
}
70
71
const cache: { [cwd: string]: Stream } = Object.create(null);
72
73
export default function (pattern: string | string[] | filter.FileFunction, options?: { cwd?: string; base?: string; dot?: boolean }) {
74
options = options || {};
75
76
const cwd = path.normalize(options.cwd || process.cwd());
77
let watcher = cache[cwd];
78
79
if (!watcher) {
80
watcher = cache[cwd] = watch(cwd);
81
}
82
83
const rebase = !options.base ? es.through() : es.mapSync(function (f: File) {
84
f.base = options!.base!;
85
return f;
86
});
87
88
return watcher
89
.pipe(filter(['**', '!.git{,/**}'], { dot: options.dot })) // ignore all things git
90
.pipe(filter(pattern, { dot: options.dot }))
91
.pipe(es.map(function (file: File, cb) {
92
fs.stat(file.path, function (err, stat) {
93
if (err && err.code === 'ENOENT') { return cb(undefined, file); }
94
if (err) { return cb(); }
95
if (!stat.isFile()) { return cb(); }
96
97
fs.readFile(file.path, function (err, contents) {
98
if (err && err.code === 'ENOENT') { return cb(undefined, file); }
99
if (err) { return cb(); }
100
101
file.contents = contents;
102
file.stat = stat;
103
cb(undefined, file);
104
});
105
});
106
}))
107
.pipe(rebase);
108
}
109
110