|
| 1 | +import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { findRootDirAndLockFiles } from './find-root' |
| 5 | + |
| 6 | +describe('findRootDirAndLockFiles()', () => { |
| 7 | + it('ignores stray parent lockfiles without a package manifest', async () => { |
| 8 | + const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-')) |
| 9 | + |
| 10 | + try { |
| 11 | + const appDir = join(rootDir, 'app') |
| 12 | + const parentLockfile = join(rootDir, 'package-lock.json') |
| 13 | + const appLockfile = join(appDir, 'package-lock.json') |
| 14 | + |
| 15 | + await mkdir(appDir) |
| 16 | + await writeFile(parentLockfile, '{}') |
| 17 | + await writeFile(join(appDir, 'package.json'), '{}') |
| 18 | + await writeFile(appLockfile, '{}') |
| 19 | + |
| 20 | + const result = findRootDirAndLockFiles(appDir) |
| 21 | + |
| 22 | + expect(result.rootDir).toBe(appDir) |
| 23 | + expect(result.lockFiles).toEqual([appLockfile]) |
| 24 | + } finally { |
| 25 | + await rm(rootDir, { recursive: true, force: true }) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + it('ignores parent package roots that do not declare workspaces', async () => { |
| 30 | + const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-')) |
| 31 | + |
| 32 | + try { |
| 33 | + const appDir = join(rootDir, 'app') |
| 34 | + const parentLockfile = join(rootDir, 'bun.lock') |
| 35 | + const appLockfile = join(appDir, 'bun.lock') |
| 36 | + |
| 37 | + await mkdir(appDir) |
| 38 | + await writeFile(join(rootDir, 'package.json'), '{}') |
| 39 | + await writeFile(parentLockfile, '') |
| 40 | + await writeFile(join(appDir, 'package.json'), '{}') |
| 41 | + await writeFile(appLockfile, '') |
| 42 | + |
| 43 | + const result = findRootDirAndLockFiles(appDir) |
| 44 | + |
| 45 | + expect(result.rootDir).toBe(appDir) |
| 46 | + expect(result.lockFiles).toEqual([appLockfile]) |
| 47 | + } finally { |
| 48 | + await rm(rootDir, { recursive: true, force: true }) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + it('keeps higher package roots when they declare workspaces', async () => { |
| 53 | + const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-')) |
| 54 | + |
| 55 | + try { |
| 56 | + const appDir = join(rootDir, 'apps', 'docs') |
| 57 | + const parentLockfile = join(rootDir, 'package-lock.json') |
| 58 | + const appLockfile = join(appDir, 'package-lock.json') |
| 59 | + |
| 60 | + await mkdir(appDir, { recursive: true }) |
| 61 | + await writeFile( |
| 62 | + join(rootDir, 'package.json'), |
| 63 | + JSON.stringify({ workspaces: ['apps/*'] }) |
| 64 | + ) |
| 65 | + await writeFile(parentLockfile, '{}') |
| 66 | + await writeFile(join(appDir, 'package.json'), '{}') |
| 67 | + await writeFile(appLockfile, '{}') |
| 68 | + |
| 69 | + const result = findRootDirAndLockFiles(appDir) |
| 70 | + |
| 71 | + expect(result.rootDir).toBe(rootDir) |
| 72 | + expect(result.lockFiles).toEqual([appLockfile, parentLockfile]) |
| 73 | + } finally { |
| 74 | + await rm(rootDir, { recursive: true, force: true }) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + it('keeps higher pnpm workspace roots ahead of nested lockfiles', async () => { |
| 79 | + const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-')) |
| 80 | + |
| 81 | + try { |
| 82 | + const appDir = join(rootDir, 'apps', 'docs') |
| 83 | + const workspaceFile = join(rootDir, 'pnpm-workspace.yaml') |
| 84 | + |
| 85 | + await mkdir(appDir, { recursive: true }) |
| 86 | + await writeFile(workspaceFile, 'packages:\n - apps/*\n') |
| 87 | + await writeFile(join(appDir, 'package.json'), '{}') |
| 88 | + await writeFile(join(appDir, 'pnpm-lock.yaml'), '') |
| 89 | + |
| 90 | + const result = findRootDirAndLockFiles(appDir) |
| 91 | + |
| 92 | + expect(result.rootDir).toBe(rootDir) |
| 93 | + expect(result.lockFiles).toEqual([workspaceFile]) |
| 94 | + } finally { |
| 95 | + await rm(rootDir, { recursive: true, force: true }) |
| 96 | + } |
| 97 | + }) |
| 98 | +}) |
0 commit comments