Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/__tests__/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('bolt add', () => {
cwd: projectDir
})
);
expect(yarn.add).toHaveBeenCalledTimes(0);
expect(yarn.add).toHaveBeenCalledTimes(1);
});

test('adding new dependency with --dev flag', async () => {
Expand Down
21 changes: 19 additions & 2 deletions src/utils/__tests__/addDependenciesToPackages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,30 @@ describe('utils/addDependenciesToPackages', () => {
);
});

test('it should not yarn install packages that are already installed', async () => {
test('it should not yarn install packages that are already installed when run from a workspace', async () => {
let rootDir = f.copy('nested-workspaces');
let project = await Project.init(rootDir);
let packages = await project.getPackages();
let pkg = project.getPackageByName(packages, 'foo');
if (!pkg) throw new Error('missing foo');

await addDependenciesToPackage(project, pkg, [{ name: 'react' }]);

expect(unsafeYarn.add).toHaveBeenCalledTimes(0);
});

test('it should yarn install packages that are already installed when run from the project root', async () => {
let cwd = f.copy('nested-workspaces');
let project = await Project.init(cwd);

await addDependenciesToPackage(project, project.pkg, [{ name: 'react' }]);

expect(unsafeYarn.add).toHaveBeenCalledTimes(0);
expect(unsafeYarn.add).toHaveBeenCalledTimes(1);
expect(unsafeYarn.add).toHaveBeenCalledWith(
project.pkg,
[{ name: 'react' }],
'dependencies'
);
});

test('it should throw if version does not match version in project config', async () => {
Expand Down
7 changes: 5 additions & 2 deletions src/utils/addDependenciesToPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ export default async function addDependenciesToPackage(
let projectDependencies = project.pkg.getAllDependencies();
let pkgDependencies = pkg.getAllDependencies();
let { graph: depGraph } = await project.getDependencyGraph(packages);
let inProjectRoot = pkg.isSamePackage(project.pkg);

let dependencyNames = dependencies.map(dep => dep.name);
let externalDeps = dependencies.filter(dep => !depGraph.has(dep.name));
let internalDeps = dependencies.filter(dep => depGraph.has(dep.name));

let externalDepsToInstallForProject = externalDeps.filter(
dep => !projectDependencies.has(dep.name)
// If we are in the project, always (re)install external deps to keep same behaviour as yarn
// Otherwise, if in a workspace, only project install if it isn't already installed
dep => inProjectRoot || !projectDependencies.has(dep.name)
);
if (externalDepsToInstallForProject.length !== 0) {
await yarn.add(project.pkg, externalDepsToInstallForProject, type);
// we reinitialise the project config because it will be modified externally by yarn
project = await Project.init(project.pkg.dir);
}
if (pkg.isSamePackage(project.pkg)) {
if (inProjectRoot) {
if (internalDeps.length > 0) {
throw new BoltError(
messages.cannotInstallWorkspaceInProject(internalDeps[0].name)
Expand Down