diff --git a/package.json b/package.json index 805a6a0..bcf4dcd 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/getWorkspaceDependencies.js b/src/getWorkspaceDependencies.js index 4af5546..5d663ec 100644 --- a/src/getWorkspaceDependencies.js +++ b/src/getWorkspaceDependencies.js @@ -1,4 +1,7 @@ -// @flow strict +// @flow + +import os from 'os'; +import semver from 'semver'; import sanitizeWorkspaces from './sanitizeWorkspaces'; import ShellCommand from './ShellCommand'; @@ -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())); }