Skip to content

Instantly share code, notes, and snippets.

@guybedford
Last active October 16, 2015 09:49
Show Gist options
  • Save guybedford/53e3f47e746b1272dbbb to your computer and use it in GitHub Desktop.
Save guybedford/53e3f47e746b1272dbbb to your computer and use it in GitHub Desktop.
// meta, packages deep
function getSharedProperties(configA, configB) {
var bKeys = Object.keys(configB);
return Object.keys(configA).filter(function(p) {
return bKeys.indexOf(p) != -1;
});
}
function detectMetaConflict(metaA, metaB) {
if (!metaA || !metaB)
return false;
return getSharedProperties(metaA, metaB).some(function(prop) {
var valueA = metaA[prop];
var valueB = metaB[prop];
// ensure both arrays (which would concat in conflict scenario)
if (valueA instanceof Array || valueB instanceof Array)
return !(valueA instanceof Array && valueB instanceof Array) || !arrayEquals(valueA, valueB);
// ensure objects don't conflict
else if (typeof valueA == 'object' || typeof valueB == 'object')
return !(typeof valueA == 'object' && typeof valueB == 'object') || detectObjectConflict(valueA, valueB);
else
return valueA !== valueB;
})
}
function arrayEquals(arrayA, arrayB) {
// TODO! DO this properly
return JSON.stringify(arrayA.sort()) == JSON.stringify(arrayB.sort());
}
function detectObjectConflict(objA, objB) {
if (!objA || !objB)
return false;
return getSharedProperties(objA, objB).some(function(prop) {
var valueA = objA[prop];
var valueB = objB[prop];
if (valueA instanceof Array && valueB instanceof Array)
return !arrayEquals(valueA, valueB);
// separate properties are always conflicts at this level
else if (typeof valueA == 'object' || typeof valueB == 'object')
return true;
else
return valueA !== valueB;
});
}
function detectSystemJSConflict(configA, configB) {
if (['pluginFirst', 'defaultJSExtensions'].some(function(value) {
if (value in configA && value in configB)
return configA[value] !== configB[value];
}))
return true;
if ('packageConfigPaths' in configA && 'packageConfigPaths' in configB)
return !arrayEquals(configA.packageConfigPaths, configB.packageConfigPaths);
if (['map', 'paths', 'bundles', 'depCache'].some(function(value) {
return detectObjectConflict(configA[value], configB[value]);
}))
return true;
if (['meta', 'babelOptions', 'traceurOptions'].some(function(value) {
return detectMetaConflict(configA[value], configB[value]);
}))
return true;
if (configA.packages && configB.packages) {
if (getSharedProperties(configA.packages, configB.packages).some(function(pkgName) {
var packageA = configA.packages[pkgName];
var packageB = configB.packages[pkgName];
if (['main', 'format', 'defaultExtension', 'basePath'].some(function(value) {
return packageA[value] !== packageB[value];
}))
return true;
if (['map', 'depCache'].some(function(value) {
return detectObjectConflict(packageA[value], packageB[value]);
}))
return true;
if (packageA.modules && packageB.modules)
if (detectMetaConflict(packageA.modules, packageB.modules))
return true;
}))
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment