This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathCSharpWatcher.js
More file actions
153 lines (129 loc) · 4.89 KB
/
CSharpWatcher.js
File metadata and controls
153 lines (129 loc) · 4.89 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
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
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*
* Code for working with CodeHelper.exe was inspired by:
* https://github.com/Microsoft/vscode/blob/314e122b16c5c1ca0288c8006e9c9c3039a51cd7/src/vs/workbench/services/files/node/watcher/win32/csharpWatcherService.ts
*/
/*eslint-env node */
/*jslint node: true */
"use strict";
var fs = require("fs");
var fspath = require("path");
var cp = require("child_process");
var anymatch = require('anymatch');
var FileWatcherManager = require("./FileWatcherManager");
function buildMatcher(ignored) {
// in case of a glob like **/.git we want also to ignore its contents **/.git/**
return anymatch(ignored.concat(ignored.map(function (glob) {
return glob + "/**";
})));
}
function watchPath(path, ignored, _watcherMap) {
var ignoreMatcher = buildMatcher(ignored);
var closing = false;
function processLine(line) {
if (line === "") {
return;
}
var parts = line.split("|");
if (parts.length !== 2) {
console.warn("CSharpWatcher unexpected line: '" + line + "'");
return;
}
var type = parseInt(parts[0], 10);
// convert it back to unix path and clear trailing whitespace
var absolutePath = parts[1].replace(/\\/g, "/").replace(/\s+$/g, "");
// convert type to an event
var event;
switch (type) {
case 0:
event = "changed";
break;
case 1:
event = "created";
break;
case 2:
event = "deleted";
break;
default:
console.warn("CSharpWatcher event type: " + type);
return;
}
// make sure ignored events are not emitted
if (ignoreMatcher(absolutePath)) {
return;
}
var parentDirPath = fspath.dirname(absolutePath) + "/";
var entryName = fspath.basename(absolutePath);
// we need stats object for changed event
if (event === "changed") {
fs.stat(absolutePath, function (err, nodeFsStats) {
if (err) {
console.warn("CSharpWatcher err getting stats: " + err.toString());
}
FileWatcherManager.emitChange(event, parentDirPath, entryName, nodeFsStats);
});
} else {
FileWatcherManager.emitChange(event, parentDirPath, entryName, null);
}
}
function onError(err) {
console.warn("CSharpWatcher process error: " + err.toString());
FileWatcherManager.unwatchPath(path);
}
function onExit(code, signal) {
if (!closing || signal !== "SIGTERM") {
console.warn("CSharpWatcher terminated unexpectedly with code: " + code + ", signal: " + signal);
}
FileWatcherManager.unwatchPath(path);
}
try {
var args = [
// fspath.resolve will normalize slashes to windows format
fspath.resolve(path)
];
var handle = cp.spawn(fspath.resolve(__dirname, 'win' + process.arch.slice(-2), "CodeHelper.exe"), args);
// Events over stdout
handle.stdout.on("data", function (buffer) {
var lines = buffer.toString("utf8").split("\n");
while (lines.length > 0) {
processLine(lines.shift());
}
});
// Errors
handle.on("error", onError);
handle.stderr.on("data", onError);
// Exit
handle.on("exit", onExit);
// Add handler for closing to the _watcherMap
_watcherMap[path] = {
close: function () {
closing = true;
handle.kill();
}
};
} catch (err) {
console.warn("Failed to watch file " + path + ": " + (err && err.message));
}
}
exports.watchPath = watchPath;