webpack-auto-inject-version/src/components/auto-increase-version/auto-increase-version.js

157 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-04-11 09:11:09 +10:00
import path from 'path';
import fs from 'fs';
2018-03-15 22:07:22 +11:00
import semver from 'semver';
2017-04-11 09:11:09 +10:00
import { isArgv } from 'core/utils';
import log from 'core/log';
import config from 'config';
2017-04-11 09:11:09 +10:00
2017-04-13 07:32:39 +10:00
export default class AutoIncreaseVersion {
2017-04-11 09:11:09 +10:00
static componentName = 'AutoIncreaseVersion';
constructor(context) {
this.context = context;
}
2017-04-13 07:32:39 +10:00
/**
* Apply will be called from main class
2018-03-15 22:07:22 +11:00
*
2017-04-13 07:32:39 +10:00
* @protected
2018-03-15 22:07:22 +11:00
* @return {Promise}
2017-04-13 07:32:39 +10:00
*/
2017-04-11 09:11:09 +10:00
apply() {
2018-03-15 22:07:22 +11:00
// setup promise
const promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
// when runInWatchMode
// we have to register AutoIncreaseVersion instead of firing it straight away
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
if (this.context.compiler) {
2018-03-15 22:07:22 +11:00
this.context.compiler.plugin('emit', (compilation, cb) => {
this.start();
cb();
});
}
return null;
}
// when runInWatchMode is off
2018-03-15 22:07:22 +11:00
this.start();
return promise;
2017-04-11 09:11:09 +10:00
}
/**
* Start version increase
* - decide scenario: major, minor, patch
*/
start() {
this.packageFile = this.openPackageFile();
if (!this.packageFile) {
return;
}
2018-10-28 04:59:02 +11:00
// handle force mode - major, minor or patch can be applied trough config
// ONLY TO BE USED FOR TESTING PURPOSES,
if (config.componentsOptions.AutoIncreaseVersion.forceMode) {
if (typeof this[config.componentsOptions.AutoIncreaseVersion.forceMode] === 'function') {
return this[config.componentsOptions.AutoIncreaseVersion.forceMode]();
}
}
2017-04-13 07:32:39 +10:00
if (isArgv('major')) {
2017-04-11 09:11:09 +10:00
this.major();
2017-04-13 07:32:39 +10:00
} else if (isArgv('minor')) {
2017-04-11 09:11:09 +10:00
this.minor();
2017-04-13 07:32:39 +10:00
} else if (isArgv('patch')) {
2017-04-11 09:11:09 +10:00
this.patch();
2017-04-13 07:32:39 +10:00
} else {
this.resolve();
2017-04-11 09:11:09 +10:00
}
}
/**
* Open package file
2018-03-15 22:07:22 +11:00
* @return {any}
2017-04-11 09:11:09 +10:00
*/
openPackageFile() {
try {
return JSON.parse(
fs.readFileSync(
path.resolve(this.context.config.PACKAGE_JSON_PATH),
'utf8'
)
);
} catch (err) {
2018-03-15 22:07:22 +11:00
console.log(err);
return null;
}
2017-04-11 09:11:09 +10:00
}
2018-10-28 04:59:02 +11:00
updateContextVersion(newVersion) {
this.context.version = newVersion;
}
2017-04-11 09:11:09 +10:00
/**
* Close & save package file
* @param newVersion
*/
closePackageFile(newVersion) {
this.packageFile.version = newVersion;
2018-10-28 04:59:02 +11:00
// prevent saving package.json file in simulate mode
if (config.componentsOptions.AutoIncreaseVersion.simulate) {
log.info(`autoIncVersion : new version : ${newVersion}`);
log.info('package.json updated!');
return;
}
// write new package.json file
2017-04-11 09:11:09 +10:00
fs.writeFile(
path.resolve(this.context.config.PACKAGE_JSON_PATH),
JSON.stringify(this.packageFile, null, this.context.config.PACKAGE_JSON_INDENT), (err) => {
2017-04-13 07:32:39 +10:00
if (err) {
this.reject(err);
console.log(err);
return false;
}
2017-04-11 09:11:09 +10:00
log.info(`autoIncVersion : new version : ${newVersion}`);
log.info('package.json updated!');
this.context.version = newVersion;
this.resolve();
2017-04-13 07:32:39 +10:00
return true;
});
2017-04-11 09:11:09 +10:00
}
/**
* Increase major
*/
major() {
2018-03-15 22:07:22 +11:00
const newVersion = semver.inc(this.packageFile.version, 'major');
2018-10-28 04:59:02 +11:00
this.updateContextVersion(newVersion);
2017-04-11 09:11:09 +10:00
this.closePackageFile(newVersion);
}
/**
* Increase minor
*/
minor() {
2018-03-15 22:07:22 +11:00
const newVersion = semver.inc(this.packageFile.version, 'minor');
2018-10-28 04:59:02 +11:00
this.updateContextVersion(newVersion);
2017-04-11 09:11:09 +10:00
this.closePackageFile(newVersion);
}
/**
* Increase patch
*/
patch() {
2018-03-15 22:07:22 +11:00
const newVersion = semver.inc(this.packageFile.version, 'patch');
2018-10-28 04:59:02 +11:00
this.updateContextVersion(newVersion);
2017-04-11 09:11:09 +10:00
this.closePackageFile(newVersion);
}
}