Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature for opening in default browser #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.2

+ Added `--open` flag to automatically open in browser.

## 1.0.3

+ Respects `index.html`
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions bin/serve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> arguments) async {
Expand All @@ -21,19 +22,23 @@ main(List<String> 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.',
valueHelp: '-s /home/myname/ssl',
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: ["./"]);

Expand Down Expand Up @@ -90,5 +95,12 @@ main(List<String> arguments) async {

bool log = parsed['log'];

if (parsed['open']) {
openInBrowser((parsed['https'] != null ? 'https://' : 'http://') +
parsed['host'] +
":" +
port.toString());
}

await serve(config, log: log);
}
25 changes: 25 additions & 0 deletions lib/open_in_browser.dart
Original file line number Diff line number Diff line change
@@ -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.");
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>

Expand Down