New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

merge-jsons-webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

merge-jsons-webpack-plugin - npm Package Compare versions

Comparing version

to
1.0.8

233

index.js
"use strict";
var es6_promise_1 = require("es6-promise");
var path = require("path");
var Glob = require("glob");
var fs = require("fs");
var _root = path.resolve(__dirname, "./");
var UTF8_ENCODING = "utf8";
var MergeJsonWebpackPlugin = (function () {
function MergeJsonWebpackPlugin(options) {
var _this = this;
this.apply = function (compiler) {
compiler.plugin("this-compilation", function (compilation) {
console.log("MergeJsonWebpackPlugin compiling....");
_this.init();
const es6_promise_1 = require("es6-promise");
const path = require("path");
const Glob = require("glob");
const fs = require("fs");
const _root = path.resolve(__dirname, "./");
const UTF8_ENCODING = "utf8";
class MergeJsonWebpackPlugin {
constructor(options) {
this.apply = (compiler) => {
compiler.plugin('emit', (compilation, done) => {
console.log('MergetJsonsWebpackPlugin emit...');
let files = this.options.files;
let output = this.options.output;
let groupBy = output.groupBy;
if (files && groupBy) {
compilation.errors.push('MergeJsonWebpackPlugin: Specify either files (all the files to merge with filename) or groupBy to specifiy a pattern(s)' +
'of file(s) to merge. ');
}
if (files) {
let outputPath = output.fileName;
this.processFiles(compilation, files, outputPath).then((result) => {
done();
});
}
else if (groupBy) {
if (groupBy.length == 0) {
compilation.errors.push('MergeJsonWebpackPlugin: \"groupBy\" must be non empty object');
}
groupBy.forEach((globs) => {
let pattern = globs.pattern;
let outputPath = globs.fileName;
this._glob(pattern).then((files) => {
this.processFiles(compilation, files, outputPath).then((result) => {
done();
});
});
});
}
});
};
this.load = function (files) {
return new es6_promise_1.Promise(function (resolve, reject) {
var mergedJsons = [];
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var f = files_1[_i];
f = f.trim();
if (!f.endsWith(".json") && !f.endsWith(".JSON")) {
throw new Error("Not a valid json file " + f);
this.processFiles = (compilation, files, outputPath) => {
var fileContents = files.map(this.readFile);
let mergedContents = {};
return es6_promise_1.Promise.all(fileContents)
.then((contents) => {
contents.forEach((content) => {
mergedContents = this.mergeDeep(mergedContents, content);
});
mergedContents = JSON.stringify(mergedContents);
compilation.assets[outputPath] = {
size: function () {
return mergedContents.length;
},
source: function () {
return mergedContents;
}
var entryData = undefined;
try {
var encoding = _this.options.encoding;
if (!encoding) {
encoding = UTF8_ENCODING;
}
console.log('encodin ', encoding);
entryData = fs.readFileSync(f, encoding);
}
catch (e) {
console.error("File missing [", f, "] error ", e);
throw e;
}
if (!entryData) {
throw new Error("Data appears to be empty in file [" + f + " ]");
}
var entryDataAsJSON = {};
try {
entryDataAsJSON = JSON.parse(entryData);
}
catch (e) {
console.error("Error parsing the json file [ ", f, " ] and error is ", e);
throw e;
}
if (typeof entryDataAsJSON !== 'object') {
throw new Error("Not a valid object , file [ " + f + " ]");
}
mergedJsons.push(entryDataAsJSON);
};
return;
})
.catch(function (reason) {
console.error("MergeJsonWebpackPlugin: Unable to process json files, ", reason);
compilation.errors.push(`MergeJsonWebpackPlugin: ${reason}`);
});
};
this.readFile = (f) => {
return new es6_promise_1.Promise((resolve, reject) => {
f = f.trim();
if (!f.endsWith(".json") && !f.endsWith(".JSON")) {
reject(`MergeJsonWebpackPlugin: Not a valid Json file ${f}`);
}
var mergedContents = {};
for (var _a = 0, mergedJsons_1 = mergedJsons; _a < mergedJsons_1.length; _a++) {
var entryData = mergedJsons_1[_a];
mergedContents = _this.mergeDeep(mergedContents, entryData);
let entryData = undefined;
try {
entryData = fs.readFileSync(f, this.options.encoding);
}
var retVal = JSON.stringify(mergedContents);
resolve(retVal);
catch (e) {
console.error("MergeJsonWebpackPlugin: File missing [", f, "] error ", e);
reject(`MergeJsonWebpackPlugin: Unable to locate file ${f}`);
}
if (!entryData) {
console.error("MergeJsonWebpackPlugin: Data appears to be empty in file [" + f + " ]");
reject(`MergeJsonWebpackPlugin: Data appears to be empty in file [ ${f} ]`);
}
let entryDataAsJSON = {};
try {
entryDataAsJSON = JSON.parse(entryData);
}
catch (e) {
console.error("MergeJsonWebpackPlugin: Error parsing the json file [ ", f, " ] and error is ", e);
reject(`MergeJsonWebpackPlugin: Error parsing the json file [${f}] `);
}
if (typeof entryDataAsJSON !== 'object') {
console.error("MergeJsonWebpackPlugin: Not a valid object , file [ " + f + " ]");
reject(`MergeJsonWebpackPlugin: Not a valid object , file [${f} ]`);
}
resolve(entryDataAsJSON);
});
};
this.mergeDeep = function (target, source) {
this.mergeDeep = (target, source) => {
if (typeof target == "object" && typeof source == "object") {
for (var key in source) {
for (const key in source) {
if (source[key] === null && (target[key] === undefined || target[key] === null)) {

@@ -78,3 +113,3 @@ target[key] = null;

target[key] = {};
_this.mergeDeep(target[key], source[key]);
this.mergeDeep(target[key], source[key]);
}

@@ -88,19 +123,7 @@ else {

};
this.write = function (_path, data) {
try {
_this.ensureDirExists(_path)
.then(function () {
fs.writeFileSync(_path, data, 'utf8');
});
}
catch (e) {
console.error("Unable to write output data to the file system ", e);
throw e;
}
};
this._glob = function (pattern) {
return new es6_promise_1.Promise(function (resolve, reject) {
this._glob = (pattern) => {
return new es6_promise_1.Promise((resolve, reject) => {
new Glob(pattern, { mark: true }, function (err, matches) {
if (err) {
throw err;
reject(err);
}

@@ -111,66 +134,6 @@ resolve(matches);

};
this.init = function () {
var files = _this.options.files;
var output = _this.options.output;
var groupBy = output.groupBy;
if (files && groupBy) {
throw new Error('Specify either files (all the files to merge with filename) or groupBy to specifiy a pattern(s)' +
'of file(s) to merge. ');
}
if (files) {
_this.processFiles(files, output.fileName);
}
else if (groupBy) {
_this.processGlob(groupBy);
}
};
this.processFiles = function (files, filename) {
_this.load(files)
.then(function (res) {
_this.write(filename, res);
});
};
this.processGlob = function (groupBy) {
if (groupBy.length == 0) {
throw new Error('\"groupBy\" must be non empty object');
}
var _loop_1 = function (g) {
var pattern = g.pattern;
var fileName = g.fileName;
_this._glob(pattern)
.then(function (res) {
return _this.load(res);
})
.then(function (res) {
_this.write(fileName, res);
});
};
for (var _i = 0, groupBy_1 = groupBy; _i < groupBy_1.length; _i++) {
var g = groupBy_1[_i];
_loop_1(g);
}
};
this.ensureDirExists = function (aPath) {
return new es6_promise_1.Promise(function (resolve, reject) {
_this.isDirExists(aPath);
resolve();
});
};
this.isDirExists = function (aPath) {
var dirname = path.dirname(aPath);
if (fs.existsSync(dirname)) {
return;
}
_this.isDirExists(dirname);
try {
fs.mkdirSync(dirname);
}
catch (e) {
console.error(' unable to create dir ', dirname, e);
}
};
this.options = options;
this.options.encoding = this.options.encoding != null ? this.options.encoding : UTF8_ENCODING;
}
return MergeJsonWebpackPlugin;
}());
}
module.exports = MergeJsonWebpackPlugin;
{
"name": "merge-jsons-webpack-plugin",
"description": "This plugin is used to merge json files into single json file,using glob or file names",
"version": "1.0.7",
"version": "1.0.8",
"author": {

@@ -46,5 +46,4 @@ "email": "sudharsan_tk@yahoo.co.in",

"deploy": "npm publish"
},
"files": {}
}
}

@@ -32,3 +32,3 @@ # Merge json files

##Details
## Details
You can specify either files or groupBy under output.

@@ -56,3 +56,3 @@

| files | Array of json files to be merged |
| output.fileName | Name of merged output file |
| output.fileName | Name of merged output file ,relative path from output.path entry |
| encoding | Optional,encoding to be used default is utf-8 |

@@ -87,6 +87,14 @@

| | **Use** curly brackets to group more than one pattern together | pattern:"{./node_modules/**/en.json,./src/assets/i18n/en.json}" |
| groupBy[].fileName | output file name for the corresponding pattern. | |
| groupBy[].fileName | output file name for the corresponding pattern.Relative path from output.path entry | |
| encoding | Optional,encoding to be used default is utf-8 | |
##Sample
## Change Logs
| Version | Changes |
|--------------------|-----------------------------------|
| 1.0.8 | Error handling improved. Now **fileName** is relative path to output path specified |
## Sample
Please navigate to example folder

@@ -93,0 +101,0 @@