Skip to content

Commit

Permalink
refactor: remove deprecated functionality and simplify code
Browse files Browse the repository at this point in the history
Deprecated multiple options arguments has been removed in favor of an options object. Deprecated
edit object has been removed in favor of an edit function. These functionalities have been
deprecated for ages and their removal allows for a significant simplification of the code.

BREAKING CHANGE: Multiple options arguments and edit object support has been removed
  • Loading branch information
joshswan committed Feb 9, 2020
1 parent e3c4bab commit f6260c3
Show file tree
Hide file tree
Showing 5 changed files with 3,684 additions and 54 deletions.
12 changes: 4 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"extends": "airbnb-base",
"rules": {
"consistent-return": "off",
"no-buffer-constructor": "off",
"no-param-reassign": ["error", {"props": false}],
"prefer-destructuring": "off",
"strict": "off"
}
"extends": "airbnb-base",
"rules": {
"consistent-return": "off"
}
}
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# Only apps should have lockfiles
yarn.lock
package-lock.json
npm-shrinkwrap.json

### Node ###
# Logs
logs
Expand Down
42 changes: 7 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
* https://github.com/joshswan/gulp-merge/blob/master/LICENSE
*/

'use strict';

const mergeWith = require('lodash.mergewith');
const deprecate = require('deprecate');
const JSON5 = require('json5');
const path = require('path');
const PluginError = require('plugin-error');
Expand All @@ -21,7 +18,8 @@ const mergeOrConcatArrays = (concatArrays, mergeArrays) => (objValue, srcValue)
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
if (concatArrays) {
return objValue.concat(srcValue);
} else if (!mergeArrays) {
}
if (!mergeArrays) {
return srcValue;
}
}
Expand All @@ -30,9 +28,7 @@ const mergeOrConcatArrays = (concatArrays, mergeArrays) => (objValue, srcValue)
};

function merge(a, b, options) {
const customizer = options.customizer;
const concatArrays = options.concatArrays;
const mergeArrays = options.mergeArrays;
const { customizer, concatArrays, mergeArrays } = options;

if (Array.isArray(a) && concatArrays) {
return a.concat(b);
Expand All @@ -41,11 +37,11 @@ function merge(a, b, options) {
return mergeWith(a, b, customizer || mergeOrConcatArrays(concatArrays, mergeArrays));
}

module.exports = function mergeJson(fileName, edit, startObj, endObj, exportModule, concatArrays) {
let options = {
module.exports = function mergeJson(opts) {
const options = {
// Defaults
fileName: 'combined.json',
edit: json => json,
edit: (json) => json,
startObj: {},
endObj: null,
exportModule: false,
Expand All @@ -55,39 +51,15 @@ module.exports = function mergeJson(fileName, edit, startObj, endObj, exportModu
jsonReplacer: null,
jsonSpace: '\t',
json5: false,
...opts,
};

if (typeof fileName === 'object') {
options = Object.assign(options, fileName);
} else if (arguments.length) {
// DEPRECATED
deprecate('Passing multiple arguments is deprecated! Pass an options object instead.');

options = Object.assign(options, {
fileName: fileName || options.fileName,
edit: edit || options.edit,
startObj: startObj || options.startObj,
endObj: endObj || options.endObj,
exportModule: exportModule || options.exportModule,
concatArrays: concatArrays || options.concatArrays,
});
}

const jsonLib = (options.json5) ? JSON5 : JSON;

if ((options.startObj && typeof options.startObj !== 'object') || (options.endObj && typeof options.endObj !== 'object')) {
throw new PluginError(PLUGIN_NAME, `${PLUGIN_NAME}: Invalid start and/or end object!`);
}

if (typeof options.edit === 'object') {
// DEPRECATED
deprecate('Using an object as an edit function is deprecated! Use a function instead.');

const obj = options.edit;

options.edit = json => merge(json, obj, options);
}

let merged = options.startObj;
let firstFile = null;

Expand Down
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gulp-merge-json",
"version": "1.3.1",
"description": "A gulp plugin to merge JSON files into one file",
"author": "Josh Swan <josh@sportifik.com>",
"author": "Josh Swan <josh@disruptivelabs.io>",
"main": "./index.js",
"typings": "./index.d.ts",
"repository": {
Expand All @@ -18,13 +18,19 @@
"json",
"merge"
],
"files": [
"index.d.ts",
"index.js",
"LICENSE",
"README.md"
],
"scripts": {
"lint": "eslint index.js test/*.js",
"test": "istanbul test ./node_modules/mocha/bin/_mocha --report html -- test/*.js --reporter spec",
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"dependencies": {
"deprecate": "1.0.0",
"json5": "^1.0.1",
"json5": "^2.1.1",
"lodash.mergewith": "^4.6.1",
"plugin-error": "^1.0.1",
"through": "^2.3.8",
Expand All @@ -33,12 +39,12 @@
"devDependencies": {
"@types/gulp-util": "^3.0.31",
"coveralls": "*",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.10.0",
"gulp": "^3.9.0",
"istanbul": "*",
"mocha": "^5.0.5",
"mocha": "^7.0.1",
"should": "^13.2.1"
},
"license": "MIT"
Expand Down
Loading

0 comments on commit f6260c3

Please sign in to comment.