import { spawnSync } from "node:child_process" import { readdirSync, statSync } from "node:fs" import { join } from "node:path" // Get the absolute path to the packages directory const packagesDir = join(process.cwd(), "packages") const linkCommand = process.argv[2] ?? "link" const linkDir = process.argv[3] ?? "../investtal" // Function to run the bun link command in a specific directory function runBunLink(packagePath: string, packageName: string) { console.log(`Linking package: ${packageName}...`) const result = spawnSync("pnpm", [linkCommand, "--dir", linkDir], { cwd: packagePath, stdio: "inherit", shell: true, }) if (result.status === 0) { console.log(`✅ Successfully ${linkCommand} ${packageName}`) } else { console.error(`❌ Failed to ${linkCommand} ${packageName}`) } } const ignorePackages = ["design-tokens", "documents"] // Main function to process all packages function linkAllPackages() { console.log(`Looking for packages in: ${packagesDir}`) try { // Get all directories in the packages folder const packageFolders = readdirSync(packagesDir).filter(item => { const itemPath = join(packagesDir, item) return statSync(itemPath).isDirectory() && !ignorePackages.includes(item) }) if (packageFolders.length === 0) { console.log("No packages found.") return } console.log(`Found ${packageFolders.length} packages to link.`) // Run bun link for each package for (const packageFolder of packageFolders) { const packagePath = join(packagesDir, packageFolder) runBunLink(packagePath, packageFolder) } console.log("All packages have been processed.") } catch (error) { console.error("Error linking packages:", error) process.exit(1) } } // Execute the main function linkAllPackages()