Skip to content

Commit

Permalink
fix(utils): update depth of resolved dependencies
Browse files Browse the repository at this point in the history
Fixes #66
  • Loading branch information
Kevin Schmidt committed Jul 3, 2020
1 parent 77dde3a commit c682232
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
11 changes: 7 additions & 4 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const resolveDependencies = function(rootProjectPath, alreadyResolved, pack, pro
if (deps && pack.build) {
deps.forEach(function(dep) {
if (dep in alreadyResolved) {
var resolvedVersion = alreadyResolved[dep];
var resolvedVersion = alreadyResolved[dep].version;
var requestedVersion = pack.dependencies[dep];

if (semver.valid(requestedVersion) ||
Expand All @@ -35,7 +35,7 @@ const resolveDependencies = function(rootProjectPath, alreadyResolved, pack, pro
throw new Error('The package "' + pack.name + '" has a ' +
'dependency on "' + dep + '" version ' +
pack.dependencies[dep] + ' which has already been ' +
'resolved as version ' + alreadyResolved[dep]);
'resolved as version ' + alreadyResolved[dep].version);
}
} else {
console.log('WARNING: "' + dep + '" version "' +
Expand Down Expand Up @@ -245,11 +245,14 @@ const resolvePackage = function(rootProjectPath, alreadyResolved, name, depth, d
if (pack.name in alreadyResolved) {
console.log('Resolved ' + depStack.join(' > ') + '@' + pack.version + ' as already resolved. Updating...');
return Promise.map(plugins.updaters, function(updater) {
return updater(pack, depth, depStack);
return Promise.all([
updater(pack, depth, depStack),
utils.updateDependencies(pack.dependencies, alreadyResolved, depth, depStack, updater, [pack.name])
]);
});
}

alreadyResolved[pack.name] = pack.version;
alreadyResolved[pack.name] = pack;
console.log('Resolved ' + depStack.join(' > ') + '@' + pack.version + ' to ' + projectDir);

return Promise.map(plugins.resolvers, function(resolver) {
Expand Down
48 changes: 48 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ describe('utils', () => {
expect(utils.getGroup(['project', 'project-config-test'])).to.equal(utils.Groups.CONFIG);
expect(utils.getGroup(['project', 'project-config-test', 'libproject-config-test'])).to.equal(utils.Groups.CONFIG);
expect(utils.getGroup(['project', 'project-plugin-test-config-test'])).to.equal(utils.Groups.PLUGIN);
expect(utils.getGroup(['project', 'another-project-plugin-test'])).to.equal(utils.Groups.BASE);
});

it('should prefer explicit priority', function() {
Expand Down Expand Up @@ -287,5 +288,52 @@ describe('utils', () => {
expect(list[0].depth).to.equal(2);
expect(list[0].group).to.equal(utils.Groups.BASE);
});

it('should update depth for dependencies', function() {
const list = [
{name: 'project', depth: 0, group: utils.Groups.BASE},
{name: 'lib1', depth: 1, group: utils.Groups.BASE},
{name: 'lib2', depth: 1, group: utils.Groups.BASE}
];

const updater = utils.getGroupDepthUpdater(list);
const depStack = ['project'];

const packApp = {
name: 'project',
build: {},
dependencies: {
'lib1': '1.0.0',
'lib2': '0.0.1'
}
};

const packLib1 = {
name: 'lib1',
build: {},
dependencies: {
'lib2': '0.0.1'
}
};

const packLib2 = {name: 'lib2', build: {}};

const resolved = {
'lib1': packLib1,
'lib2': packLib2
};

return utils.updateDependencies(packApp.dependencies, resolved, 0, depStack, updater, depStack)
.then(() => {
expect(list[0].depth).to.equal(0);
expect(list[0].group).to.equal(utils.Groups.BASE);

expect(list[1].depth).to.equal(1);
expect(list[1].group).to.equal(utils.Groups.BASE);

expect(list[2].depth).to.equal(2);
expect(list[2].group).to.equal(utils.Groups.BASE);
});
});
});
});
35 changes: 34 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,38 @@ const getGroupDepthUpdater = function(list) {
};


/**
* Update dependencies that have already been resolved.
*
* @param {Object} dependencies The package dependencies.
* @param {Object} resolved Map of resolved dependencies.
* @param {number} depth The current depth.
* @param {Array<string>} depStack The current dependency stack.
* @param {Function} updater The update function.
* @param {Array<string>} updated Packages that have already been updated.
*
* @return {Promise} A promise that resolves when all dependencies have been updated.
*/
const updateDependencies = function(dependencies, resolved, depth, depStack, updater) {
const promises = [];
if (dependencies) {
for (const key in dependencies) {
const resolvedPack = resolved[key];

// Only update a package once to avoid dependency cycles
if (resolvedPack && resolvedPack.build && depStack.indexOf(resolvedPack.name) === -1) {
const newDepth = depth + 1;
const newDepStack = [...depStack, resolvedPack.name];
promises.push(updater(resolvedPack, newDepth, newDepStack));
promises.push(updateDependencies(resolvedPack.dependencies, resolved, newDepth, newDepStack,
updater));
}
}
}
return Promise.all(promises);
};


module.exports = {
findLines: findLines,
isAppPackage: isAppPackage,
Expand All @@ -345,5 +377,6 @@ module.exports = {
getPackage: getPackage,
getPackagePriority: getPackagePriority,
priorityGroupDepthSort: priorityGroupDepthSort,
resolveModulePath: resolveModulePath
resolveModulePath: resolveModulePath,
updateDependencies: updateDependencies
};

0 comments on commit c682232

Please sign in to comment.