Skip to content

Commit

Permalink
Improves "yarn workspaces info" (#5389)
Browse files Browse the repository at this point in the history
* Improves "yarn workspaces info" to include data about how workspaces depend on each other

* Update workspaces.js

* Update workspaces.js

* Update workspaces.js
  • Loading branch information
arcanis authored Feb 23, 2018
1 parent 4676389 commit 7bf5c16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions __tests__/commands/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ test('workspaces info should list the workspaces', (): Promise<void> => {
expect(reporter.getBufferJson()).toEqual({
'workspace-1': {
location: 'packages/workspace-child-1',
workspaceDependencies: [],
mismatchedWorkspaceDependencies: [],
},
'workspace-2': {
location: 'packages/workspace-child-2',
workspaceDependencies: ['workspace-1'],
mismatchedWorkspaceDependencies: [],
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"prescript": "echo workspace-2 prescript",
"script": "echo workspace-2 script",
"postscript": "echo workspace-2 postscript"
},
"dependencies": {
"workspace-1": "1.0.0"
}
}
27 changes: 26 additions & 1 deletion src/cli/commands/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import type Config from '../../config.js';
import {MessageError} from '../../errors.js';
import type {Reporter} from '../../reporters/index.js';
import buildSubCommands from './_build-sub-commands.js';
import {DEPENDENCY_TYPES} from '../../constants.js';

const invariant = require('invariant');
const path = require('path');
const semver = require('semver');

export function hasWrapper(commander: Object, args: Array<string>): boolean {
return true;
Expand All @@ -27,8 +29,31 @@ export async function info(config: Config, reporter: Reporter, flags: Object, ar
const publicData = {};

for (const workspaceName of Object.keys(workspaces)) {
const {loc, manifest} = workspaces[workspaceName];

const workspaceDependencies = new Set();
const mismatchedWorkspaceDependencies = new Set();

for (const dependencyType of DEPENDENCY_TYPES) {
if (dependencyType !== 'peerDependencies') {
for (const dependencyName of Object.keys(manifest[dependencyType] || {})) {
if (Object.prototype.hasOwnProperty.call(workspaces, dependencyName)) {
invariant(manifest && manifest[dependencyType], 'The request should exist');
const requestedRange = manifest[dependencyType][dependencyName];
if (semver.satisfies(workspaces[dependencyName].manifest.version, requestedRange)) {
workspaceDependencies.add(dependencyName);
} else {
mismatchedWorkspaceDependencies.add(dependencyName);
}
}
}
}
}

publicData[workspaceName] = {
location: path.relative(config.lockfileFolder, workspaces[workspaceName].loc).replace(/\\/g, '/'),
location: path.relative(config.lockfileFolder, loc).replace(/\\/g, '/'),
workspaceDependencies: Array.from(workspaceDependencies),
mismatchedWorkspaceDependencies: Array.from(mismatchedWorkspaceDependencies),
};
}

Expand Down

0 comments on commit 7bf5c16

Please sign in to comment.