From 6a3725ebdb219f1303b44be56e3a812dd350e285 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Wed, 7 Dec 2022 11:43:32 +0100
Subject: [PATCH] WIP watching subdirectoires

---
 .dockerignore        |  1 +
 Dockerfile           |  2 +-
 Dockerfile.devserver |  2 +-
 build_deps.sh        | 60 ++++++++++++++++++++++++++++----------------
 package.json         | 14 +++++++++--
 5 files changed, 54 insertions(+), 25 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index ef69cb3..36b421b 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,3 +1,4 @@
 node_modules/
 ./*/node_modules/
 annotot-db/
+.cache
diff --git a/Dockerfile b/Dockerfile
index ba72fd7..1e184c1 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,5 @@
 FROM debian:stable as builder
-RUN apt-get update && apt-get install -y npm git
+RUN apt-get update && apt-get install -y npm git jq
 
 COPY . /opt
 WORKDIR /opt
diff --git a/Dockerfile.devserver b/Dockerfile.devserver
index a17d738..7d88b4b 100644
--- a/Dockerfile.devserver
+++ b/Dockerfile.devserver
@@ -1,5 +1,5 @@
 FROM debian:stable-slim
-RUN apt-get update && apt-get install -y git npm
+RUN apt-get update && apt-get install -y git npm jq inotify-tools
 RUN useradd --home-dir /opt node
 RUN chown node:node /opt
 EXPOSE 9000
diff --git a/build_deps.sh b/build_deps.sh
index 85429dc..de4f6ad 100755
--- a/build_deps.sh
+++ b/build_deps.sh
@@ -1,25 +1,43 @@
 #!/bin/bash
-declare -A SUBMODULES=(
-    ['mirador-video-annotation']="build:es"
-    ['annotations-plugin']="build"
-)
-
-run_on_modules(){
-    for mod in "${!SUBMODULES[@]}"; do
-        echo -e "\t$mod"
-        cd $mod
-        if [ "$1" == "install" ]; then
-            npm install
-        else
-            npm run ${SUBMODULES[${mod}]}
-        fi
-        cd ..
-    done
+## Runs action $2 for submodule $1
+watch() {
+inotifywait \
+    --event modify \
+    --event create \
+    --event delete \
+    --event move \
+    --monitor \
+    --quiet \
+    --recursive \
+    --format '%e %w%f' \
+    ./src | while read line; do
+                echo "$line"
+                $@
+            done
+}
+run() {
+    mod=$1
+    shift
+    echo "Running '$@' on '$mod'"
+    cd $mod
+    $@
+    if $watch; then
+        watch $@ &
+    fi
+    cd ..
 }
 
-if [ "$1" == "install" ]; then
-    echo "Installing submodules"
-    run_on_modules install
+action=$1
+watch=false
+if [ "$action" == "watch" ]; then
+    action=build
+    watch=true
 fi
-echo "Compiling submodules"
-run_on_modules build
+
+echo "Submodule action '$action' triggered"
+jq -r --arg action "$action" \
+    '.subscripts | to_entries[] | "\(.key) \(.value | .[$action])"' \
+    package.json | \
+    while read line; do
+        run $line
+done
diff --git a/package.json b/package.json
index b8288f7..ceef3eb 100644
--- a/package.json
+++ b/package.json
@@ -4,10 +4,20 @@
   "description": "",
   "private": true,
   "scripts": {
-    "preinstall": "./build_deps.sh install",
     "prebuild": "./build_deps.sh build",
     "build": "webpack --config webpack.config.js",
-    "serve": "npm install && webpack serve --config webpack.config.js"
+    "preserve": "./build_deps.sh watch",
+    "serve": "webpack serve --config webpack.config.js"
+  },
+  "subscripts": {
+      "annotations-plugin": {
+          "install": "npm install",
+          "build": "npm run build"
+      },
+      "mirador-video-annotation": {
+          "install": "npm install",
+          "build": "npm run build:es"
+      }
   },
   "author": "",
   "license": "ISC",
-- 
GitLab