forked from nwutils/nw-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fixes lodash vulnerability issue - closes nwutils#500
- Loading branch information
1 parent
a0f2994
commit c490cb0
Showing
2 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}; |