Skip to content

Commit

Permalink
platform-overrides module port
Browse files Browse the repository at this point in the history
- fixes lodash vulnerability issue
- closes nwutils#500
  • Loading branch information
vankasteelj committed Jan 14, 2019
1 parent a0f2994 commit c490cb0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var rcedit = thenify(require('rcedit'));
var winresourcer = thenify(require('winresourcer'));
var spawn = require('child_process').spawn;
var semver = require('semver');
var platformOverrides = require('platform-overrides');
var platformOverrides = require('./platformOverrides.js');
var deprecate = require('deprecate');
var updateNotifier = require('update-notifier');
var lazyRequire = require('lazy-req')(require);
Expand Down
122 changes: 122 additions & 0 deletions lib/platformOverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Apply platform-specific manifest values. Works with JSON or plain objects. The platform-specific options will override the others only when "building" for that platform and the platformOverrides property will be removed.
(c) The MIT license, 2014, Adam Lynch <[email protected]> (https://github.com/adam-lynch/platform-overrides)
*/

var applyOverrides, detectPlatform, getOverridesToApply, os, _;

os = require('os');
_ = require('lodash');

detectPlatform = function() {
var platform;
platform = os.platform();
if (platform === 'darwin') {
platform = 'osx';
} else if (platform.match(/^win/)) {
platform = 'win';
}
return {
agnostic: platform,
specific: platform + (process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432') ? 64 : 32)
};
};

getOverridesToApply = function(platform, cb) {
var agnosticPlatform, architectureAgnostic, architectureSpecific, _i, _len;
architectureAgnostic = ['linux', 'osx', 'win'];
architectureSpecific = [];
for (_i = 0, _len = architectureAgnostic.length; _i < _len; _i++) {
agnosticPlatform = architectureAgnostic[_i];
architectureSpecific.push(agnosticPlatform + '32');
architectureSpecific.push(agnosticPlatform + '64');
}
if (platform) {
if (!(architectureAgnostic.indexOf(platform) > -1 || architectureSpecific.indexOf(platform) > -1)) {
return cb(new Error('Invalid platform passed', null));
}
if (architectureAgnostic.indexOf(platform) > -1) {
return cb(null, {
agnostic: platform,
specific: null
});
} else {
return cb(null, {
agnostic: platform.substr(0, platform.length - 2),
specific: platform
});
}
} else {
return cb(null, detectPlatform());
}
};

applyOverrides = function(_arg) {
var cb, options, platform, result;
platform = _arg.platform, options = _arg.options, cb = _arg.cb;
result = _.cloneDeep(options);
if ((options.platformOverrides[platform] != null) && Object.keys(options.platformOverrides[platform]).length) {
result = _.mergeWith(result, options.platformOverrides[platform], function(optionValue, overrideValue) {

/*
If overrides.x is {} but source.x is a non-empty object {prop:0, another:2},
take the {}}
*/
if ((_.isPlainObject(overrideValue) || _.isArray(overrideValue)) && _.isEmpty(overrideValue)) {
return overrideValue;
}
});
}
return cb(result);
};

module.exports = function(_arg, cb) {
var callback, options, platform;
options = _arg.options, platform = _arg.platform;
callback = cb != null ? cb : (function() {});
return getOverridesToApply(platform, function(err, overridesToApply) {
var applySpecificOverrides, objectMode, originalOptions;
if (err != null) {
return callback(err, null);
}
originalOptions = options;
objectMode = _.isPlainObject(options);
try {
options = objectMode ? options : JSON.parse(options);
} catch (_error) {
err = _error;
return callback(err, null);
}
if (options.platformOverrides != null) {
applySpecificOverrides = function(optionsToOverride) {
return applyOverrides({
platform: overridesToApply.specific,
options: optionsToOverride,
cb: function(result) {
delete result.platformOverrides;
return callback(null, objectMode ? result : JSON.stringify(result));
}
});
};
if (overridesToApply.agnostic != null) {
return applyOverrides({
platform: overridesToApply.agnostic,
options: options,
cb: function(result) {
if (overridesToApply.specific != null) {
return applySpecificOverrides(result);
} else {
delete result.platformOverrides;
return callback(null, objectMode ? result : JSON.stringify(result));
}
}
});
} else {
return applySpecificOverrides(options);
}
} else {
return callback(null, originalOptions);
}
});
};

0 comments on commit c490cb0

Please sign in to comment.