Skip to content

Commit

Permalink
Merge pull request #4 from jacebrowning/launch-action
Browse files Browse the repository at this point in the history
Add a command to launch the site's index
  • Loading branch information
jacebrowning committed Jun 10, 2016
2 parents 6e91f52 + ad93c33 commit 3e6b751
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
16 changes: 14 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Custom Directory
# Options

## Root Directory

The server will attempt to run in one of the following directories:

- `./dist`
- `./site`
- `./_site`
- `./public`
- `./`

Expand All @@ -12,10 +16,18 @@ To serve from a custom location:
$ sappy my/build/folder
```

# Custom Port
## Custom Port

The default port is 8080. To use a custom port:

```
$ sappy --port 4200
```

# Actions

To open the site's index automatically:

```
$ sappy --launch
```
4 changes: 2 additions & 2 deletions sappy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Entry point to run the package directly."""

from . import server
from . import cli


if __name__ == '__main__':
server.main()
cli.main()
25 changes: 25 additions & 0 deletions sappy/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Command-line interface."""

import click

from . import __version__
from . import server

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

ReadableDirectory = click.Path(exists=True, readable=True, file_okay=False)


@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument('root', type=ReadableDirectory, required=False)
@click.option('-p', '--port', type=int, help="Port for the web server.")
@click.option('-l', '--launch', is_flag=True, help="Launch the page's index.")
@click.version_option(version=__version__)
def main(root=None, port=None, launch=False):
"""Serve a single-page application from a directory."""
path, httpd = server.init(root, port)

click.echo("Serving {d}/ on {p}".format(d=path, p=httpd.server_port))
if launch:
click.launch("http://localhost:{p}".format(p=httpd.server_port))
httpd.serve_forever()
18 changes: 0 additions & 18 deletions sappy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@
from pathlib import Path
from http.server import HTTPServer

import click

from . import __version__, settings
from .handlers import SinglePageApplicationHandler

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

ReadableDirectory = click.Path(exists=True, readable=True, file_okay=False)


@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument('root', type=ReadableDirectory, required=False)
@click.option('-p', '--port', type=int)
@click.version_option(version=__version__)
def main(root=None, port=None):
"""Serve a single-page application from a directory."""
path, httpd = init(root, port)

click.echo("Serving {d}/ on {p}".format(d=path, p=httpd.server_port))
httpd.serve_forever()


def init(root, port):
"""Create a new HTTP daemon to run."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

packages=setuptools.find_packages(),

entry_points={'console_scripts': ['sappy = sappy.server:main']},
entry_points={'console_scripts': ['sappy = sappy.cli:main']},

long_description=LONG_DESCRIPTION,
license='MIT',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from click.testing import CliRunner
import requests

from sappy import server
from sappy.cli import main


def cli(*options):
Expand Down Expand Up @@ -64,6 +64,6 @@ def with_custom_port(temp_with_dist):

def with_an_invalid_directory(temp):
runner = CliRunner()
result = runner.invoke(server.main, ['unknown/directory'])
result = runner.invoke(main, ['unknown/directory'])

expect(result.exit_code) == 2

0 comments on commit 3e6b751

Please sign in to comment.