Release version 1.2.0

This commit is contained in:
rad.swiat 2018-10-27 18:59:02 +01:00
parent 5f4b82bc42
commit 58a56af144
11 changed files with 768 additions and 543 deletions

View file

@ -1,4 +1,5 @@
npm-debug* npm-debug*
.idea/ .idea/
.vscode/
demo/ demo/
scrapyard/ scrapyard/

View file

@ -23,8 +23,8 @@ Auto Inject Version (AIV) can:
- auto increase package.json version by --env.major, --env.minor, --env.patch passed into webpack - auto increase package.json version by --env.major, --env.minor, --env.patch passed into webpack
# How to use # How to use
It's easy to set it up, all you need is: It's easy to set it up, all you need is:
* use WebpackAutoInject in webpack plugins * use WebpackAutoInject in webpack plugins
* pass config as a parameter, or leave it blank as all options are "on" by default. * pass config as a parameter, or leave it blank as all options are "on" by default.
### Simple config example ( in webpack.conf.js ) ### Simple config example ( in webpack.conf.js )
@ -67,7 +67,8 @@ module.exports = {
}, },
InjectAsComment: { InjectAsComment: {
tag: 'Version: {version} - {date}', tag: 'Version: {version} - {date}',
dateFormat: 'h:MM:ss TT' dateFormat: 'h:MM:ss TT', // change timezone: `UTC:h:MM:ss` or `GMT:h:MM:ss`
multiLineCommentType: false, // use `/** */` instead of `//` as comment block
}, },
InjectByTag: { InjectByTag: {
fileRegex: /\.+/, fileRegex: /\.+/,
@ -173,11 +174,12 @@ Example:
... ...
InjectAsComment: { InjectAsComment: {
tag: 'Build version: {version} - {date}', // default tag: 'Build version: {version} - {date}', // default
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT' // default dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT', // default
multiLineCommentType: false, // default
} }
}) })
] ]
``` ```
Default: true Default: true
@ -238,6 +240,11 @@ to prevent stripping out AIV comments eg:
``` ```
# Change log # Change log
## [1.2.0] - 27/10/2018
- inject as comment will no more be a version behind with auto increase version
- inject as comment can now switched to multiline comment type eg /** */
- added support for npm log levels eg `npm start -s` will disable console logs
- unit tests added inside the `demo` folder, `npm run test`
## [1.1.0] - 15/03/2018 ## [1.1.0] - 15/03/2018
- webpack sync apply - webpack sync apply
- "name" has been removed as not used anyway, use SHORT instead - "name" has been removed as not used anyway, use SHORT instead

View file

@ -1,5 +1,4 @@
// [AIV_SHORT] Build version: 0.14.0 - Saturday, October 27th, 2018, 2:45:47 PM /******/ (function(modules) { // webpackBootstrap
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache /******/ // The module cache
/******/ var installedModules = {}; /******/ var installedModules = {};
/******/ /******/
@ -88,7 +87,7 @@ test(myVariable);
/* 1 */ /* 1 */
/***/ (function(module, exports) { /***/ (function(module, exports) {
module.exports = "<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Title</title></head><body><span>0.14.0</span> <span>_14:10:47_</span> <span>0.14.0__14:10:47_</span> <span>V:0.14.0 Date:_14:10:47_</span> <span>Version 0.14.0 , _14:10:47_</span> <span id=date>_14:10:47_</span></body></html>" module.exports = "<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Title</title></head><body><span>0.14.0</span> <span>_18:10:17_</span> <span>0.14.0__18:10:17_</span> <span>V:0.14.0 Date:_18:10:17_</span> <span>Version 0.14.0 , _18:10:17_</span> <span id=date>_18:10:17_</span></body></html>"
/***/ }), /***/ }),
/* 2 */ /* 2 */
@ -98,4 +97,4 @@ module.exports = __webpack_require__(0);
/***/ }) /***/ })
/******/ ]); /******/ ]);

View file

@ -9,6 +9,11 @@ describe('Inject by tag', () => {
describe('default', () => { describe('default', () => {
const config = { const config = {
SILENT: true, SILENT: true,
components: {
AutoIncreaseVersion: false,
InjectAsComment: false,
InjectByTag: true,
},
componentsOptions: { componentsOptions: {
InjectByTag: { InjectByTag: {
dateFormat: '_HH:mm:ss_', dateFormat: '_HH:mm:ss_',
@ -37,19 +42,128 @@ describe('Inject by tag', () => {
const date = $('#date').text().trim(); const date = $('#date').text().trim();
expect(/^(_\d\d:\d\d:\d\d_)$/.test(date)).to.be.true; expect(/^(_\d\d:\d\d:\d\d_)$/.test(date)).to.be.true;
}); });
});
}
});
});
});
if (webpackConfName === 'confHtml') {
it('HTML!?!?>!?', () => {
const distHtml = getDistFile('index.html');
expect(distHtml).to.not.include('[AIV]');
});
}
describe('Inject as comment', () => {
// default inject by tag tests
describe('default', () => {
const config = {
SILENT: true,
components: {
AutoIncreaseVersion: false,
InjectAsComment: true,
InjectByTag: false,
},
componentsOptions: {
InjectAsComment: {
tag: '_Version: {version} - {date}',
dateFormat: '_HH:mm:ss_',
// multiLineCommentType: true
},
},
};
// container for webpack iterations
describe('Iterate webpack configs', async () => {
for (const [webpackConfName, webpackConfig] of testWebpackConfigs) {
describe(`webpack: ${webpackConfName}`, () => {
it('prepare', async () => {
await webpackCompile(webpackConfig, config);
});
it('Should include AIV_SHORT comment block', async () => {
const distMainJS = getDistFile('js/main.js');
expect(distMainJS).to.include('[AIV_SHORT] _Version:');
});
it('Should use single line comment block', () => {
const distMainJS = getDistFile('js/main.js');
expect(distMainJS).to.match(/^(\/\/)/);
});
}); });
} }
}); });
}); });
// default inject by tag tests
describe('multiline', () => {
const config = {
SILENT: true,
components: {
AutoIncreaseVersion: false,
InjectAsComment: true,
InjectByTag: false,
},
componentsOptions: {
InjectAsComment: {
tag: '_Version: {version} - {date}',
dateFormat: '_HH:mm:ss_',
multiLineCommentType: true
},
},
};
// container for webpack iterations
describe('Iterate webpack configs', async () => {
for (const [webpackConfName, webpackConfig] of testWebpackConfigs) {
describe(`webpack: ${webpackConfName}`, () => {
it('prepare', async () => {
await webpackCompile(webpackConfig, config);
});
it('Should use multiline comment block', () => {
const distMainJS = getDistFile('js/main.js');
expect(distMainJS).to.match(/^(\/\*\*)/);
});
});
}
});
});
}); });
describe('Auto increase version', () => {
// default inject by tag tests
describe('default', () => {
const config = {
SILENT: true,
components: {
AutoIncreaseVersion: true,
InjectAsComment: true,
InjectByTag: false,
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: true,
simulate: true, // testing purpose only
forceMode: 'patch', // testing purpose only
},
},
};
// container for webpack iterations
describe('Iterate webpack configs', async () => {
for (const [webpackConfName, webpackConfig] of testWebpackConfigs) {
describe(`webpack: ${webpackConfName}`, () => {
it('prepare', async () => {
await webpackCompile(webpackConfig, config);
});
it('InjectAsComment should include correct AIV version', async () => {
const distMainJS = getDistFile('js/main.js');
expect(distMainJS).to.include('0.14.1');
});
});
}
});
});
});

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{ {
"name": "webpack-auto-inject-version", "name": "webpack-auto-inject-version",
"version": "1.1.0", "version": "1.2.0",
"repository": "radswiat/webpack-auto-inject-version", "repository": "radswiat/webpack-auto-inject-version",
"description": "Webpack plugin for auto inject version from package.json", "description": "Webpack plugin for auto inject version from package.json",
"main": "dist/WebpackAutoInjectVersion.js", "main": "dist/WebpackAutoInjectVersion.js",

View file

@ -1,34 +0,0 @@
// this.context.compiler.plugin('compilation', (compilation) => {
// console.log(chalk.red('COOOMPILE!'));
// console.log(compilation.assets)
//
// compilation.plugin('optimize-chunk-assets', (chunks, cb) => {
// //unless you specified multiple entries in your config
// //there's only one chunk at this point
// chunks.forEach((chunk) => {
// //chunks have circular references to their modules
// console.log(chalk.green('----- module -----'));
// // console.log(JSON.stringify(chunk));
//
// chunk.files.forEach((file) => {
// console.log('--------------------------');
// console.log(file);
// // console.log(compilation.assets[file]);
// // compilation.assets[file] = 'asdasdadas';
//
// // const asset = `VERSION! ${endOfLine} ${compilation.assets[file].source()} `;
// // compilation.assets[file].source = () => asset;
// // console.log('--------- end -----------------');
//
// const assetFile = compilation.assets[file];
// const assetFilename = file;
// const ext = path.extname(assetFilename).replace(/(\?)(.){0,}/, '');
// console.log('ext', ext);
// const newContent = this.handleAssetFile(ext, assetFile);
// console.log(newContent);
// });
// });
// cb();
// });
//
// });

View file

@ -54,6 +54,15 @@ export default class AutoIncreaseVersion {
if (!this.packageFile) { if (!this.packageFile) {
return; return;
} }
// 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]();
}
}
if (isArgv('major')) { if (isArgv('major')) {
this.major(); this.major();
} else if (isArgv('minor')) { } else if (isArgv('minor')) {
@ -83,12 +92,25 @@ export default class AutoIncreaseVersion {
} }
} }
updateContextVersion(newVersion) {
this.context.version = newVersion;
}
/** /**
* Close & save package file * Close & save package file
* @param newVersion * @param newVersion
*/ */
closePackageFile(newVersion) { closePackageFile(newVersion) {
this.packageFile.version = newVersion; this.packageFile.version = newVersion;
// 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
fs.writeFile( fs.writeFile(
path.resolve(this.context.config.PACKAGE_JSON_PATH), path.resolve(this.context.config.PACKAGE_JSON_PATH),
JSON.stringify(this.packageFile, null, 4), (err) => { JSON.stringify(this.packageFile, null, 4), (err) => {
@ -110,6 +132,7 @@ export default class AutoIncreaseVersion {
*/ */
major() { major() {
const newVersion = semver.inc(this.packageFile.version, 'major'); const newVersion = semver.inc(this.packageFile.version, 'major');
this.updateContextVersion(newVersion);
this.closePackageFile(newVersion); this.closePackageFile(newVersion);
} }
@ -118,6 +141,7 @@ export default class AutoIncreaseVersion {
*/ */
minor() { minor() {
const newVersion = semver.inc(this.packageFile.version, 'minor'); const newVersion = semver.inc(this.packageFile.version, 'minor');
this.updateContextVersion(newVersion);
this.closePackageFile(newVersion); this.closePackageFile(newVersion);
} }
@ -126,6 +150,7 @@ export default class AutoIncreaseVersion {
*/ */
patch() { patch() {
const newVersion = semver.inc(this.packageFile.version, 'patch'); const newVersion = semver.inc(this.packageFile.version, 'patch');
this.updateContextVersion(newVersion);
this.closePackageFile(newVersion); this.closePackageFile(newVersion);
} }
} }

View file

@ -129,7 +129,12 @@ export default class InjectAsComment {
* @param asset * @param asset
*/ */
injectIntoJs(asset) { injectIntoJs(asset) {
let modAsset = this.parseTags(`// [${config.SHORT}] `, ' '); let modAsset;
if (this.context.config.componentsOptions.InjectAsComment.multiLineCommentType) {
modAsset = this.parseTags(`/** [${config.SHORT}] `, '*/ ');
} else {
modAsset = this.parseTags(`// [${config.SHORT}] `, ' ');
}
modAsset += `${endOfLine} ${asset.source()} `; modAsset += `${endOfLine} ${asset.source()} `;
asset.source = () => modAsset; asset.source = () => modAsset;
} }

View file

@ -14,6 +14,7 @@ export default {
InjectAsComment: { InjectAsComment: {
tag: 'Build version: {version} - {date}', tag: 'Build version: {version} - {date}',
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT', dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT',
multiLineCommentType: false,
}, },
InjectByTag: { InjectByTag: {
fileRegex: /\.+/, fileRegex: /\.+/,

View file

@ -14,7 +14,11 @@ class Log {
} }
getLogLevel() { getLogLevel() {
if (isArgv('aiv-log-full')) {
// support npm log levels
if (process.env.npm_config_loglevel === 'silent') {
this.logLevel = 0;
} else if (isArgv('aiv-log-full')) {
this.logLevel = 3; this.logLevel = 3;
} else if (isArgv('aiv-log-none')) { } else if (isArgv('aiv-log-none')) {
this.logLevel = 0; this.logLevel = 0;