diff --git a/CHANGELOG.md b/CHANGELOG.md index a13068e..5304f2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.1.0 + +* The main function generated by this package will return a `Promise` if the + corresponding Dart main function returns a `Future`. + # 1.0.0 * Initial stable release. diff --git a/lib/src/npm.dart b/lib/src/npm.dart index 83670f1..ae33c4a 100644 --- a/lib/src/npm.dart +++ b/lib/src/npm.dart @@ -283,6 +283,8 @@ String get _wrapperLibrary { var wrapper = StringBuffer(); wrapper.writeln("import 'dart:typed_data';"); wrapper.writeln("import 'package:js/js.dart';"); + wrapper.writeln("import 'package:node_interop/node_interop.dart';"); + wrapper.writeln("import 'package:node_interop/util.dart';"); // Dart-import each executable library so we can JS-export their `main()` // methods and call them from individual files in the npm package. @@ -296,6 +298,18 @@ String get _wrapperLibrary { wrapper.writeln("import $target as module_main;"); } + // Define a JS-interop Future to Promise translator so that we can export + // a Promise-based API + wrapper.writeln(""" +Object _translateReturnValue(Object val) { + if (val is Future) { + return futureToPromise(val); + } else { + return val; + } +} +"""); + // Define a JS-interop "exports" field that we can use to export the various // main methods. wrapper.writeln(""" @@ -326,10 +340,11 @@ class _Exports {"""); wrapper.writeln(""" Function _wrapMain(Function main) { if (main is Object Function()) { - return allowInterop((_) => main()); + return allowInterop((_) => _translateReturnValue(main())); } else { return allowInterop( - (args) => main(List.from(args as List))); + (args) => _translateReturnValue( + main(List.from(args as List)))); } }"""); diff --git a/pubspec.yaml b/pubspec.yaml index 66d28e4..17f487d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cli_pkg -version: 1.0.0 +version: 1.1.0 description: Grinder tasks for releasing Dart CLI packages. homepage: https://github.com/google/dart_cli_pkg @@ -17,6 +17,7 @@ dependencies: js: "^0.6.0" meta: "^1.1.7" mustache: "^1.0.0" + node_interop: "^1.1.0" node_preamble: "^1.1.0" package_config: '^1.9.0' path: '^1.0.0'