提交 5ee721c6 编写于 作者: B Benjamin Pasero

config - need to watch parent folder to detect ADD and DELETE of config

上级 541f0113
......@@ -6,6 +6,7 @@
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import * as objects from 'vs/base/common/objects';
import {IDisposable, dispose, toDisposable} from 'vs/base/common/lifecycle';
import Event, {Emitter} from 'vs/base/common/event';
......@@ -113,7 +114,15 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
private registerWatcher(): void {
// Support for watching symlinks
// Watch the parent of the path so that we detect ADD and DELETES
const parentFolder = path.dirname(this._path);
fs.exists(parentFolder, exists => {
if (exists) {
this.watch(parentFolder);
}
});
// Check if the path is a symlink and watch its target if so
fs.lstat(this._path, (err, stat) => {
if (err || stat.isDirectory()) {
return; // path is not a valid file
......@@ -129,11 +138,6 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
this.watch(realPath);
});
}
// We found a normal file
else {
this.watch(this._path);
}
});
}
......@@ -142,13 +146,17 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
return; // avoid watchers that will never get disposed by checking for being disposed
}
const watcher = fs.watch(path);
watcher.on('change', () => this.onConfigFileChange());
try {
const watcher = fs.watch(path);
watcher.on('change', () => this.onConfigFileChange());
this.disposables.push(toDisposable(() => {
watcher.removeAllListeners();
watcher.close();
}));
this.disposables.push(toDisposable(() => {
watcher.removeAllListeners();
watcher.close();
}));
} catch (error) {
console.warn(`Failed to watch ${path} for configuration changes (${error.toString()})`);
}
}
private onConfigFileChange(): void {
......
......@@ -111,6 +111,50 @@ suite('Config', () => {
});
});
test('watching also works when file created later', function (done: () => void) {
testFile((testFile, cleanUp) => {
let watcher = new ConfigWatcher<{ foo: string; }>(testFile);
fs.writeFileSync(testFile, '// my comment\n{ "foo": "bar" }');
setTimeout(function () {
fs.writeFileSync(testFile, '// my comment\n{ "foo": "changed" }');
}, 50);
watcher.onDidUpdateConfiguration(event => {
assert.ok(event);
assert.equal(event.config.foo, 'changed');
assert.equal(watcher.getValue('foo'), 'changed');
watcher.dispose();
cleanUp(done);
});
});
});
test('watching detects the config file getting deleted', function (done: () => void) {
testFile((testFile, cleanUp) => {
fs.writeFileSync(testFile, '// my comment\n{ "foo": "bar" }');
let watcher = new ConfigWatcher<{ foo: string; }>(testFile);
setTimeout(function () {
fs.unlinkSync(testFile);
}, 50);
watcher.onDidUpdateConfiguration(event => {
assert.ok(event);
watcher.dispose();
cleanUp(done);
});
});
});
test('reload', function (done: () => void) {
testFile((testFile, cleanUp) => {
fs.writeFileSync(testFile, '// my comment\n{ "foo": "bar" }');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册