Skip to content

Commit

Permalink
feat(setVersion): use version from package.json if none given
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
gligoran committed Apr 9, 2017
1 parent ccf7d12 commit 96a1d5f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"dependencies": {
"read-pkg": "2.0.0",
"xml2js": "0.4.17"
},
"description": "CLI and JavaScript API for setting the version in Apache Cordova config.xml",
Expand Down
26 changes: 24 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fs from 'fs';
import { Parser, Builder} from 'xml2js';
import readPkg from 'read-pkg';

/**
* writes version string to config.xml
Expand All @@ -10,7 +11,28 @@ import { Parser, Builder} from 'xml2js';
* @param callback
*/
function setVersion(configPath, version, callback) {
fs.readFile(configPath, {encoding: 'UTF-8'}, handleSetVersionReadFile.bind(this, configPath, version, callback));
getVersion(version, callback)
.then(function (version) {
fs.readFile(configPath, {encoding: 'UTF-8'}, handleSetVersionReadFile.bind(this, configPath, version, callback));
})
.catch(function (error) {
callback(error);
});
}

function getVersion(version) {
if (version) {
return Promise.resolve(version);
}

return readPkg()
.then(function (packageJson) {
if (packageJson && !packageJson.version) {
throw new Error('package.json has no version field');
}

return packageJson.version;
});
}

/**
Expand All @@ -37,7 +59,7 @@ function handleSetVersionReadFile(configPath, version, callback, readError, conf
* @param parseError
* @param {{widget:{$:{version:string}}}} configXml
*/
function handleSetVersionParseXml(configPath, version, callback,parseError, configXml) {
function handleSetVersionParseXml(configPath, version, callback, parseError, configXml) {
if (parseError) {
callback(parseError);
return;
Expand Down
39 changes: 39 additions & 0 deletions test/configs/config.expected-json.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="2.4.9" 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>
83 changes: 77 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ const configPaths = {
GOOD: './configs/config.good.xml',
MALFORMED: './configs/config.malformed.xml',
MISSING: './configs/config.missing.xml',
EXPECTED: './configs/config.expected.xml'
EXPECTED: './configs/config.expected.xml',
EXPECTED_JSON: './configs/config.expected-json.xml'
};
const packagePaths = {
COPY: './package.json',
GOOD: './packages/package.good.json',
MALFORMED: './packages/package.malformed.json',
NO_VERSION: './packages/package.no-version.json',
MISSING: './packages/package.missing.json'
};

describe('cordova-set-version', () => {
beforeEach(() => {
before(() => {
process.chdir(__dirname);
});

Expand All @@ -38,15 +46,15 @@ describe('cordova-set-version', () => {
cordovaSetVersion.setVersion(configPaths.COPY, '1.0.0', function (error) {
expect(error).to.not.exist;

var config = fs.readFileSync(configPaths.COPY, fileParams);
var expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);
let config = fs.readFileSync(configPaths.COPY, fileParams);
let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);
expect(config).to.equal(expectedConfig);

done();
});
});

it('should return an error about missing file', (done) => {
it('should return an error about missing config.xml file', (done) => {
cordovaSetVersion.setVersion(configPaths.MISSING, '1.0.0', function (error) {
expect(error).to.exist;
expect(error.message).to.contain('no such file or directory');
Expand All @@ -56,13 +64,76 @@ describe('cordova-set-version', () => {
});
});

it('should return an error about bad file', (done) => {
it('should return an error about bad config.xml file', (done) => {
cordovaSetVersion.setVersion(configPaths.MALFORMED, '1.0.0', function (error) {
expect(error).to.exist;
expect(error.message).to.not.contain('no such file or directory');

done();
});
});

it('should use version from package.json', (done) => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.GOOD, packagePaths.COPY);

cordovaSetVersion.setVersion(configPaths.COPY, null, function (error) {
expect(error).to.not.exist;

let config = fs.readFileSync(configPaths.COPY, fileParams);
let expectedJsonConfig = fs.readFileSync(configPaths.EXPECTED_JSON, fileParams);
expect(config).to.equal(expectedJsonConfig);

done();
});
});

it('should return an error about missing package file', (done) => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

cordovaSetVersion.setVersion(configPaths.COPY, null, function (error) {
expect(error).to.exist;
expect(error.message).to.contain('no such file or directory');
expect(error.message).to.contain('package.json');

done();
});
});

it('should return an error about bad package file', (done) => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.MALFORMED, packagePaths.COPY);

cordovaSetVersion.setVersion(configPaths.COPY, null, function (error) {
expect(error).to.exist;
expect(error.message).to.contain('Unexpected end of input');
expect(error.message).to.contain('package.json');

done();
});
});

it('should return an error about no version', (done) => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.NO_VERSION, packagePaths.COPY);

cordovaSetVersion.setVersion(configPaths.COPY, null, function (error) {
expect(error).to.exist;
expect(error.message).to.contain('no version');
expect(error.message).to.contain('package.json');

done();
});
});

afterEach(function() {
if (fs.existsSync(configPaths.COPY)) {
fs.removeSync(configPaths.COPY);
}

if (fs.existsSync(packagePaths.COPY)) {
fs.removeSync(packagePaths.COPY);
}
});
});
});
11 changes: 11 additions & 0 deletions test/packages/package.good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "good-package-json",
"version": "2.4.9",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
6 changes: 6 additions & 0 deletions test/packages/package.malformed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "malformed-package-json",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
10 changes: 10 additions & 0 deletions test/packages/package.no-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "good-package-json",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

0 comments on commit 96a1d5f

Please sign in to comment.