Skip to content

Commit

Permalink
Monorepo utils: add support for Yarn Berry
Browse files Browse the repository at this point in the history
Offloading: adeira/universe#2154

adeira-source-id: 578d5959a067fdce88339aee6028d3077802a080
  • Loading branch information
mrtnzlml authored and adeira-github-bot committed Mar 7, 2022
1 parent 4cd7a5a commit c88f362
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
3 changes: 2 additions & 1 deletion 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/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()));
}

0 comments on commit c88f362

Please sign in to comment.