webpack-auto-inject-version/dist/components/auto-inc-version.js

69 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-10-20 17:58:19 +11:00
var semver = require('semver');
var config = require('../config');
2016-10-20 19:32:26 +11:00
var path = require('path');
var fs = require('fs');
var u = require('../core/utils');
var chalk = require('chalk');
var Promise = require('bluebird');
2016-10-20 17:58:19 +11:00
var IncVersion = (function () {
2016-10-20 19:32:26 +11:00
function IncVersion(context) {
this.context = context;
2016-10-20 17:58:19 +11:00
}
2016-10-20 19:32:26 +11:00
IncVersion.prototype.apply = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.resolve = resolve;
_this.reject = reject;
_this.start();
});
};
IncVersion.prototype.start = function () {
this.packageFile = this.openPackageFile();
var argv = process.argv;
if (u.isArgv('major')) {
this.major();
}
else if (u.isArgv('minor')) {
this.minor();
}
else if (u.isArgv('patch')) {
this.patch();
}
else {
console.log(chalk.bgRed("[@] " + config.SHORT + " error > ") + ' --major --minor --patch missing in arguments. ');
console.log(chalk.bgRed("[@] " + config.SHORT + " how to> ") + ' webpack -w --major');
this.reject();
}
};
2016-10-20 17:58:19 +11:00
IncVersion.prototype.openPackageFile = function () {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
};
2016-10-20 19:32:26 +11:00
IncVersion.prototype.closePackageFile = function (newVersion) {
var _this = this;
this.packageFile.version = newVersion;
fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), function (err) {
2016-10-20 17:58:19 +11:00
if (err) {
2016-10-20 19:32:26 +11:00
_this.reject(err);
2016-10-20 17:58:19 +11:00
return console.log(err);
}
2016-10-20 19:32:26 +11:00
console.log('');
console.log(chalk.bgGreen("[@] " + config.SHORT + " OK > ") + ' package.json updated : ' + _this.packageFile.version);
_this.resolve();
2016-10-20 17:58:19 +11:00
});
};
IncVersion.prototype.major = function () {
2016-10-20 19:32:26 +11:00
var newVersion = semver.inc(this.packageFile.version, 'major');
this.closePackageFile(newVersion);
2016-10-20 17:58:19 +11:00
};
IncVersion.prototype.minor = function () {
2016-10-20 19:32:26 +11:00
var newVersion = semver.inc(this.packageFile.version, 'minor');
this.closePackageFile(newVersion);
2016-10-20 17:58:19 +11:00
};
IncVersion.prototype.patch = function () {
2016-10-20 19:32:26 +11:00
var newVersion = semver.inc(this.packageFile.version, 'patch');
this.closePackageFile(newVersion);
2016-10-20 17:58:19 +11:00
};
return IncVersion;
}());
2016-10-20 19:32:26 +11:00
module.exports = IncVersion;