From f2516a4461aa4f0c679ff99759f9febf9e88f0a5 Mon Sep 17 00:00:00 2001 From: Michael Charles Aubrey Date: Thu, 30 Jul 2020 16:47:47 +0900 Subject: [PATCH] add feature for opening in default browser --- CHANGELOG.md | 4 ++++ README.md | 8 ++++++++ bin/serve.dart | 16 ++++++++++++++-- lib/open_in_browser.dart | 25 +++++++++++++++++++++++++ pubspec.yaml | 2 +- 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 lib/open_in_browser.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index fe2ef46..9edc723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.0.2 + ++ Added `--open` flag to automatically open in browser. + ## 1.0.3 + Respects `index.html` diff --git a/README.md b/README.md index 01c546d..d4aa53e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,14 @@ Use `--base-path` (abbreviated as `-b`) to serve contents with desired base path serve -b /myblog ``` +# Automatically open browser when served + +Use `--open` (abbreviated as `-o`) to automatically open your default browser at the location being served. + +```bash +serve -o +``` + # More features to come! + [ ] Support CORS diff --git a/bin/serve.dart b/bin/serve.dart index f393345..ef7719a 100644 --- a/bin/serve.dart +++ b/bin/serve.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:jaguar/jaguar.dart'; import 'package:path/path.dart' as p; +import 'package:serve/open_in_browser.dart'; import 'package:serve/serve.dart'; main(List arguments) async { @@ -21,6 +22,10 @@ main(List arguments) async { abbr: 'l', help: 'If set, all requests will be logged to the stdout.', defaultsTo: true); + args.addFlag('open', + abbr: 'o', + help: 'If set, opens default browser to the location being served.', + defaultsTo: false); args.addOption('https', abbr: 's', help: 'Directory where certificate.pem and keys.pem are stored.', @@ -28,12 +33,12 @@ main(List arguments) async { defaultsTo: null); args.addMultiOption('base', abbr: 'b', - help: 'Base path to serve the contents at', + help: 'Base path to serve the contents at.', valueHelp: '-b /v1/app', defaultsTo: ["/"]); args.addMultiOption('dir', abbr: 'd', - help: 'Contents of the directory to serve', + help: 'Contents of the directory to serve.', valueHelp: '-h /var/local/www/', defaultsTo: ["./"]); @@ -90,5 +95,12 @@ main(List arguments) async { bool log = parsed['log']; + if (parsed['open']) { + openInBrowser((parsed['https'] != null ? 'https://' : 'http://') + + parsed['host'] + + ":" + + port.toString()); + } + await serve(config, log: log); } diff --git a/lib/open_in_browser.dart b/lib/open_in_browser.dart new file mode 100644 index 0000000..dd5811c --- /dev/null +++ b/lib/open_in_browser.dart @@ -0,0 +1,25 @@ +import "dart:io"; + +void openInBrowser(String url) { + bool fail = false; + switch (Platform.operatingSystem) { + case "linux": + Process.run("xdg-open", [url]); + break; + case "macos": + Process.run("open", [url]); + break; + case "windows": + Process.run("explorer", [url]); + break; + default: + fail = true; + break; + } + + if (!fail) { + print("Opening default browser to $url"); + } else { + print("Failed to open default browser."); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 6359f3b..d6c5924 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: serve description: CLI to serves static files in a directory. -version: 2.0.1 +version: 2.0.2 homepage: https://github.com/Jaguar-dart/serve author: Ravi Teja Gudapati