Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monorepo utils: add support for Yarn Berry #4045

Merged
merged 2 commits into from
Mar 7, 2022
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
3 changes: 2 additions & 1 deletion src/monorepo-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@babel/register": "^7.17.0",
"@babel/runtime": "^7.17.2",
"glob": "^7.2.0",
"is-ci": "^3.0.1"
"is-ci": "^3.0.1",
"semver": "^7.3.5"
}
}
48 changes: 37 additions & 11 deletions src/monorepo-utils/src/getWorkspaceDependencies.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// @flow strict
// @flow

import os from 'os';
import semver from 'semver';

import sanitizeWorkspaces from './sanitizeWorkspaces';
import ShellCommand from './ShellCommand';
Expand All @@ -8,19 +11,42 @@ import type { WorkspaceDependencies } from './Workspaces.flow';
* This function returns the workspace dependencies
*/
export default function getWorkspaceDependencies(): WorkspaceDependencies {
const stdout = new ShellCommand(null, 'yarn', '--json', 'workspaces', 'info')
.runSynchronously()
.getStdout();
const versionStdout = new ShellCommand(null, 'yarn', '--version').runSynchronously().getStdout();

// branch for Yarn Berry (v2+):
if (semver.gte(versionStdout, '2.0.0')) {
const sanitizeStdout = (stdout) => {
const workspaces = {};
const rows = stdout.split(os.EOL);
for (const row of rows) {
const parsedRow = JSON.parse(row);
if (parsedRow.name) {
workspaces[parsedRow.name] = {
location: parsedRow.location,
workspaceDependencies: parsedRow.workspaceDependencies,
mismatchedWorkspaceDependencies: parsedRow.mismatchedWorkspaceDependencies,
};
}
}
return workspaces;
};

const stdout = new ShellCommand(null, 'yarn', 'workspaces', 'list', '--verbose', '--json')
.runSynchronously()
.getStdout();

const sanitizeStdout = () => {
return sanitizeWorkspaces(sanitizeStdout(stdout.trim()));
}

// Yarn Classic (v1.*):
const sanitizeStdout = (stdout) => {
const data = JSON.parse(stdout);
if (data.data === undefined) {
// yarn updated how they return data from yarn workspaces info --json, this is to support 1.22.0
return data;
}
// This is how the data has to be parsed prior to 1.22.0
return JSON.parse(data.data);
};

return sanitizeWorkspaces(sanitizeStdout());
const stdout = new ShellCommand(null, 'yarn', '--json', 'workspaces', 'info')
.runSynchronously()
.getStdout();

return sanitizeWorkspaces(sanitizeStdout(stdout.trim()));
}