From b806f13c1a022982d690dea3e59412b45bf336ed Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Tue, 9 Feb 2021 10:50:55 +0100 Subject: :hammer: Add postbuild-script --- package.json | 2 +- postbuild.sh | 33 +++++++++++++++++++++++++++++++++ src/service-worker.js | 10 ++-------- 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100755 postbuild.sh diff --git a/package.json b/package.json index 46844e7..81ba90a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "dev": "parcel ./src/index.html --https --public-url /hoverbit-ble/", "build": "parcel build ./src/index.html --public-url /hoverbit-ble/", - "postbuild": "cp src/service-worker.js dist/service-worker.js" + "postbuild": "./postbuild.sh" }, "dependencies": { "@fortawesome/fontawesome-free": "^5.15.2", diff --git a/postbuild.sh b/postbuild.sh new file mode 100755 index 0000000..c1f0ffc --- /dev/null +++ b/postbuild.sh @@ -0,0 +1,33 @@ +#!/bin/bash +PUBLISH_URL="/hoverbit-ble/" + +cd "dist" || exit 1 + +VERSION=$(cat ../package.json \ + | grep version \ + | head -1 \ + | awk -F: '{ print $2 }' \ + | sed 's/[",]//g' \ + | tr -d '[[:space:]]') + +tput setaf 4; echo "> Add service-worker.js..."; tput sgr0 +cp ../src/service-worker.js ./service-worker.js + +tput setaf 4; echo "> Add version number to files ($VERSION)..."; tput sgr0 +sed -i".bak" "s/{{ VERSION }}/$VERSION/g" index.html +sed -i".bak" "s/{{ VERSION }}/$VERSION/g" service-worker.js + +tput setaf 4; echo "> Add cache-files to webmanifest..."; tput sgr0 +cache_files="'$PUBLISH_URL'"; +for file in *; do + if [[ "$file" =~ (manifest.webmanifest|*.git) ]]; then + continue + fi + cache_files="$cache_files,""'$PUBLISH_URL$file'" +done +sed -i".bak" "s|\"{{ CACHE_FILES }}\"|$cache_files|g" service-worker.js + +tput setaf 4; echo "> Remove .bak-files..."; tput sgr0 +rm *.bak + +cd .. diff --git a/src/service-worker.js b/src/service-worker.js index bc3f42d..96accb3 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -1,13 +1,7 @@ var APP_PREFIX = 'hoverbitcontroller' // Identifier for this app (this needs to be consistent across every cache update) -var VERSION = 'version_01' // Version of the off-line cache (change this value everytime you want to update cache) +var VERSION = '{{ VERSION }}' // Version of the off-line cache (change this value everytime you want to update cache) var CACHE_NAME = APP_PREFIX + VERSION -var URLS = [ // Add URL you want to cache in this list. - '/hoverbit-ble/', // If you have separate JS/CSS files, - '/hoverbit-ble/index.html', // add path to those files here - '/hoverbit-ble/styles.css', - '/hoverbit-ble/microbit.umd.js', - '/hoverbit-ble/script.js' -] +var URLS = ["{{ CACHE_FILES }}"] // This will be replaced by the deploy-script // Respond with cached resources self.addEventListener('fetch', function (e) { -- cgit v1.2.3