#!/usr/bin/env node

const semverUtils = require('semver-utils');
const jsonfile = require('jsonfile');
jsonfile.spaces = 2;
const argv = require('yargs')
  .usage('Usage: $0 [-r "releaseVersion"]')
  .required('r', 'Release version is required')
  .option('r', { alias: 'release', describe: 'Release version', type: 'string'})
  .option('pre', { alias: 'prefix', demand: false, default: 'rc', describe: 'Change release version prefix', type: 'string'})
  .option('b', { alias: 'build', demand: false, default: '', describe: 'Build meta data', type: 'string'})
  .option('p', { alias: 'path', demand: false, default: '.', describe: 'Path to package.json', type: 'string'})
  .option('v', { alias: 'verbose', demand: false, describe: 'logs more info', type: 'string'})
  .count('verbose')
  .help('?')
  .alias('?', 'help')
  .example('$0 -r 12', 'creates 1.0.0-rc.12')
  .example('$0 -r 12 --pre alpha -b 420', 'creates 1.0.0-alpha.12+420')
  .example('$0 -r dev --pre \'\' -b 12', 'creates 1.0.0-dev+12')
  .example('$0 -r \'\' --pre \'\' -b build.12 -p /path/to/project', 'creates 1.0.0+build.12')
  .argv;

const VERBOSE_LEVEL = argv.verbose;
function INFO()  { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments)};
function DEBUG() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments)};
function VERBOSE() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments)};

const packagePath = argv.p + '/package.json';
DEBUG('packagePath: ' + packagePath);

const packagejson = jsonfile.readFileSync(packagePath);
const currentVersion = packagejson.version;
DEBUG('currentVersion: ' + currentVersion);

const parsedVersion = semverUtils.parse(currentVersion);
DEBUG('parsedVersion: ' + parsedVersion);

const release = argv.r;
if(release.length) {
  VERBOSE('release: ' + release);
}

const prefix = argv.pre.length ? argv.pre + (argv.r.length ? '.' : '') : '';
VERBOSE('release prefix: ' + prefix);

const buildNumber = argv.b;
DEBUG('Updating with Build Number: ' + buildNumber);

var newPackageVersion = semverUtils.stringify({
  major: parsedVersion.major,
  minor: parsedVersion.minor,
  patch: parsedVersion.patch,
  release: prefix + release,
  build: buildNumber
});
VERBOSE('newPackageVersion: ' + newPackageVersion);

packagejson.version = newPackageVersion;

try {
  jsonfile.writeFileSync(packagePath, packagejson);
} catch (error) {
  error('ERROR Occured: ', error);
}

INFO('Version ' + currentVersion + ' updated to Version: ' + newPackageVersion);

@@ -193,56 +193,28 @@
 on%22%5D
-')%0A  .required('r', 'Release version is required
+ %5B-b %22buildVersion%22%5D
 ')%0A 
@@ -989,27 +989,8 @@
 ('$0
- -r %5C'%5C' --pre %5C'%5C'
  -b 
@@ -1163,33 +1163,32 @@
 ole, arguments)%7D
-;
 %0Afunction DEBUG(
@@ -1251,17 +1251,16 @@
 uments)%7D
-;
 %0Afunctio
@@ -1333,17 +1333,16 @@
 uments)%7D
-;
 %0A%0Aconst 
@@ -1401,19 +1401,20 @@
 gePath: 
-' +
+%25s',
  package
@@ -1544,19 +1544,20 @@
 ersion: 
-' +
+%25s',
  current
@@ -1695,30 +1695,18 @@
 .r;%0A
-if(
 release
-.length) %7B%0A 
+ &&
  VER
@@ -1720,19 +1720,20 @@
 elease: 
-' +
+%25s',
  release
@@ -1739,125 +1739,230 @@
 e);%0A
-%7D%0A%0Aconst prefix = argv.pre.length ? argv.pre + (argv.r.length ? '.' : '') : '';%0AVERBOSE('release prefix: ' + prefix);
+%0Avar prerelease;%0Aif (release) %7B%0A  var prefix = argv.pre;%0A  if (prefix.length %3E 0) %7B%0A    VERBOSE('release prefix: %25s', prefix);%0A    prefix += '.';%0A  %7D%0A  prerelease = prefix + release;%0A  DEBUG('prerelease %25s', prerelease);%0A%7D
 %0A%0Aco
@@ -2022,19 +2022,20 @@
 Number: 
-' +
+%25s',
  buildNu
@@ -2190,30 +2190,24 @@
 release: pre
-fix + 
 release,%0A  b
@@ -2248,35 +2248,36 @@
 PackageVersion: 
-' +
+%25s',
  newPackageVersi
@@ -2456,54 +2456,51 @@
 ion 
-' + currentVersion + ' updated to 
+%25s updated to Version: %25s', current
 Version
-: ' +
+,
  new

make prerelease optional