watch-win32.ts 3.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6 7 8 9 10 11 12
import * as path from 'path';
import * as cp from 'child_process';
import * as fs from 'fs';
import * as File from 'vinyl';
import * as es from 'event-stream';
import * as filter from 'gulp-filter';
import { Stream } from 'stream';
E
Erich Gamma 已提交
13

A
Alex Dima 已提交
14
const watcherPath = path.join(__dirname, 'watcher.exe');
E
Erich Gamma 已提交
15

A
Alex Dima 已提交
16
function toChangeType(type: '0' | '1' | '2'): 'change' | 'add' | 'unlink' {
E
Erich Gamma 已提交
17 18 19 20 21 22 23
	switch (type) {
		case '0': return 'change';
		case '1': return 'add';
		default: return 'unlink';
	}
}

A
Alex Dima 已提交
24 25 26
function watch(root: string): Stream {
	const result = es.through();
	let child: cp.ChildProcess | null = cp.spawn(watcherPath, [root]);
E
Erich Gamma 已提交
27

28
	child.stdout!.on('data', function (data) {
A
Alex Dima 已提交
29 30 31
		const lines: string[] = data.toString('utf8').split('\n');
		for (let i = 0; i < lines.length; i++) {
			const line = lines[i].trim();
E
Erich Gamma 已提交
32 33 34 35
			if (line.length === 0) {
				continue;
			}

A
Alex Dima 已提交
36 37
			const changeType = <'0' | '1' | '2'>line[0];
			const changePath = line.substr(2);
38 39 40 41 42 43

			// filter as early as possible
			if (/^\.git/.test(changePath) || /(^|\\)out($|\\)/.test(changePath)) {
				continue;
			}

A
Alex Dima 已提交
44
			const changePathFull = path.join(root, changePath);
E
Erich Gamma 已提交
45

A
Alex Dima 已提交
46
			const file = new File({
E
Erich Gamma 已提交
47 48 49
				path: changePathFull,
				base: root
			});
A
Alex Dima 已提交
50
			(<any>file).event = toChangeType(changeType);
E
Erich Gamma 已提交
51 52 53 54
			result.emit('data', file);
		}
	});

55
	child.stderr!.on('data', function (data) {
E
Erich Gamma 已提交
56 57 58
		result.emit('error', data);
	});

J
Joao Moreno 已提交
59
	child.on('exit', function (code) {
E
Erich Gamma 已提交
60 61 62 63 64 65
		result.emit('error', 'Watcher died with code ' + code);
		child = null;
	});

	process.once('SIGTERM', function () { process.exit(0); });
	process.once('SIGTERM', function () { process.exit(0); });
A
Alex Dima 已提交
66
	process.once('exit', function () { if (child) { child.kill(); } });
E
Erich Gamma 已提交
67 68 69 70

	return result;
}

A
Alex Dima 已提交
71
const cache: { [cwd: string]: Stream; } = Object.create(null);
E
Erich Gamma 已提交
72

A
Alex Dima 已提交
73
module.exports = function (pattern: string | string[] | filter.FileFunction, options?: { cwd?: string; base?: string; }) {
E
Erich Gamma 已提交
74 75
	options = options || {};

A
Alex Dima 已提交
76 77
	const cwd = path.normalize(options.cwd || process.cwd());
	let watcher = cache[cwd];
E
Erich Gamma 已提交
78 79 80 81 82

	if (!watcher) {
		watcher = cache[cwd] = watch(cwd);
	}

A
Alex Dima 已提交
83 84
	const rebase = !options.base ? es.through() : es.mapSync(function (f: File) {
		f.base = options!.base!;
E
Erich Gamma 已提交
85 86 87 88 89 90
		return f;
	});

	return watcher
		.pipe(filter(['**', '!.git{,/**}'])) // ignore all things git
		.pipe(filter(pattern))
A
Alex Dima 已提交
91
		.pipe(es.map(function (file: File, cb) {
E
Erich Gamma 已提交
92
			fs.stat(file.path, function (err, stat) {
A
Alex Dima 已提交
93
				if (err && err.code === 'ENOENT') { return cb(undefined, file); }
E
Erich Gamma 已提交
94 95 96 97
				if (err) { return cb(); }
				if (!stat.isFile()) { return cb(); }

				fs.readFile(file.path, function (err, contents) {
A
Alex Dima 已提交
98
					if (err && err.code === 'ENOENT') { return cb(undefined, file); }
E
Erich Gamma 已提交
99 100 101 102
					if (err) { return cb(); }

					file.contents = contents;
					file.stat = stat;
A
Alex Dima 已提交
103
					cb(undefined, file);
E
Erich Gamma 已提交
104 105 106 107
				});
			});
		}))
		.pipe(rebase);
108
};