From 12b95f0e7673bb15d8a5ab29e275bd3b73de7f1e Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Fri, 9 Feb 2018 15:20:34 +0000
Subject: [PATCH] Try building peertube locally with a recent node version

---
 src/freedombone-app-peertube | 46 +++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/freedombone-app-peertube b/src/freedombone-app-peertube
index 9c027750..b0043010 100755
--- a/src/freedombone-app-peertube
+++ b/src/freedombone-app-peertube
@@ -200,7 +200,9 @@ function upgrade_peertube {
     function_check set_repo_commit
     set_repo_commit $PEERTUBE_DIR "peertube commit" "$PEERTUBE_COMMIT" $PEERTUBE_REPO
 
-    npm run upgrade-peertube
+    alias npm-exec='PATH=$(npm bin):$PATH'
+    npm-exec npm run upgrade-peertube
+
     chown -R peertube:peertube $PEERTUBE_DIR
     systemctl start peertube
 }
@@ -770,23 +772,39 @@ function install_peertube {
     git checkout $PEERTUBE_COMMIT -b $PEERTUBE_COMMIT
     set_completion_param "peertube commit" "$PEERTUBE_COMMIT"
 
-    npm install -g yarn
+    npm install npm@5.6.0
+    if [ ! "$?" = "0" ]; then
+        echo $'Failed to install local npm version 5.6.0'
+        exit 45465774
+    fi
+    alias npm-exec='PATH=$(npm bin):$PATH'
+    npm-exec install node@9.5.0
+    if [ ! "$?" = "0" ]; then
+        echo $'Failed to install local nodejs version 9.5.0'
+        exit 54875459
+    fi
+    npm install npm@5.6.0
+    if [ ! "$?" = "0" ]; then
+        echo $'Failed to install local npm version 5.6.0'
+        exit 678356845684
+    fi
+    npm install yarn@1.3.2
     if [ ! "$?" = "0" ]; then
         echo $'Failed to install yarn'
-        exit 79353234
+        exit 578947946
     fi
-    yarn add -D webpack --network-concurrency 1
-    yarn install
-    if [ ! "$?" = "0" ]; then
-        echo $'Failed to run yarn install'
-        exit 63754235
-    fi
-    npm install
+    npm-exec yarn add -D webpack --network-concurrency 1
+    #npm-exec yarn install --non-interactive
+    #if [ ! "$?" = "0" ]; then
+    #    echo $'Failed to run yarn install'
+    #    exit 63754235
+    #fi
+    npm-exec npm install
     if [ ! "$?" = "0" ]; then
         echo $'Failed to install peertube'
         exit 7835243
     fi
-    npm run build
+    npm-exec npm run build
     if [ ! "$?" = "0" ]; then
         echo $'Failed to build peertube'
         exit 5293593
@@ -803,13 +821,13 @@ function install_peertube {
     echo 'User=peertube' >> /etc/systemd/system/peertube.service
     echo 'Group=peertube' >> /etc/systemd/system/peertube.service
     echo "WorkingDirectory=$PEERTUBE_DIR" >> /etc/systemd/system/peertube.service
-    echo "ExecStart=/usr/local/bin/npm start" >> /etc/systemd/system/peertube.service
-    echo "ExecStop=/usr/local/bin/npm stop" >> /etc/systemd/system/peertube.service
+    echo "ExecStart=$PEERTUBE_DIR/node_modules/.bin/npm start" >> /etc/systemd/system/peertube.service
+    echo "ExecStop=$PEERTUBE_DIR/node_modules/.bin/npm stop" >> /etc/systemd/system/peertube.service
     echo 'StandardOutput=syslog' >> /etc/systemd/system/peertube.service
     echo 'StandardError=syslog' >> /etc/systemd/system/peertube.service
     echo 'SyslogIdentifier=peertube' >> /etc/systemd/system/peertube.service
     echo 'Restart=always' >> /etc/systemd/system/peertube.service
-    echo "Environment=NODE_ENV=production" >> /etc/systemd/system/peertube.service
+    echo "Environment=NODE_ENV=production PATH=\$(npm bin):\$PATH" >> /etc/systemd/system/peertube.service
     echo '' >> /etc/systemd/system/peertube.service
     echo '[Install]' >> /etc/systemd/system/peertube.service
     echo 'WantedBy=multi-user.target' >> /etc/systemd/system/peertube.service