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

68 lines
2.2 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-21 18:32:35 +11:00
var log = require('../core/log');
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();
if (u.isArgv('major')) {
this.major();
}
else if (u.isArgv('minor')) {
this.minor();
}
else if (u.isArgv('patch')) {
this.patch();
}
else {
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-21 18:32:35 +11:00
log.info("autoIncVersion : new version : " + newVersion);
log.info('package.json updated!');
_this.context.version = newVersion;
2016-10-20 19:32:26 +11:00
_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;