Skip to content

Commit

Permalink
feat: change to named args in object (#269)
Browse files Browse the repository at this point in the history
args for cordovaSetVersion function are now in an object and are named

BREAKING CHANGE: cordovaSetVersion now has a single argument with named fields
  • Loading branch information
gligoran authored Aug 20, 2020
1 parent 1518ab5 commit 207d0d9
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 214 deletions.
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const options = {

const cli = meow(options);

const filename = cli.input[0] || null;
const configPath = cli.input[0] || null;
const version = cli.flags.version || null;
const buildNumber = +cli.flags.buildNumber || null;

cordovaSetVersion(filename, version, buildNumber);
cordovaSetVersion({ configPath, version, buildNumber });
64 changes: 8 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,6 @@ const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

const xmlBuilder = new Builder();
const DefaultConfigPath = './config.xml';

function parse1Argument(arg) {
if (typeof arg === 'string' && arg.indexOf('.xml') < 0) {
return [null, arg, null];
}

if (typeof arg === 'number') {
return [null, null, arg];
}

return [arg, null, null];
}

function parse2Arguments(arg1, arg2) {
const arg1IsString = typeof arg1 === 'string';
const arg1IsStringXml = arg1IsString && arg1.indexOf('.xml') >= 0;
const arg2IsNumber = typeof arg2 === 'number';

if (arg2IsNumber && (arg1IsStringXml || !arg1IsString)) {
return [arg1, null, arg2];
}

if (arg1IsString && !arg1IsStringXml) {
return [null, arg1, arg2];
}

return [arg1, arg2, null];
}

function parseArguments(...args) {
switch (args.length) {
case 0:
return [null, null, null];
case 1:
return parse1Argument(args[0]);
case 2:
return parse2Arguments(args[0], args[1]);
default:
return args;
}
}

function checkTypeErrors(configPath, version, buildNumber) {
if (typeof configPath !== 'string') {
Expand Down Expand Up @@ -105,25 +63,19 @@ function setAttributes(xml, version, buildNumber) {
* @param {string} [version]
* @param {number} [buildNumber]
*/
async function cordovaSetVersion(...args) {
let [configPath, version, buildNumber] = parseArguments(...args);
async function cordovaSetVersion({ configPath, version, buildNumber } = {}) {
const cPath = configPath || './config.xml';

configPath = configPath || DefaultConfigPath;
version = version || null;
buildNumber = buildNumber || null;
checkTypeErrors(cPath, version, buildNumber);

checkTypeErrors(configPath, version, buildNumber);
const currentConfig = await getXml(cPath);

let xml = await getXml(configPath);

if (!version && !buildNumber) {
version = await getVersionFromPackage(version);
}
const v = !version && !buildNumber ? await getVersionFromPackage(version) : version;

xml = setAttributes(xml, version, buildNumber);
const newConfig = setAttributes(currentConfig, v, buildNumber);

const newData = xmlBuilder.buildObject(xml);
return writeFile(configPath, newData, { encoding: 'UTF-8' });
const newData = xmlBuilder.buildObject(newConfig);
return writeFile(cPath, newData, { encoding: 'UTF-8' });
}

export default cordovaSetVersion;
42 changes: 21 additions & 21 deletions test/index-tests/build-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,81 @@ import cordovaSetVersion from '../../src';
import { tempConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs';

function buildNumberTest() {
describe('(buildNumber)', () => {
describe('({ buildNumber })', () => {
it('should override existing buildNumber and preserve existing version', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile);

await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86 });

expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD));
});

it('should add buildNumber and preserve existing version', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempConfigFile);

await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86 });

expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_NO_BUILD));
});

it('should override existing buildNumber and not add version', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempConfigFile);

await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86 });

expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_BUILD));
});

it('should add buildNumber and not add version', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempConfigFile);

await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86 });

expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_NO_BUILD));
});

it('should return an error about configPath type', async () => {
it('should return an error about buildNumber type', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile);

try {
await cordovaSetVersion({});
await cordovaSetVersion({ buildNumber: {} });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('configPath');
expect(error.message).toContain('buildNumber');
expect(error.message).toContain('must be a');
}
});

it('should return an error about missing config file', async () => {
it('should return an error about buildNumber type', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile);

try {
await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86.2 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('no such file or directory');
expect(error.message).toContain('config.xml');
expect(error.message).toContain('buildNumber');
expect(error.message).toContain('must be a');
}
});

it('should return an error about malformed config file', async () => {
fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile);

it('should return an error about missing config file', async () => {
try {
await cordovaSetVersion(86);
await cordovaSetVersion({ buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).not.toContain('no such file or directory');
expect(error.message).toContain('no such file or directory');
expect(error.message).toContain('config.xml');
}
});

it('should return an error about buildNumber type', async () => {
it('should return an error about malformed config file', async () => {
fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile);

try {
await cordovaSetVersion(86.2);
await cordovaSetVersion({ buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('buildNumber');
expect(error.message).toContain('must be an');
expect(error.message).not.toContain('no such file or directory');
}
});
});
Expand Down
28 changes: 20 additions & 8 deletions test/index-tests/config-path-build-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ import cordovaSetVersion from '../../src';
import { tempProvidedConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs';

function configPathBuildNumberTest() {
describe('(configPath, buildNumber)', () => {
describe('({ configPath, buildNumber })', () => {
it('should override existing buildNumber and preserve existing version', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD));
});

it('should add buildNumber and preserve existing version', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_NO_BUILD));
});

it('should override existing buildNumber and not add version', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_BUILD));
});

it('should add buildNumber and not add version', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_NO_BUILD));
});
Expand All @@ -42,17 +42,29 @@ function configPathBuildNumberTest() {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

try {
await cordovaSetVersion({}, 86);
await cordovaSetVersion({ configPath: {}, buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('configPath');
expect(error.message).toContain('must be a');
}
});

it('should return an error about buildNumber type', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

try {
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: {} });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('buildNumber');
expect(error.message).toContain('must be a');
}
});

it('should return an error about missing config file', async () => {
try {
await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('no such file or directory');
Expand All @@ -64,7 +76,7 @@ function configPathBuildNumberTest() {
fs.copySync(entryConfigFiles.MALFORMED, tempProvidedConfigFile);

try {
await cordovaSetVersion(tempProvidedConfigFile, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).not.toContain('no such file or directory');
Expand Down
20 changes: 10 additions & 10 deletions test/index-tests/config-path-version-build-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src';
import { tempProvidedConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs';

function configPathVersionBuildNumberTest() {
describe('(configPath, version, buildNumber)', () => {
describe('({ configPath, version, buildNumber })', () => {
it('should override both existing version and buildNumber', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(
readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_BUILD),
Expand All @@ -19,7 +19,7 @@ function configPathVersionBuildNumberTest() {
it('should override existing version and add buildNumber', async () => {
fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(
readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_NO_BUILD),
Expand All @@ -29,7 +29,7 @@ function configPathVersionBuildNumberTest() {
it('should add version and override existing buildNumber', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(
readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_BUILD),
Expand All @@ -39,7 +39,7 @@ function configPathVersionBuildNumberTest() {
it('should add version and buildNumber', async () => {
fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile);

await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });

expect(readFile(tempProvidedConfigFile)).toBe(
readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_NO_BUILD),
Expand All @@ -50,7 +50,7 @@ function configPathVersionBuildNumberTest() {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

try {
await cordovaSetVersion({}, '2.4.9', 86);
await cordovaSetVersion({ configPath: {}, version: '2.4.9', buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('configPath');
Expand All @@ -62,7 +62,7 @@ function configPathVersionBuildNumberTest() {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

try {
await cordovaSetVersion(tempProvidedConfigFile, {}, 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: {}, buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('version');
Expand All @@ -74,7 +74,7 @@ function configPathVersionBuildNumberTest() {
fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile);

try {
await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', {});
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: {} });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('buildNumber');
Expand All @@ -84,7 +84,7 @@ function configPathVersionBuildNumberTest() {

it('should return an error about missing config file', async () => {
try {
await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).toContain('no such file or directory');
Expand All @@ -96,7 +96,7 @@ function configPathVersionBuildNumberTest() {
fs.copySync(entryConfigFiles.MALFORMED, tempProvidedConfigFile);

try {
await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86);
await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 });
} catch (error) {
expect(error).not.toBeNil();
expect(error.message).not.toContain('no such file or directory');
Expand Down
Loading

0 comments on commit 207d0d9

Please sign in to comment.