From e75a828fce5f6cb243ce93c98a60f2ca44bebecf Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Thu, 20 Jan 2022 20:22:58 +0100 Subject: [PATCH] always use shell script to generate apps.json, remove js version Gets rid of duplicated functionality, but allows passing a filename argument to write to a different file instead of `apps.json`. --- apps.json | 5 ++- bin/create_apps_json.sh | 17 +++++++--- bin/update_local_apps_json.js | 62 ----------------------------------- package.json | 2 +- 4 files changed, 18 insertions(+), 68 deletions(-) delete mode 100755 bin/update_local_apps_json.js diff --git a/apps.json b/apps.json index 822af47f29..537a4f697c 100644 --- a/apps.json +++ b/apps.json @@ -7,7 +7,10 @@ # Otherwise nothing has changed. GitHub Pages will automatically # create apps.json as your site is hosted, or if you're hosting # yourself you can run bin/create_apps_json.sh -# +# +# If you serve the store from localhost for development/testing, +# the loader looks for apps.local.json instead, you can run +# `bin/create_apps_json.sh apps.local.json` to create that file. # ================================================================= # Uncomment the following line if you only want explicitly listed diff --git a/bin/create_apps_json.sh b/bin/create_apps_json.sh index adc5f8a62c..d61f7afe18 100755 --- a/bin/create_apps_json.sh +++ b/bin/create_apps_json.sh @@ -13,17 +13,26 @@ # # If you do this, please do not attempt to commit your modified # apps.json back into the main BangleApps repository! +# +# You can pass an optional filename to this script, and it will write +# to that instead, apps.local.json is used when opening the loader on localhost +outfile="${1:-apps.json}" cd `dirname $0`/.. -echo "[" > apps.json +echo "[" > "$outfile" +first=1 for app in apps/*/; do echo "Processing $app..."; if [[ "$app" =~ ^apps/_example.* ]]; then echo "Ignoring $app" else - cat ${app}metadata.json >> apps.json + if [ $first -eq 1 ]; then + first=0; + else + echo "," >> "$outfile" + fi; + cat ${app}metadata.json >> "$outfile" # echo ",\"$app\"," >> apps.json # DEBUG ONLY - echo "," >> apps.json fi done -echo "null]" >> apps.json +echo "]" >> "$outfile" diff --git a/bin/update_local_apps_json.js b/bin/update_local_apps_json.js deleted file mode 100755 index 3ba54f289d..0000000000 --- a/bin/update_local_apps_json.js +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/nodejs -/* Merge all apps/metadata.json files into apps.local.json -*/ - -const fs = require("fs"); - -const BASEDIR = __dirname+"/../"; -const APPSDIR = BASEDIR+"apps/"; -const APPSFILE = "apps.local.json"; -const APPSPATH = BASEDIR+ APPSFILE; - -function ERROR(s) { - console.error("ERROR: "+s); - process.exit(1); -} -function INFO(s) { - console.info(s); -} - -const apps = []; -const dirs = fs.readdirSync(APPSDIR, {withFileTypes: true}); -dirs.forEach(dir => { - let appsFile; - if (dir.name.startsWith("_example")) { - return; - } - try { - appsFile = fs.readFileSync(APPSDIR+dir.name+"/metadata.json").toString(); - } catch(e) { - return; - } - try { - apps.push(JSON.parse(appsFile)); - } catch(e) { - console.log(e); - const m = e.toString().match(/in JSON at position (\d+)/); - if (m) { - const char = parseInt(m[1]); - console.log("==============================================="); - console.log("LINE "+appsFile.substr(0, char).split("\n").length); - console.log("==============================================="); - console.log(appsFile.substr(char-10, 20)); - console.log("==============================================="); - } - console.log(m); - ERROR(dir.name+"/metadata.json not valid JSON"); - } -}); -// order doesn't matter as the loader sorts apps, but sort by anyway -apps.sort((a, b) => ((0|a.sortorder)-(0|b.sortorder)) || a.id.localeCompare(b.id)); -const json = JSON.stringify(apps, null, 2); -let update = false; -if (fs.existsSync(APPSPATH)) { - const old = fs.readFileSync(APPSPATH).toString(); - if (old===json) { - INFO(`${APPSFILE} is already up-to-date`); - process.exit(); - } - update = true; -} -fs.writeFileSync(APPSPATH, json); -INFO(`${update ? 'Updated' : 'Wrote'} ${APPSFILE}`); \ No newline at end of file diff --git a/package.json b/package.json index aa1b0b88a5..32c96e3eae 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "lint-apps": "eslint ./apps --ext .js", "test": "node bin/sanitycheck.js && eslint ./apps --ext .js", - "update-local-apps": "node bin/update_local_apps_json.js", + "update-local-apps": "./bin/create_apps_json.sh apps.local.json", "local": "npm-watch & npx http-server -a localhost -c-1", "start": "npx http-server -c-1" },