Skip to content

Instantly share code, notes, and snippets.

@voznik
Created June 14, 2019 09:48
Show Gist options
  • Save voznik/74349244f67f20304118aa48f471f807 to your computer and use it in GitHub Desktop.
Save voznik/74349244f67f20304118aa48f471f807 to your computer and use it in GitHub Desktop.
Updates cordova `config.xml` version from npm version. Also creates or updates `$VERSION` environment variable in .env file. (Bash vs Node)
/**
* Creates or updates `version.json` file with git version info
* Updates cordova `config.xml` version from npm version.
*/
const { gitDescribeSync } = require('git-describe');
const { resolve, relative } = require('path');
const { writeFileSync } = require('fs');
const cordovaSetVersion = require('cordova-set-version');
const version = process.env.npm_package_version;
const gitInfo = gitDescribeSync({
dirtyMark: false,
dirtySemver: false
});
gitInfo.version = version;
const file = resolve(__dirname, '..', 'src', 'config', 'version.json');
writeFileSync(file, `${JSON.stringify(gitInfo, null, 4)} `, { encoding: 'utf-8' });
console.log(`Wrote version info ${gitInfo.raw} to ${relative(resolve(__dirname, '..'), file)}`);
cordovaSetVersion(/* version */); // reads version from package.json
console.log(`Wrote Cordova version ${version}`);
#!/bin/bash
#
# Updates cordova `config.xml` version from npm version.
# Also creates or updates `$VERSION` environment variable in .env file
CONFIG="config.xml"
NEW_VERSION=$npm_package_version
GIT_VERSION=$(git describe --tags)
ENV_FILE=".env"
echo "GIT_VERSION = $GIT_VERSION"
if [ -f "$CONFIG" ]; then
# sed to replace version in config.xml
sed -i "s/\(widget.*version=\"\)\([0-9,.]*\)\"/\1$NEW_VERSION\"/g" $CONFIG
echo "Updated $CONFIG with version $NEW_VERSION"
else
echo 'Could not find config.xml'
exit 1
fi
if [ -f "$ENV_FILE" ]; then
# sed to replace or add version in .env
# 1) only sed
# sed \
# -e "/^\(VERSION=\).*/{s//\1$GIT_VERSION/;:a;n;ba;q}" \
# -e "$aVERSION=$GIT_VERSION" $ENV_FILE
# 2) sed & grep
sed -i "s/^VERSION.*/VERSION=$GIT_VERSION/g" $ENV_FILE
grep -q "VERSION=$GIT_VERSION" $ENV_FILE || echo "VERSION=$GIT_VERSION" >> $ENV_FILE
echo "Updated $ENV_FILE with version $GIT_VERSION"
else
echo 'Could not find .env'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment