versioner-snatvb
Version:
Auto versions controll tool
208 lines (207 loc) • 7.37 kB
JavaScript
"use strict";
/**
* Created by snatvb on 01.02.17.
*/
var shell = require("shelljs");
var path = require("path");
var fs = require("fs");
var rl = require("readline-sync");
var chalk = require("chalk");
var logger_1 = require("./logger");
var utils_1 = require("./utils");
var App = (function () {
function App(dirname) {
this.PACKAGE_FILE = 'package.json';
this.VERSION_FILE = 'version.json';
this.appName = "versioner";
this.appNameTitle = "Versioner";
this.dirname = dirname;
if (this.about()) {
return;
}
this.basedir = shell.pwd().stdout;
this.packageJson = this.loadJson(this.PACKAGE_FILE);
if (this.packageJson === null) {
return;
}
// this.version = this.getVersion();
this.loadOrCreateVersion();
this.startListenCommands();
}
App.prototype.loadJson = function (file, showError) {
if (showError === void 0) { showError = true; }
var result;
try {
result = fs.readFileSync(path.join(this.basedir, file)).toString();
}
catch (err) {
if (!showError) {
return null;
}
logger_1.error("Error read file " + file + ":");
console.log(err);
return null;
}
try {
result = JSON.parse(result);
}
catch (err) {
if (!showError) {
return null;
}
logger_1.error("Error parse file " + file + ":");
console.log(err);
return null;
}
return result;
};
App.prototype.loadOrCreateVersion = function () {
var version = this.loadJson(this.VERSION_FILE, false);
if (version === null) {
// console.log(this.version);
version = this.getVersion();
this.saveVersion(version);
}
this.version = version;
};
App.prototype.getVersion = function () {
var result;
try {
var version = this.packageJson.version.split('.');
var platform = this.questionPlatform();
result = {
major: parseInt(version[0], 10),
core: parseInt(version[1], 10),
minor: parseInt(version[2], 10),
platform: platform,
build: 0
};
}
catch (err) {
logger_1.error("Error parse version from " + this.PACKAGE_FILE);
console.log(err);
return null;
}
return result;
};
App.prototype.questionPlatform = function () {
var result = rl.question(chalk.yellow('Write your platform: '));
if (result.length < 1) {
logger_1.error("Platform is empty, it's wrong! Go more!");
return this.questionPlatform();
}
return result;
};
App.prototype.startListenCommands = function () {
this.upgrade();
this.git();
};
App.prototype.git = function () {
var git = utils_1.getCommand('git');
var rc = utils_1.getCommand('--rc');
var release = utils_1.getCommand('-r');
var push = utils_1.getCommand('--push');
if (git === null) {
return;
}
this.versionUpgrade();
this.gitExec(git, rc !== null, push !== null, release !== null);
};
App.prototype.versionUpgrade = function () {
if (utils_1.sameVersion(this.packageJson.version, this.version)) {
this.version.build++;
this.saveVersion(this.version);
}
else {
var version = this.getVersion();
this.saveVersion(version);
}
};
App.prototype.saveVersion = function (version) {
fs.writeFileSync(path.join(this.basedir, this.VERSION_FILE), JSON.stringify(version, null, "\t"));
fs.writeFileSync(path.join(this.basedir, this.PACKAGE_FILE), JSON.stringify(this.packageJson, null, "\t"));
};
App.prototype.gitExec = function (git, rc, push, release) {
if (typeof git.value !== "string") {
return logger_1.error("Git command don't have commit: " + this.appName + " git \"COMMIT_COMMENT\"");
}
var strVersion = utils_1.generateStringVersion(this.version);
if (release) {
strVersion = "v" + utils_1.generateStringVersionClear(this.version) + "-release_" + this.version.build;
}
else if (rc) {
strVersion = utils_1.generateStringVersionRC(this.version);
}
shell.exec('git add --all');
shell.exec("git commit -m \"" + git.value + "\"");
shell.exec("git tag -a \"" + strVersion + "\" -m \"" + git.value + "\"");
if (push) {
shell.exec("git push origin " + strVersion);
}
};
App.prototype.upgrade = function () {
var major = utils_1.getCommand('--major');
var core = utils_1.getCommand('--core');
var minor = utils_1.getCommand('--minor');
if (major !== null) {
this.version.major++;
this.version.build = 0;
}
if (core !== null) {
this.version.core++;
this.version.build = 0;
}
if (minor !== null) {
this.version.minor++;
this.version.build = 0;
}
this.packageJson.version = utils_1.generateStringVersionClear(this.version);
console.log('Your version: ' + chalk.green(utils_1.generateStringVersion(this.version)));
this.saveVersion(this.version);
};
App.prototype.help = function () {
var cmd = utils_1.getCommand('--help');
if (cmd === null) {
return false;
}
console.log("Welcome to the", chalk.magenta(this.appNameTitle));
console.log("Commands:");
console.log(utils_1.aboutCmd(this.appName, 'git', '[COMMENT]', 'Use this command where you will be pushing'));
console.log(utils_1.aboutCmd(this.appName, '--rc', null, 'Release candidate'));
console.log(utils_1.aboutCmd(this.appName, '-r', null, 'Release version'));
console.log(utils_1.aboutCmd(this.appName, '--major', null, 'Upgrade major version'));
console.log(utils_1.aboutCmd(this.appName, '--core', null, 'Upgrade core version'));
console.log(utils_1.aboutCmd(this.appName, '--minor', null, 'Upgrade minor version'));
return true;
};
App.prototype.about = function () {
var result = false;
if (this.help()) {
result = true;
}
if (this.v()) {
result = true;
}
return result;
};
App.prototype.v = function () {
var cmd = utils_1.getCommand('-v');
var cmdAlias = utils_1.getCommand('--version');
if (cmd === null && cmdAlias === null) {
return false;
}
var json;
try {
json = fs.readFileSync(path.join(this.dirname, this.PACKAGE_FILE)).toString();
json = JSON.parse(json);
}
catch (err) {
logger_1.error("Sorry, I can't found my version file :(");
}
console.log(chalk.green("My version is:"), json.version);
return true;
};
return App;
}());
exports.__esModule = true;
exports["default"] = App;