Skip to content

Commit

Permalink
--generate-dir option, closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 9, 2020
1 parent 32c8685 commit e376a4e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The `--project` argument is required - it specifies the project name that should
* `--debug` enables the Now CLI debug output
* `--token` allows you to pass a Now authentication token, rather than needing to first run `now login` to configure the tool
* `--public` runs `now --public` to publish the application source code at `/_src` - e.g. https://datasette-public.now.sh/_src
* `--generate-dir` - by default this tool generates a new Now app in a temporary directory, deploys it and then deletes the directory. Use `--generate-dir=my-app` to output the generated application files to a new directory of your choice instead. You can then deploy it by running `now` in that directory.

### Full help

Expand Down Expand Up @@ -70,5 +71,6 @@ Options:
--no-prod Don't deploy directly to production
--debug Enable Now CLI debug output
--public Publish source with Now CLI --public
--generate-dir DIRECTORY Output generated application files here
--help Show this message and exit.
```
35 changes: 27 additions & 8 deletions datasette_publish_now/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import os
import re
import shutil

INDEX_PY = """
from datasette.app import Datasette
Expand Down Expand Up @@ -58,6 +59,11 @@ def publish_subcommand(publish):
@click.option(
"--public", is_flag=True, help="Publish source with Now CLI --public",
)
@click.option(
"--generate-dir",
type=click.Path(dir_okay=True, file_okay=False),
help="Output generated application files here",
)
def now2(
files,
metadata,
Expand All @@ -81,6 +87,7 @@ def now2(
no_prod,
debug,
public,
generate_dir,
):
fail_if_publish_binary_not_installed(
"now", "Zeit Now", "https://zeit.co/download"
Expand Down Expand Up @@ -143,11 +150,23 @@ def now2(
open("requirements.txt", "w").write(
"\n".join([datasette_install] + list(install))
)
cmd = ["now", "--confirm", "--no-clipboard"]
if debug:
cmd.append("--debug")
if not no_prod:
cmd.append("--prod")
if public:
cmd.append("--public")
run(cmd)
if generate_dir:
# Copy these to the specified directory
shutil.copytree(".", generate_dir)
click.echo(
"Your generated application files have been written to:", err=True
)
click.echo(" {}\n".format(generate_dir), err=True)
click.echo("To deploy using Zeit Now, run the following:")
click.echo(" cd {}".format(generate_dir), err=True)
click.echo(" now --prod".format(generate_dir), err=True)
else:
# Run the deploy with now
cmd = ["now", "--confirm", "--no-clipboard"]
if debug:
cmd.append("--debug")
if not no_prod:
cmd.append("--prod")
if public:
cmd.append("--public")
run(cmd)
29 changes: 29 additions & 0 deletions tests/test_publish_now.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from click.testing import CliRunner
from datasette import cli
from unittest import mock
import os
import pathlib
import re
import subprocess
Expand Down Expand Up @@ -64,6 +65,34 @@ def test_publish_now_public(mock_run, mock_which):
)


@mock.patch("shutil.which")
@mock.patch("datasette_publish_now.run")
def test_publish_now_generate(mock_run, mock_which, tmpdir):
mock_which.return_value = True
mock_run.return_value = mock.Mock(0)
runner = CliRunner()
with runner.isolated_filesystem():
open("test.db", "w").write("data")
result = runner.invoke(
cli.cli,
[
"publish",
"now2",
"test.db",
"--project",
"foo",
"--public",
"--generate-dir",
tmpdir / "out",
],
)
assert result.exit_code == 0
assert not mock_run.called
# Test that the correct files were generated
filenames = set(os.listdir(tmpdir / "out"))
assert {"requirements.txt", "index.py", "now.json", "test.db"} == filenames


def test_help_in_readme(request):
# Ensure the --help output embedded in the README is up-to-date
readme_path = pathlib.Path(__file__).parent.parent / "README.md"
Expand Down

0 comments on commit e376a4e

Please sign in to comment.