From abe24c068268241fee87e4b60a5df9acaa0511da Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Sun, 10 Feb 2019 00:57:10 -0800 Subject: [PATCH 1/5] What if Watchman CLI is missing... --- packages/relay-compiler/bin/RelayCompilerMain.js | 4 ++-- packages/relay-compiler/core/GraphQLWatchmanClient.js | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 16d55282e83ce..282f4dd3e6785 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -160,7 +160,7 @@ async function main(options: { ); } } - if (options.watch && !options.watchman) { + if (options.watch && !useWatchman) { throw new Error('Watchman is required to watch for changes.'); } if (options.watch && !hasWatchmanRootFile(srcDir)) { @@ -270,7 +270,7 @@ Ensure that one such file exists in ${srcDir} or its parents. // TODO: allow passing in a flag or detect? sourceControl: null, }); - if (!options.validate && !options.watch && options.watchman) { + if (!options.validate && !options.watch && useWatchman) { // eslint-disable-next-line no-console console.log('HINT: pass --watch to keep watching for changes.'); } diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 58d0f640ba208..9474065303d41 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -11,6 +11,7 @@ 'use strict'; const watchman = require('fb-watchman'); +const {execSync} = require('child_process'); const MAX_ATTEMPT_LIMIT = 5; @@ -23,6 +24,14 @@ class GraphQLWatchmanClient { _attemptLimit: number; static isAvailable(): Promise { + try { + const checkWatchman = execSync('command -v watchman'); + if (checkWatchman === '') { + return false; + } + } catch { + return false; + } return new Promise(resolve => { const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT); client.on('error', () => { From e2d5bb6a12095c16f683813165a5869a3ce0705d Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Sun, 10 Feb 2019 01:10:15 -0800 Subject: [PATCH 2/5] Happy flow --- packages/relay-compiler/bin/RelayCompilerMain.js | 2 +- .../relay-compiler/core/GraphQLWatchmanClient.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 282f4dd3e6785..8c8759709f2b8 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -160,7 +160,7 @@ async function main(options: { ); } } - if (options.watch && !useWatchman) { + if (options.watch && !options.watchman) { throw new Error('Watchman is required to watch for changes.'); } if (options.watch && !hasWatchmanRootFile(srcDir)) { diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 9474065303d41..5e41110545e5f 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -24,15 +24,15 @@ class GraphQLWatchmanClient { _attemptLimit: number; static isAvailable(): Promise { - try { - const checkWatchman = execSync('command -v watchman'); - if (checkWatchman === '') { - return false; - } - } catch { - return false; - } return new Promise(resolve => { + try { + const checkWatchman = execSync('command -v watchman'); + if (checkWatchman === '') { + return resolve(false); + } + } catch { + return resolve(false); + } const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT); client.on('error', () => { resolve(false); From 5aeb3b6332303ce6495e7868ddc5ac43b7eb06dd Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Sun, 10 Feb 2019 01:10:15 -0800 Subject: [PATCH 3/5] Happy flow --- packages/relay-compiler/bin/RelayCompilerMain.js | 2 +- .../relay-compiler/core/GraphQLWatchmanClient.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 282f4dd3e6785..8c8759709f2b8 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -160,7 +160,7 @@ async function main(options: { ); } } - if (options.watch && !useWatchman) { + if (options.watch && !options.watchman) { throw new Error('Watchman is required to watch for changes.'); } if (options.watch && !hasWatchmanRootFile(srcDir)) { diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 9474065303d41..7b026baed7502 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -24,15 +24,15 @@ class GraphQLWatchmanClient { _attemptLimit: number; static isAvailable(): Promise { - try { - const checkWatchman = execSync('command -v watchman'); - if (checkWatchman === '') { - return false; - } - } catch { - return false; - } return new Promise(resolve => { + try { + const checkWatchman = execSync('command -v watchman').toString().trim(); + if (checkWatchman === '') { + return resolve(false); + } + } catch { + return resolve(false); + } const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT); client.on('error', () => { resolve(false); From e6d7d4c0d48c9a1571750e5ded93340392b9a209 Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Fri, 22 Feb 2019 13:14:30 -0800 Subject: [PATCH 4/5] Change the implementatio for watchman check --- .../relay-compiler/bin/RelayCompilerMain.js | 1 - .../core/GraphQLWatchmanClient.js | 26 +++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 8c8759709f2b8..85775c210319b 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -185,7 +185,6 @@ Ensure that one such file exists in ${srcDir} or its parents. verbose: options.verbose, quiet: options.quiet, }); - const useWatchman = options.watchman && (await WatchmanClient.isAvailable()); const schema = getSchema(schemaPath); diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 7b026baed7502..b3e8c2fbdc78b 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -11,7 +11,7 @@ 'use strict'; const watchman = require('fb-watchman'); -const {execSync} = require('child_process'); +const childProcess = require('child_process'); const MAX_ATTEMPT_LIMIT = 5; @@ -19,18 +19,28 @@ function delay(delayMs: number): Promise { return new Promise(resolve => setTimeout(resolve, delayMs)); } +function hasWatchmanInstalled(): Promise { + return new Promise(resolve => { + const proc = childProcess.spawn('watchman', ['--version']); + proc.on('error', () => { + resolve(false); + }); + proc.on('close', code => { + if (code === 0) { + resolve(true); + } + }); + }); +} + class GraphQLWatchmanClient { _client: any; _attemptLimit: number; static isAvailable(): Promise { - return new Promise(resolve => { - try { - const checkWatchman = execSync('command -v watchman').toString().trim(); - if (checkWatchman === '') { - return resolve(false); - } - } catch { + return new Promise(async resolve => { + const hasWatchman = await hasWatchmanInstalled(); + if (!hasWatchman) { return resolve(false); } const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT); From 9cb7999f35686432196bfbc35a0ca0a320c73bea Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Fri, 22 Feb 2019 13:14:30 -0800 Subject: [PATCH 5/5] Change the implementation of watchman client availability --- .../relay-compiler/bin/RelayCompilerMain.js | 1 - .../core/GraphQLWatchmanClient.js | 26 +++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 8c8759709f2b8..85775c210319b 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -185,7 +185,6 @@ Ensure that one such file exists in ${srcDir} or its parents. verbose: options.verbose, quiet: options.quiet, }); - const useWatchman = options.watchman && (await WatchmanClient.isAvailable()); const schema = getSchema(schemaPath); diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 7b026baed7502..b3e8c2fbdc78b 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -11,7 +11,7 @@ 'use strict'; const watchman = require('fb-watchman'); -const {execSync} = require('child_process'); +const childProcess = require('child_process'); const MAX_ATTEMPT_LIMIT = 5; @@ -19,18 +19,28 @@ function delay(delayMs: number): Promise { return new Promise(resolve => setTimeout(resolve, delayMs)); } +function hasWatchmanInstalled(): Promise { + return new Promise(resolve => { + const proc = childProcess.spawn('watchman', ['--version']); + proc.on('error', () => { + resolve(false); + }); + proc.on('close', code => { + if (code === 0) { + resolve(true); + } + }); + }); +} + class GraphQLWatchmanClient { _client: any; _attemptLimit: number; static isAvailable(): Promise { - return new Promise(resolve => { - try { - const checkWatchman = execSync('command -v watchman').toString().trim(); - if (checkWatchman === '') { - return resolve(false); - } - } catch { + return new Promise(async resolve => { + const hasWatchman = await hasWatchmanInstalled(); + if (!hasWatchman) { return resolve(false); } const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT);