From 06f045e90bb57c36738e58ee6830e2a2391bc6a3 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 16 Sep 2023 18:28:16 +0200 Subject: Migrate CLI to python --- .gitignore | 3 +- README.md | 14 +- bin/dev-server | 11 +- bin/get-books | 24 -- bin/view | 16 -- flake.nix | 13 +- public/index.html | 7 - public/main.css | 235 ------------------ src/book.ts | 15 -- src/lib/functions.ts | 7 - src/lib/i18n.ts | 14 -- src/lib/rx.ts | 404 ------------------------------- src/lib/search.ts | 17 -- src/library/__init__.py | 0 src/library/command.py | 21 ++ src/main.py | 56 +++++ src/main.ts | 46 ---- src/view/__init__.py | 0 src/view/books.ts | 115 --------- src/view/client/book.ts | 15 ++ src/view/client/lib/functions.ts | 7 + src/view/client/lib/i18n.ts | 14 ++ src/view/client/lib/rx.ts | 404 +++++++++++++++++++++++++++++++ src/view/client/lib/search.ts | 17 ++ src/view/client/main.ts | 46 ++++ src/view/client/view/books.ts | 116 +++++++++ src/view/client/view/components/modal.ts | 38 +++ src/view/client/view/filters.ts | 90 +++++++ src/view/command.py | 17 ++ src/view/components/modal.ts | 38 --- src/view/filters.ts | 90 ------- src/view/public/index.html | 7 + src/view/public/main.css | 235 ++++++++++++++++++ src/view/tsconfig.json | 13 + tsconfig.json | 13 - 35 files changed, 1119 insertions(+), 1059 deletions(-) delete mode 100755 bin/get-books delete mode 100755 bin/view delete mode 100644 public/index.html delete mode 100644 public/main.css delete mode 100644 src/book.ts delete mode 100644 src/lib/functions.ts delete mode 100644 src/lib/i18n.ts delete mode 100644 src/lib/rx.ts delete mode 100644 src/lib/search.ts create mode 100644 src/library/__init__.py create mode 100644 src/library/command.py create mode 100644 src/main.py delete mode 100644 src/main.ts create mode 100644 src/view/__init__.py delete mode 100644 src/view/books.ts create mode 100644 src/view/client/book.ts create mode 100644 src/view/client/lib/functions.ts create mode 100644 src/view/client/lib/i18n.ts create mode 100644 src/view/client/lib/rx.ts create mode 100644 src/view/client/lib/search.ts create mode 100644 src/view/client/main.ts create mode 100644 src/view/client/view/books.ts create mode 100644 src/view/client/view/components/modal.ts create mode 100644 src/view/client/view/filters.ts create mode 100644 src/view/command.py delete mode 100644 src/view/components/modal.ts delete mode 100644 src/view/filters.ts create mode 100644 src/view/public/index.html create mode 100644 src/view/public/main.css create mode 100644 src/view/tsconfig.json delete mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 6bb7df2..f7a3017 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -public/*.js +__pycache__ +src/view/public/*.js diff --git a/README.md b/README.md index 9c530de..d9ec3fd 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,12 @@ read = "Read" Each `metadata.toml` file correspond to a book, and there **must** be a cover named `cover.ext` in the same directory. Any extension works. -## Show library - -View the book library by running: - - ./bin/view browser-cmd path-to-books - ## Dev server -Enter nix shell: +In nix shell (`nix develop`), run: - nix develop + ./bin/dev-server path-to-books -Then run the dev-server: +## Show library - ./bin/dev-server path-to-books + BOOK_LIBRARY=path-to-books python src/main.py library diff --git a/bin/dev-server b/bin/dev-server index 2d1bf99..c98fdc5 100755 --- a/bin/dev-server +++ b/bin/dev-server @@ -3,7 +3,7 @@ set -euo pipefail cd $(dirname "$0")/.. if [ "$#" == 1 ]; then - BOOK_DIR="$1" + BOOK_LIBRARY="$1" else echo "usage: $0 path-to-book-directory" exit 1 @@ -11,17 +11,18 @@ fi # Watch books -BUILD_BOOKS_CMD="./bin/get-books $BOOK_DIR > public/books.js && echo public/books.js updated." +BUILD_BOOKS_CMD="echo \"const bookLibrary=\" > src/view/public/books.js && python src/main.py library >> src/view/public/books.js && echo src/view/public/books.js updated." watchexec \ - --watch "$BOOK_DIR" \ + --watch "$BOOK_LIBRARY" \ -- "$BUILD_BOOKS_CMD" & # Watch TypeScript +cd src/view CHECK="echo -e 'Checking TypeScript…\n' && tsc --checkJs" -BUILD="esbuild --bundle src/main.ts --target=es2017 --outdir=public" +BUILD="esbuild --bundle main.ts --target=es2017 --outdir=public" SHOW="echo -e '\nOpen $PWD/public/index.html'" watchexec \ --clear \ - --watch src \ + --watch client \ -- "$CHECK && $BUILD && $SHOW" diff --git a/bin/get-books b/bin/get-books deleted file mode 100755 index 1c9bf67..0000000 --- a/bin/get-books +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -cd $(dirname "$0")/.. - -if [ "$#" == 1 ]; then - BOOK_DIR="$1" -else - echo "usage: $0 path-to-book-directory" - exit 1 -fi - -echo "const bookLibrary = [" - -for METADATA in $(find "$BOOK_DIR" -name 'metadata.toml'); do - DIR=$(dirname "$METADATA") - COVER=$(ls $DIR/cover.*) - - TOML=$(cat "$METADATA") - WITH_COVER=$(echo -e "$TOML\ncover = \"$COVER\"") - echo "$WITH_COVER" | toml2json - echo "," -done - -echo "]" diff --git a/bin/view b/bin/view deleted file mode 100755 index 4fd4efc..0000000 --- a/bin/view +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -cd $(dirname "$0")/.. - -if [ "$#" == 2 ]; then - BROWSER="$1" - BOOK_DIR="$2" -else - echo "usage: $0 browser-cmd path-to-book-directory" - exit 1 -fi - -TMP_DIR=$(mktemp --directory) -cp public/* "$TMP_DIR" -bin/get-books "$BOOK_DIR" > "$TMP_DIR/books.js" -eval "$BROWSER $TMP_DIR/index.html" diff --git a/flake.nix b/flake.nix index 0f50d16..10c1430 100644 --- a/flake.nix +++ b/flake.nix @@ -8,15 +8,24 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; + + ebook-convert = with pkgs; writeShellScriptBin "ebook-convert" '' + set -euo pipefail + ${calibre}/bin/ebook-convert "$@" + ''; in with pkgs; { devShell = mkShell { buildInputs = [ esbuild nodePackages.typescript psmisc # fuser - python3 - toml2json watchexec + + # CLI + python311 + python311Packages.requests + python311Packages.pillow + ebook-convert ]; }; } diff --git a/public/index.html b/public/index.html deleted file mode 100644 index ce4d568..0000000 --- a/public/index.html +++ /dev/null @@ -1,7 +0,0 @@ - - - -