diff options
-rw-r--r-- | docs/cli_commands.md | 2 | ||||
-rw-r--r-- | lib/python/qmk/cli/docs.py | 32 |
2 files changed, 25 insertions, 9 deletions
diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 1427b592f..9113e3b02 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md | |||
@@ -352,6 +352,8 @@ Now open your dev environment and live a squiggly-free life. | |||
352 | This command starts a local HTTP server which you can use for browsing or improving the docs. Default port is 8936. | 352 | This command starts a local HTTP server which you can use for browsing or improving the docs. Default port is 8936. |
353 | Use the `-b`/`--browser` flag to automatically open the local webserver in your default browser. | 353 | Use the `-b`/`--browser` flag to automatically open the local webserver in your default browser. |
354 | 354 | ||
355 | This command runs `docsify serve` if `docsify-cli` is installed (which provides live reload), otherwise Python's builtin HTTP server module will be used. | ||
356 | |||
355 | **Usage**: | 357 | **Usage**: |
356 | 358 | ||
357 | ``` | 359 | ``` |
diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py index d8f9b045a..c24b914bc 100644 --- a/lib/python/qmk/cli/docs.py +++ b/lib/python/qmk/cli/docs.py | |||
@@ -2,6 +2,7 @@ | |||
2 | """ | 2 | """ |
3 | import http.server | 3 | import http.server |
4 | import os | 4 | import os |
5 | import shutil | ||
5 | import webbrowser | 6 | import webbrowser |
6 | 7 | ||
7 | from milc import cli | 8 | from milc import cli |
@@ -11,20 +12,33 @@ from milc import cli | |||
11 | @cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.') | 12 | @cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.') |
12 | @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) | 13 | @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) |
13 | def docs(cli): | 14 | def docs(cli): |
14 | """Spin up a local HTTPServer instance for the QMK docs. | 15 | """Spin up a local HTTP server for the QMK docs. |
15 | """ | 16 | """ |
16 | os.chdir('docs') | 17 | os.chdir('docs') |
17 | 18 | ||
18 | with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd: | 19 | # If docsify-cli is installed, run that instead so we get live reload |
19 | cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/") | 20 | if shutil.which('docsify'): |
20 | cli.log.info("Press Control+C to exit.") | 21 | command = ['docsify', 'serve', '--port', f'{cli.config.docs.port}', '--open' if cli.config.docs.browser else ''] |
21 | 22 | ||
22 | if cli.config.docs.browser: | 23 | cli.log.info(f"Running {{fg_cyan}}{str.join(' ', command)}{{fg_reset}}") |
23 | webbrowser.open(f'http://localhost:{cli.config.docs.port}') | 24 | cli.log.info("Press Control+C to exit.") |
24 | 25 | ||
25 | try: | 26 | try: |
26 | httpd.serve_forever() | 27 | cli.run(command, capture_output=False) |
27 | except KeyboardInterrupt: | 28 | except KeyboardInterrupt: |
28 | cli.log.info("Stopping HTTP server...") | 29 | cli.log.info("Stopping HTTP server...") |
29 | finally: | 30 | else: |
30 | httpd.shutdown() | 31 | # Fall back to Python HTTPServer |
32 | with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd: | ||
33 | cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/") | ||
34 | cli.log.info("Press Control+C to exit.") | ||
35 | |||
36 | if cli.config.docs.browser: | ||
37 | webbrowser.open(f'http://localhost:{cli.config.docs.port}') | ||
38 | |||
39 | try: | ||
40 | httpd.serve_forever() | ||
41 | except KeyboardInterrupt: | ||
42 | cli.log.info("Stopping HTTP server...") | ||
43 | finally: | ||
44 | httpd.shutdown() | ||