From 80df74f8239e6f3bafb69908ff511e997b7d57c2 Mon Sep 17 00:00:00 2001
From: Bhabesh <bhabesh.mukhopadhyay@nibio.no>
Date: Sun, 20 Jun 2021 23:11:37 +0200
Subject: [PATCH] Create hook to get app version
Get app version from <widget> at config.xml of cordova
---
config.xml | 2 ++
hooks/versionHook.js | 63 ++++++++++++++++++++++++++++++++++++++++++++
index.html | 1 +
package.json | 3 ++-
4 files changed, 68 insertions(+), 1 deletion(-)
create mode 100644 hooks/versionHook.js
diff --git a/config.xml b/config.xml
index 3b89543..758b858 100644
--- a/config.xml
+++ b/config.xml
@@ -9,6 +9,8 @@ Build it with 'npm run build' before any cordova
specific actions - so that vuejs can
compile to www folder</description>
<author email="tor-einar.skog@nibio.no" href="https://www.nibio.no/en/employees/tor-einar-skog">Tor-Einar Skog</author>
+ <author email="bhabesh.mukhopadhyay@nibio.no" href="https://nibio.no/en/employees/bhabesh-bhabani-mukhopadhyay">Bhabesh B Mukhopadhyay</author>
+ <hook src="hooks/versionHook.js" type="after_prepare" />
<content src="index.html" />
<access origin="*" />
<allow-navigation href="*" />
diff --git a/hooks/versionHook.js b/hooks/versionHook.js
new file mode 100644
index 0000000..30e1d36
--- /dev/null
+++ b/hooks/versionHook.js
@@ -0,0 +1,63 @@
+#!/usr/bin/env node
+
+// From : https://stackoverflow.com/questions/31999368/check-cordova-version-from-javascript/65471679#65471679
+// Good to read https://stackoverflow.com/questions/28304156/how-do-i-get-the-version-string-i-defined-in-the-config-xml
+// Good to read https://www.bram.us/2015/01/04/cordova-build-hook-script-for-displaying-build-version-in-your-app/
+// see https://stackoverflow.com/a/42650842
+// This plugin replaces text in a file with the app version from config.xml.
+
+// be sure to exec `npm i xml2js --save-dev` to make xml2js available
+console.log('Enter into Hooks');
+var wwwFileToReplace = "index.html";
+
+var fs = require('fs')
+var path = require('path')
+
+module.exports = function (context) {
+ var projectRoot = context.opts.projectRoot
+ const wwwDir = path.join(projectRoot, 'platforms', 'android', 'app', 'src', 'main', 'assets', 'www')
+
+ var configXMLPath = 'config.xml'
+ loadConfigXMLDoc(configXMLPath, (rawJSON) => {
+ var version = rawJSON.widget.$.version
+ console.log('Version:', version)
+
+ var fullfilename = path.join(wwwDir, wwwFileToReplace)
+ if (fs.existsSync(fullfilename)) {
+ replaceStringInFile(fullfilename, '%%VERSION%%', version)
+ console.log(context.hook + ': Replaced version in file: ' + path.relative(projectRoot, fullfilename))
+ } else {
+ console.error('File does not exist: ', path.relative(projectRoot, fullfilename))
+ process.exit(1)
+ }
+ })
+}
+
+function loadConfigXMLDoc (filePath, callback) {
+ var fs = require('fs')
+ var xml2js = require('xml2js')
+ try {
+ var fileData = fs.readFileSync(filePath, 'ascii')
+ var parser = new xml2js.Parser()
+ parser.parseString(fileData.substring(0, fileData.length), function (err, result) {
+ if (err) {
+ console.error(err)
+ process.exit(1)
+ } else {
+ // console.log("config.xml as JSON", JSON.stringify(result, null, 2))
+ console.log("File '" + filePath + "' was successfully read.")
+ callback(result)
+ }
+ })
+ } catch (ex) {
+ console.log(ex)
+ process.exit(1)
+ }
+}
+
+function replaceStringInFile (filename, toReplace, replaceWith) {
+ var data = fs.readFileSync(filename, 'utf8')
+
+ var result = data.replace(new RegExp(toReplace, 'g'), replaceWith)
+ fs.writeFileSync(filename, result, 'utf8')
+}
\ No newline at end of file
diff --git a/index.html b/index.html
index 1517175..1b0f861 100644
--- a/index.html
+++ b/index.html
@@ -57,6 +57,7 @@ html, body {
<li class="nav-item">
<router-link class="nav-link" onclick="$('.offcanvas-collapse').toggleClass('open')" to="/settings">Innstillinger</router-link>
</li>
+ <li class="nav-item">(v%%VERSION%%)</li>
</ul>
</v-if>
diff --git a/package.json b/package.json
index a92c0ad..9162a4b 100644
--- a/package.json
+++ b/package.json
@@ -63,7 +63,8 @@
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.9.1",
- "webpack-merge": "^4.1.0"
+ "webpack-merge": "^4.1.0",
+ "xml2js": "^0.4.23"
},
"browserslist": [
"> 1%",
--
GitLab