From b80c65c240b84e9cecf69b7f2b3692f5abe64f12 Mon Sep 17 00:00:00 2001 From: Kevin Choi Date: Wed, 28 Oct 2020 22:15:36 -0400 Subject: [PATCH 1/4] Add a translation from Future to Promise for NPM packages --- lib/src/npm.dart | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/src/npm.dart b/lib/src/npm.dart index 83670f1..584ffff 100644 --- a/lib/src/npm.dart +++ b/lib/src/npm.dart @@ -296,6 +296,29 @@ 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(""" +@JS() +class Promise { + external Promise(void executor(void resolve(T result), Function reject)); +} + +Promise _futureToPromise(Future future) { + return new Promise(allowInterop((resolve, reject) { + future.then(resolve, onError: reject); + })); +} + +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 +349,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)))); } }"""); From 926e09f1fea70391c72257a6283f386b6c60b5cb Mon Sep 17 00:00:00 2001 From: Kevin Choi Date: Fri, 30 Oct 2020 11:49:25 -0400 Subject: [PATCH 2/4] Replace @JS Promise with Promise from node_interop package --- lib/src/npm.dart | 10 +++------- pubspec.yaml | 1 + 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/src/npm.dart b/lib/src/npm.dart index 584ffff..6b20d44 100644 --- a/lib/src/npm.dart +++ b/lib/src/npm.dart @@ -283,6 +283,7 @@ 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';"); // Dart-import each executable library so we can JS-export their `main()` // methods and call them from individual files in the npm package. @@ -299,13 +300,8 @@ String get _wrapperLibrary { // Define a JS-interop Future to Promise translator so that we can export // a Promise-based API wrapper.writeln(""" -@JS() -class Promise { - external Promise(void executor(void resolve(T result), Function reject)); -} - -Promise _futureToPromise(Future future) { - return new Promise(allowInterop((resolve, reject) { +Promise _futureToPromise(Future future) { + return new Promise(allowInterop((resolve, reject) { future.then(resolve, onError: reject); })); } diff --git a/pubspec.yaml b/pubspec.yaml index 66f3b3a..214465b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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' From faaabad4ef3e51cc93099183c92af7a668cbda42 Mon Sep 17 00:00:00 2001 From: Kevin Choi Date: Wed, 4 Nov 2020 11:25:45 -0500 Subject: [PATCH 3/4] Use futureToPromise provided by node_interop/util --- lib/src/npm.dart | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/src/npm.dart b/lib/src/npm.dart index 6b20d44..ae33c4a 100644 --- a/lib/src/npm.dart +++ b/lib/src/npm.dart @@ -284,6 +284,7 @@ String get _wrapperLibrary { 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. @@ -300,15 +301,9 @@ String get _wrapperLibrary { // Define a JS-interop Future to Promise translator so that we can export // a Promise-based API wrapper.writeln(""" -Promise _futureToPromise(Future future) { - return new Promise(allowInterop((resolve, reject) { - future.then(resolve, onError: reject); - })); -} - Object _translateReturnValue(Object val) { if (val is Future) { - return _futureToPromise(val); + return futureToPromise(val); } else { return val; } From 5a0a3fb33d8237ccb0c3e9ae5bc5605cbc576ea9 Mon Sep 17 00:00:00 2001 From: Kevin Choi Date: Fri, 13 Nov 2020 17:46:14 -0500 Subject: [PATCH 4/4] Bump minor version to 1.1.0, add changelog --- CHANGELOG.md | 5 +++++ pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) 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/pubspec.yaml b/pubspec.yaml index b06bb70..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