wireit
Version:
Upgrade your npm scripts to make them smarter and more efficient
69 lines • 2.22 kB
JavaScript
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Deferred } from './deferred.js';
/**
* Monitors the stdout and stderr of a child process line-by-line searching for
* a match of the given regular expression.
*
* Note we can't use readline here because we want to check lines that haven't
* completed yet.
*/
export class LineMonitor {
#child;
#pattern;
#matched;
#stdout;
#stderr;
constructor(child, pattern) {
this.#matched = new Deferred();
this.#stdout = '';
this.#stderr = '';
/**
* Resolves to `{"ok": true}` when a match was found or `{"ok": false}` when
* this monitor was aborted.
*/
this.matched = this.#matched.promise;
this.#onStdout = (data) => {
this.#stdout = this.#check(this.#stdout + String(data));
};
this.#onStderr = (data) => {
this.#stderr = this.#check(this.#stderr + String(data));
};
this.#child = child;
this.#pattern = pattern;
child.stdout.on('data', this.#onStdout);
child.stderr.on('data', this.#onStderr);
}
abort() {
this.#removeEventListeners();
this.#matched.resolve({ ok: false, error: undefined });
}
#removeEventListeners() {
this.#child.stdout.removeListener('data', this.#onStdout);
this.#child.stderr.removeListener('data', this.#onStderr);
}
#onStdout;
#onStderr;
#check(buffer) {
const lines = buffer.split(/\n/g);
let end = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (i !== lines.length - 1) {
// Don't move beyond the final line, since it might be incomplete, and
// we want to match the entire line the next time _check is called.
end += line.length + 1;
}
if (this.#pattern.test(line)) {
this.#removeEventListeners();
this.#matched.resolve({ ok: true, value: undefined });
break;
}
}
return buffer.slice(end);
}
}
//# sourceMappingURL=line-monitor.js.map