Skip to content
Snippets Groups Projects
Commit 80df74f8 authored by Bhabesh Bhabani Mukhopadhyay's avatar Bhabesh Bhabani Mukhopadhyay
Browse files

Create hook to get app version

Get app version from <widget> at config.xml of cordova
parent cbd3cd61
Branches
No related tags found
No related merge requests found
...@@ -9,6 +9,8 @@ Build it with 'npm run build' before any cordova ...@@ -9,6 +9,8 @@ Build it with 'npm run build' before any cordova
specific actions - so that vuejs can specific actions - so that vuejs can
compile to www folder</description> 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="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" /> <content src="index.html" />
<access origin="*" /> <access origin="*" />
<allow-navigation href="*" /> <allow-navigation href="*" />
......
#!/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
...@@ -57,6 +57,7 @@ html, body { ...@@ -57,6 +57,7 @@ html, body {
<li class="nav-item"> <li class="nav-item">
<router-link class="nav-link" onclick="$('.offcanvas-collapse').toggleClass('open')" to="/settings">Innstillinger</router-link> <router-link class="nav-link" onclick="$('.offcanvas-collapse').toggleClass('open')" to="/settings">Innstillinger</router-link>
</li> </li>
<li class="nav-item">(v%%VERSION%%)</li>
</ul> </ul>
</v-if> </v-if>
......
...@@ -63,7 +63,8 @@ ...@@ -63,7 +63,8 @@
"webpack": "^3.6.0", "webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0", "webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.9.1", "webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.1.0" "webpack-merge": "^4.1.0",
"xml2js": "^0.4.23"
}, },
"browserslist": [ "browserslist": [
"> 1%", "> 1%",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment