fixture-dependencies.mjs 918 B

123456789101112131415161718192021
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. // A junction around pnpm's whole node_modules breaks its relative links.
  4. // Resolve each package first; Windows junctions then need no symlink privilege.
  5. export function linkFixtureDependencies(packageRoot, fixtureRoot) {
  6. const source = path.join(packageRoot, 'node_modules')
  7. const destination = path.join(fixtureRoot, 'node_modules')
  8. fs.mkdirSync(destination)
  9. const link = name => {
  10. const target = fs.realpathSync.native(path.join(source, name))
  11. fs.symlinkSync(target, path.join(destination, name), process.platform === 'win32' ? 'junction' : 'dir')
  12. }
  13. for (const name of fs.readdirSync(source)) {
  14. if (name.startsWith('.')) continue
  15. if (name.startsWith('@')) {
  16. fs.mkdirSync(path.join(destination, name))
  17. for (const child of fs.readdirSync(path.join(source, name))) link(path.join(name, child))
  18. } else link(name)
  19. }
  20. }