Skip to content

Commit

Permalink
feat(version): add setVersion to write version number in config.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
gligoran committed Apr 4, 2017
1 parent 1c387b1 commit fd96703
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.vscode
npm-debug*.log
coverage
coverage
test/config.xml
Empty file added newLog.log
Empty file.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
"path": "cz-conventional-changelog"
}
},
"dependencies": {},
"dependencies": {
"xml2js": "0.4.17"
},
"description": "CLI and JavaScript API for setting the version in Apache Cordova config.xml",
"devDependencies": {
"chai": "3.5.0",
"codecov": "2.1.0",
"commitizen": "2.9.6",
"cz-conventional-changelog": "2.0.0",
"fs-extra": "2.1.2",
"istanbul": "0.4.5",
"mocha": "3.2.0",
"semantic-release": "^6.3.2"
"semantic-release": "6.3.2"
},
"homepage": "https://github.com/gligoran/cordova-set-version#readme",
"keywords": [
Expand Down
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
'use strict';

module.exports = () => {
var fs = require('fs');
var xml2js = require('xml2js');
var xmlParser = new xml2js.Parser();
var xmlBuilder = new xml2js.Builder();

};
module.exports = {
setVersion: setVersion
};

function setVersion(configPath, version, callback) {
fs.readFile(configPath, { encoding: 'UTF-8' }, function (readError, configData) {
if (readError) {
callback(readError);
}

xmlParser.parseString(configData, function (parseError, configXml) {
if (parseError) {
callback(parseError);
}

configXml.widget.$.version = version;
var newConfigData = xmlBuilder.buildObject(configXml);
fs.writeFile(configPath, newConfigData, { encoding: 'UTF-8' }, callback);
});
});
}
39 changes: 39 additions & 0 deletions test/config.expected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">
<allow-intent href="market:*"/>
</platform>
<platform name="ios">
<allow-intent href="itms:*"/>
<allow-intent href="itms-apps:*"/>
</platform>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="FadeSplashScreenDuration" value="300"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
<plugin name="cordova-plugin-console" spec="1.0.5"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
<plugin name="cordova-plugin-device" spec="1.1.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
</widget>
39 changes: 39 additions & 0 deletions test/config.original.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="0.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">
<allow-intent href="market:*"/>
</platform>
<platform name="ios">
<allow-intent href="itms:*"/>
<allow-intent href="itms-apps:*"/>
</platform>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="FadeSplashScreenDuration" value="300"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
<plugin name="cordova-plugin-console" spec="1.0.5"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
<plugin name="cordova-plugin-device" spec="1.1.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
</widget>
36 changes: 34 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
var expect = require('chai').expect;
var cordovaSetVersion = require('../src/index');
var fs = require('fs-extra');

describe('cordova-set-version', function() {
it('should work!', function() {
var originalPath = './test/config.original.xml';
var configPath = './test/config.xml';
var expectedConfigPath = './test/config.expected.xml';
var fileParams = { encoding: 'UTF-8' };

describe('cordova-set-version', () => {
it('should exist', () => {
expect(cordovaSetVersion).to.exist;
});

describe('setVersion', () => {
beforeEach(() => {
fs.copySync(originalPath, configPath);
});

it('should exist', () => {
expect(cordovaSetVersion).to.have.property('setVersion');
});

it('should be a function', () => {
expect(typeof cordovaSetVersion.setVersion).to.equal('function');
});

it('should produce same content as in `config.expected.xml` when passed the argument `1.0.0`', (done) => {
cordovaSetVersion.setVersion(configPath, '1.0.0', function (error) {
expect(error).to.not.exist;

var config = fs.readFileSync(configPath, fileParams);
var expectedConfig = fs.readFileSync(expectedConfigPath, fileParams);
expect(config).to.equal(expectedConfig);

done();
});
});
});
});

0 comments on commit fd96703

Please sign in to comment.