|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const LOCAL_NTFS_REQUIREMENT = |
| 6 | + "Windows source checkouts must be on a local NTFS volume. pnpm workspace links require NTFS junctions; FAT32/exFAT volumes and network shares are not supported. Keep cloud-synced folders outside the checkout."; |
| 7 | + |
| 8 | +function fail(reason) { |
| 9 | + return { ok: false, reason: `${LOCAL_NTFS_REQUIREMENT} ${reason}` }; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Validate the Windows volume before pnpm attempts to create workspace links. |
| 14 | + * |
| 15 | + * The function is exported so the platform-specific policy can be tested without |
| 16 | + * requiring a Windows host. Non-Windows platforms are intentionally a no-op. |
| 17 | + */ |
| 18 | +export function checkWindowsSourceLocation({ |
| 19 | + platform = process.platform, |
| 20 | + cwd = process.cwd(), |
| 21 | + execFile = execFileSync, |
| 22 | + allowNonFixed = process.env.GITHUB_ACTIONS === "true", |
| 23 | +} = {}) { |
| 24 | + if (platform !== "win32") return { ok: true, skipped: true }; |
| 25 | + |
| 26 | + const pathApi = platform === "win32" ? path.win32 : path; |
| 27 | + const root = pathApi.parse(pathApi.resolve(cwd)).root; |
| 28 | + if (!/^[a-z]:\\$/iu.test(root) || root.startsWith("\\\\")) { |
| 29 | + return fail("The checkout root is not a local drive-letter path."); |
| 30 | + } |
| 31 | + // `fsutil` accepts a drive letter more reliably than a root path with a |
| 32 | + // trailing backslash across Windows runner images. |
| 33 | + const volume = root.slice(0, 2); |
| 34 | + |
| 35 | + let driveType; |
| 36 | + let volumeInfo; |
| 37 | + try { |
| 38 | + driveType = execFile("fsutil", ["fsinfo", "drivetype", volume], { |
| 39 | + encoding: "utf8", |
| 40 | + stdio: ["ignore", "pipe", "pipe"], |
| 41 | + }); |
| 42 | + volumeInfo = execFile("fsutil", ["fsinfo", "volumeinfo", volume], { |
| 43 | + encoding: "utf8", |
| 44 | + stdio: ["ignore", "pipe", "pipe"], |
| 45 | + }); |
| 46 | + } catch (error) { |
| 47 | + const detail = error instanceof Error ? ` (${error.message})` : ""; |
| 48 | + return fail(`Windows could not verify the checkout volume${detail}.`); |
| 49 | + } |
| 50 | + |
| 51 | + if (!allowNonFixed && !/:\s*DRIVE_FIXED(?:\r?\n|$)/iu.test(driveType)) { |
| 52 | + return fail("The checkout volume is not a local fixed drive."); |
| 53 | + } |
| 54 | + if (!/:\s*NTFS(?:\r?\n|$)/iu.test(volumeInfo)) { |
| 55 | + return fail("The checkout volume is not formatted as NTFS."); |
| 56 | + } |
| 57 | + |
| 58 | + return { ok: true, skipped: false }; |
| 59 | +} |
| 60 | + |
| 61 | +export function runWindowsSourceLocationCheck({ |
| 62 | + platform = process.platform, |
| 63 | + cwd = process.cwd(), |
| 64 | + execFile = execFileSync, |
| 65 | + allowNonFixed = process.env.GITHUB_ACTIONS === "true", |
| 66 | + report = (message) => console.error(`[source-check] ${message}`), |
| 67 | +} = {}) { |
| 68 | + const result = checkWindowsSourceLocation({ platform, cwd, execFile, allowNonFixed }); |
| 69 | + if (!result.ok) report(result.reason); |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
| 73 | +const scriptPath = process.argv[1] ? path.resolve(process.argv[1]) : ""; |
| 74 | +if (scriptPath === fileURLToPath(import.meta.url)) { |
| 75 | + const result = runWindowsSourceLocationCheck(); |
| 76 | + if (!result.ok) process.exitCode = 1; |
| 77 | +} |
0 commit comments