Typescriptify the main process code (12/N)
Summary: Converted static/watchman.js to typescript Reviewed By: passy Differential Revision: D20071220 fbshipit-source-id: 7d86ffa8dd728246eeac8a01e2573564e8fd6bbe
This commit is contained in:
committed by
Facebook Github Bot
parent
aff433c8ad
commit
8d66c3aba7
@@ -19,7 +19,7 @@ import http from 'http';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import {compileMain} from './build-utils';
|
import {compileMain} from './build-utils';
|
||||||
const Watchman = require('../static/watchman');
|
import Watchman from '../static/watchman';
|
||||||
const Metro = require('../static/node_modules/metro');
|
const Metro = require('../static/node_modules/metro');
|
||||||
const MetroResolver = require('../static/node_modules/metro-resolver');
|
const MetroResolver = require('../static/node_modules/metro-resolver');
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import recursiveReaddir from 'recursive-readdir';
|
|||||||
import expandTilde from 'expand-tilde';
|
import expandTilde from 'expand-tilde';
|
||||||
import pMap from 'p-map';
|
import pMap from 'p-map';
|
||||||
import {homedir} from 'os';
|
import {homedir} from 'os';
|
||||||
const Watchman = require('./watchman');
|
import Watchman from './watchman';
|
||||||
|
|
||||||
const HOME_DIR = homedir();
|
const HOME_DIR = homedir();
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ async function startWatchingPluginsUsingWatchman(
|
|||||||
onPluginChanged: (plugin: PluginInfo) => void,
|
onPluginChanged: (plugin: PluginInfo) => void,
|
||||||
) {
|
) {
|
||||||
// Initializing a watchman for each folder containing plugins
|
// Initializing a watchman for each folder containing plugins
|
||||||
const watchmanRootMap: {[key: string]: any} = {};
|
const watchmanRootMap: {[key: string]: Watchman} = {};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
plugins.map(async plugin => {
|
plugins.map(async plugin => {
|
||||||
const watchmanRoot = path.resolve(plugin.rootDir, '..');
|
const watchmanRoot = path.resolve(plugin.rootDir, '..');
|
||||||
|
|||||||
@@ -7,37 +7,39 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const watchman = require('fb-watchman');
|
import {Client} from 'fb-watchman';
|
||||||
const uuid = require('uuid');
|
import uuid from 'uuid';
|
||||||
const path = require('path');
|
import path from 'path';
|
||||||
|
|
||||||
module.exports = class Watchman {
|
export default class Watchman {
|
||||||
constructor(rootDir) {
|
constructor(private rootDir: string) {}
|
||||||
this.rootDir = rootDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
initialize() {
|
private client?: Client;
|
||||||
|
private watch?: any;
|
||||||
|
private relativeRoot?: string;
|
||||||
|
|
||||||
|
async initialize(): Promise<void> {
|
||||||
if (this.client) {
|
if (this.client) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.client = new watchman.Client();
|
this.client = new Client();
|
||||||
this.client.setMaxListeners(250);
|
this.client.setMaxListeners(250);
|
||||||
return new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
const onError = err => {
|
const onError = (err: Error) => {
|
||||||
this.client.removeAllListeners('error');
|
this.client!.removeAllListeners('error');
|
||||||
reject(err);
|
reject(err);
|
||||||
this.client.end();
|
this.client!.end();
|
||||||
delete this.client;
|
delete this.client;
|
||||||
};
|
};
|
||||||
this.client.once('error', onError);
|
this.client!.once('error', onError);
|
||||||
this.client.capabilityCheck(
|
this.client!.capabilityCheck(
|
||||||
{optional: [], required: ['relative_root']},
|
{optional: [], required: ['relative_root']},
|
||||||
error => {
|
error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
onError(error);
|
onError(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.client.command(
|
this.client!.command(
|
||||||
['watch-project', this.rootDir],
|
['watch-project', this.rootDir],
|
||||||
(error, resp) => {
|
(error, resp) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -57,7 +59,11 @@ module.exports = class Watchman {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async startWatchFiles(relativeDir, handler, options) {
|
async startWatchFiles(
|
||||||
|
relativeDir: string,
|
||||||
|
handler: (resp: any) => void,
|
||||||
|
options: {excludes: string[]},
|
||||||
|
): Promise<void> {
|
||||||
if (!this.watch) {
|
if (!this.watch) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Watchman is not initialized, please call "initialize" function and wait for the returned promise completion before calling "startWatchFiles".',
|
'Watchman is not initialized, please call "initialize" function and wait for the returned promise completion before calling "startWatchFiles".',
|
||||||
@@ -65,7 +71,7 @@ module.exports = class Watchman {
|
|||||||
}
|
}
|
||||||
options = Object.assign({excludes: []}, options);
|
options = Object.assign({excludes: []}, options);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.client.command(['clock', this.watch], (error, resp) => {
|
this.client!.command(['clock', this.watch], (error, resp) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return reject(error);
|
return reject(error);
|
||||||
}
|
}
|
||||||
@@ -76,7 +82,7 @@ module.exports = class Watchman {
|
|||||||
expression: [
|
expression: [
|
||||||
'allof',
|
'allof',
|
||||||
['not', ['type', 'd']],
|
['not', ['type', 'd']],
|
||||||
...options.excludes.map(e => ['not', ['match', e, 'wholename']]),
|
...options!.excludes.map(e => ['not', ['match', e, 'wholename']]),
|
||||||
],
|
],
|
||||||
fields: ['name'],
|
fields: ['name'],
|
||||||
since: clock,
|
since: clock,
|
||||||
@@ -87,22 +93,19 @@ module.exports = class Watchman {
|
|||||||
|
|
||||||
const id = uuid.v4();
|
const id = uuid.v4();
|
||||||
|
|
||||||
this.client.command(
|
this.client!.command(['subscribe', this.watch, id, sub], error => {
|
||||||
['subscribe', this.watch, id, sub],
|
if (error) {
|
||||||
(error, resp) => {
|
return reject(error);
|
||||||
if (error) {
|
}
|
||||||
return reject(error);
|
this.client!.on('subscription', resp => {
|
||||||
|
if (resp.subscription !== id || !resp.files) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
this.client.on('subscription', resp => {
|
handler(resp);
|
||||||
if (resp.subscription !== id || !resp.files) {
|
});
|
||||||
return;
|
resolve();
|
||||||
}
|
});
|
||||||
handler(resp);
|
|
||||||
});
|
|
||||||
resolve();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
Reference in New Issue
Block a user