diff --git a/config.xml b/config.xml index 3b8954328edeb0df2c71d62edea274662467a3a4..758b858fb1231d1351353a789c54acfe4318659f 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 0000000000000000000000000000000000000000..30e1d3657d104e27b8f549a909c779b3905e7c1e --- /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 1517175c0bd2a4fba766c3938ffa426f013cb9b2..1b0f861a97876fe1da8617f001f706a92ae209e5 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 a92c0ad03370543fc79a6f6124c5527e7efde60d..9162a4b7a18843228a701720014d07f2c79c581d 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%",