| 430 | } |
| 431 | |
| 432 | function workspaceMock (t, opts) { |
| 433 | const toObject = [(a, c) => ({ ...a, ...c }), {}] |
| 434 | const { workspaces: workspacesDef, ...rest } = { clean: true, ...opts } |
| 435 | const workspaces = Object.fromEntries(Object.entries(workspacesDef).map(([name, ws]) => { |
| 436 | return [name, Object.fromEntries(Object.entries(ws).map(([wsPackageDep, wsPackageDepOpts]) => { |
| 437 | return [wsPackageDep, { ...rest, ...wsPackageDepOpts }] |
| 438 | }))] |
| 439 | })) |
| 440 | const root = 'workspace-root' |
| 441 | const version = '1.0.0' |
| 442 | const names = Object.keys(workspaces) |
| 443 | const ws = Object.entries(workspaces).map(([name, _ws]) => dependencyDetails(name, { ws: _ws })) |
| 444 | const deps = ws.map(({ depsMap }) => depsMap).flat() |
| 445 | const tarballs = deps.map(w => w.tarballDir).reduce(...toObject) |
| 446 | const symlinks = names |
| 447 | .map((name) => ({ [name]: t.fixture('symlink', `../${name}`) })).reduce(...toObject) |
| 448 | const hoisted = deps.filter(d => d.hoist).map(w => w.dirtyOrCleanDir).reduce(...toObject) |
| 449 | const workspaceFolders = ws.map(w => w.packageDir).reduce(...toObject) |
| 450 | const packageJSON = { name: root, version, workspaces: names } |
| 451 | const packageLockJSON = ({ |
| 452 | name: root, |
| 453 | version, |
| 454 | lockfileVersion: 3, |
| 455 | requires: true, |
| 456 | packages: { |
| 457 | '': { name: root, version, workspaces: names }, |
| 458 | ...deps.filter(d => d.hoist).map(d => d.packageLockEntry).reduce(...toObject), |
| 459 | ...ws.map(d => d.packageLockEntry).flat().reduce(...toObject), |
| 460 | ...ws.map(d => d.packageLockLink).flat().reduce(...toObject), |
| 461 | ...ws.map(d => d.packageLockLocal).flat().reduce(...toObject), |
| 462 | ...deps.filter(d => !d.hoist).map(d => d.packageLockEntry).reduce(...toObject), |
| 463 | }, |
| 464 | }) |
| 465 | return { |
| 466 | tarballs, |
| 467 | node_modules: { |
| 468 | ...hoisted, |
| 469 | ...symlinks, |
| 470 | }, |
| 471 | 'package-lock.json': JSON.stringify(packageLockJSON), |
| 472 | 'package.json': JSON.stringify(packageJSON), |
| 473 | ...workspaceFolders, |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | module.exports = setupMockNpm |
| 478 | module.exports.load = setupMockNpm |