diff --git a/pubspec.yaml b/pubspec.yaml index 7373127..fd12378 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,12 @@ homepage: https://github.com/Workiva/sockjs_client_wrapper environment: sdk: '>=2.4.0 <3.0.0' +dependency_overrides: + dart_dev: + git: + url: git@github.com:Workiva/dart_dev.git + ref: background_process_tool + dependencies: js: ^0.6.1 w_common: ^1.20.1 diff --git a/tool/dart_dev/config.dart b/tool/dart_dev/config.dart index 3f642bb..d27320d 100644 --- a/tool/dart_dev/config.dart +++ b/tool/dart_dev/config.dart @@ -12,68 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -import 'dart:async'; -import 'dart:io'; - import 'package:dart_dev/dart_dev.dart'; -final config = { +final Map config = { ...coreConfig, 'test': CompoundTool() - ..addTool(DevTool.fromFunction(startTestServer), alwaysRun: true) + ..addTool(_testServer.starter, alwaysRun: true) ..addTool(TestTool(), argMapper: takeAllArgs) - ..addTool(DevTool.fromFunction(stopTestServer), alwaysRun: true), + ..addTool(_testServer.stopper, alwaysRun: true), 'serve': CompoundTool() - ..addTool(DevTool.fromFunction(startExampleServer), alwaysRun: true) + ..addTool(_exampleServer.starter, alwaysRun: true) ..addTool(WebdevServeTool()..webdevArgs = ['example:8080']) - ..addTool(DevTool.fromFunction(stopExampleServer), alwaysRun: true), + ..addTool(_exampleServer.stopper, alwaysRun: true), }; -Process _exampleServer; -Process _testServer; - -Future startExampleServer(DevToolExecutionContext _) async { - _exampleServer = await Process.start('node', ['example/server.js'], - mode: ProcessStartMode.inheritStdio); - return firstOf([ - // Exit early if it fails to start, - _exampleServer.exitCode, - // otherwise just wait a little bit to ensure it starts up completely. - Future.delayed(Duration(seconds: 2)).then((_) => 0), - ]); -} - -Future stopExampleServer(DevToolExecutionContext _) async { - _exampleServer?.kill(); - await _exampleServer.exitCode; - return 0; -} - -Future startTestServer(DevToolExecutionContext _) async { - _testServer = await Process.start('node', ['tool/server.js'], - mode: ProcessStartMode.inheritStdio); - return firstOf([ - // Exit early if it fails to start, - _testServer.exitCode, - // otherwise just wait a little bit to ensure it starts up completely. - Future.delayed(Duration(seconds: 2)).then((_) => 0), - ]); -} - -Future stopTestServer(DevToolExecutionContext _) async { - _testServer?.kill(); - await _testServer.exitCode; - return 0; -} - -Future firstOf(Iterable> futures) { - final c = Completer(); - for (final future in futures) { - future.then((v) { - if (!c.isCompleted) { - c.complete(v); - } - }).catchError(c.completeError); - } - return c.future; -} +final _exampleServer = ProcessTool('node', ['example/server.js']) + .backgrounded(startAfterDelay: Duration(seconds: 2)); +final _testServer = ProcessTool('node', ['tool/server.js']) + .backgrounded(startAfterDelay: Duration(seconds: 2));