|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import chalk from 'chalk'; |
| 3 | +import { spawn } from 'child_process'; |
| 4 | +import { Command } from 'commander'; |
| 5 | +import fs from 'fs'; |
| 6 | +import { homedir } from 'os'; |
| 7 | +import path from 'path'; |
| 8 | + |
| 9 | +import { Scope } from './scopes'; |
| 10 | +import { formatLog } from './utils'; |
| 11 | + |
| 12 | +const program = new Command(); |
| 13 | + |
| 14 | +program |
| 15 | + .name('link') |
| 16 | + .description( |
| 17 | + "Link local LeafyGreen packages to a destination app. This is useful for testing changes to a package's source code in another project.", |
| 18 | + ) |
| 19 | + .arguments('destination') |
| 20 | + .option('-v --verbose', 'Prints additional information to the console', false) |
| 21 | + // TODO: Add `scope` option using `.addOption` method |
| 22 | + .action(linkPackages) |
| 23 | + .parse(process.argv); |
| 24 | + |
| 25 | +async function linkPackages( |
| 26 | + destination: string, |
| 27 | + opts: { scope: string; verbose: boolean }, |
| 28 | +) { |
| 29 | + const { verbose } = opts; |
| 30 | + const relativeDestination = path.relative(process.cwd(), destination); |
| 31 | + |
| 32 | + // Check if the destination exists |
| 33 | + if (fs.existsSync(destination) && fs.lstatSync(destination).isDirectory()) { |
| 34 | + console.log( |
| 35 | + chalk.green( |
| 36 | + `Linking packages to ${formatLog.path(relativeDestination)} ...`, |
| 37 | + ), |
| 38 | + ); |
| 39 | + await linkPackageForScope('@leafygreen-ui'); |
| 40 | + await linkPackageForScope('@lg-tools'); |
| 41 | + console.log(chalk.green('Finished linking packages.')); |
| 42 | + } else { |
| 43 | + throw new Error( |
| 44 | + `Can't find the directory ${formatLog.path(relativeDestination)}.`, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + async function linkPackageForScope(scope: keyof typeof Scope) { |
| 49 | + // The directory where the leafygreen-ui packages are installed |
| 50 | + const installedModulesDir = path.join(destination, 'node_modules', scope); |
| 51 | + |
| 52 | + // Check that the destination has leafygreen-ui packages installed |
| 53 | + if (fs.existsSync(installedModulesDir)) { |
| 54 | + // Get a list of all the packages in the destination |
| 55 | + // Run yarn link on each package |
| 56 | + // Run yarn link <packageName> on the destination |
| 57 | + const installedLGPackages = fs.readdirSync(installedModulesDir); |
| 58 | + console.log( |
| 59 | + chalk.gray(` Creating links to ${formatLog.scope(scope)} packages...`), |
| 60 | + ); |
| 61 | + await Promise.all( |
| 62 | + installedLGPackages.map(pkg => { |
| 63 | + createYarnLinkForPackage(scope, pkg); |
| 64 | + }), |
| 65 | + ); |
| 66 | + console.log( |
| 67 | + chalk.gray( |
| 68 | + ` Connecting links for ${formatLog.scope( |
| 69 | + scope, |
| 70 | + )} packages to ${chalk.blue(formatLog.path(relativeDestination))}...`, |
| 71 | + ), |
| 72 | + ); |
| 73 | + await Promise.all( |
| 74 | + installedLGPackages.map((pkg: string) => |
| 75 | + linkPackageToDestination(scope, pkg), |
| 76 | + ), |
| 77 | + ); |
| 78 | + } else { |
| 79 | + console.error( |
| 80 | + chalk.gray( |
| 81 | + ` Couldn't find ${formatLog.scope( |
| 82 | + scope, |
| 83 | + )} scoped packages installed at ${chalk.blue( |
| 84 | + formatLog.path(relativeDestination), |
| 85 | + )}. Skipping.`, |
| 86 | + ), |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Runs the yarn link command in a leafygreen-ui package directory |
| 93 | + * @returns Promise that resolves when the yarn link command has finished |
| 94 | + */ |
| 95 | + function createYarnLinkForPackage( |
| 96 | + scope: keyof typeof Scope, |
| 97 | + packageName: string, |
| 98 | + ): Promise<void> { |
| 99 | + const scopeSrc = Scope[scope]; |
| 100 | + return new Promise<void>(resolve => { |
| 101 | + const packagesDirectory = findDirectory(process.cwd(), scopeSrc); |
| 102 | + |
| 103 | + if (packagesDirectory) { |
| 104 | + verbose && |
| 105 | + console.log( |
| 106 | + 'Creating link for:', |
| 107 | + chalk.green(`${scope}/${packageName}`), |
| 108 | + ); |
| 109 | + spawn('yarn', ['link'], { |
| 110 | + cwd: path.join(packagesDirectory, packageName), |
| 111 | + stdio: verbose ? 'inherit' : 'ignore', |
| 112 | + }) |
| 113 | + .on('close', resolve) |
| 114 | + .on('error', () => { |
| 115 | + throw new Error(`Couldn't create link for package: ${packageName}`); |
| 116 | + }); |
| 117 | + } else { |
| 118 | + throw new Error( |
| 119 | + `Can't find a ${scopeSrc} directory in ${process.cwd()} or any of its parent directories.`, |
| 120 | + ); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Runs the yarn link <packageName> command in the destination directory |
| 127 | + * @returns Promise that resolves when the yarn link <packageName> command has finished |
| 128 | + */ |
| 129 | + function linkPackageToDestination( |
| 130 | + scope: keyof typeof Scope, |
| 131 | + packageName: string, |
| 132 | + ): Promise<void> { |
| 133 | + const fullPackageName = `${scope}/${packageName}`; |
| 134 | + return new Promise<void>(resolve => { |
| 135 | + verbose && console.log('Linking package:', chalk.blue(fullPackageName)); |
| 136 | + spawn('yarn', ['link', fullPackageName], { |
| 137 | + cwd: destination, |
| 138 | + stdio: verbose ? 'inherit' : 'ignore', |
| 139 | + }) |
| 140 | + .on('close', resolve) |
| 141 | + .on('error', () => { |
| 142 | + throw new Error(`Couldn't link package: ${fullPackageName}`); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function findDirectory( |
| 149 | + startDir: string, |
| 150 | + targetDir: string, |
| 151 | +): string | undefined { |
| 152 | + const testDir = path.join(startDir, targetDir); |
| 153 | + |
| 154 | + if (fs.existsSync(testDir) && fs.lstatSync(testDir).isDirectory()) { |
| 155 | + return testDir; |
| 156 | + } else { |
| 157 | + const parentDir = path.join(startDir, '..'); |
| 158 | + |
| 159 | + // If we haven't reached the users home directory, recursively look for the packages directory |
| 160 | + if (parentDir !== homedir()) { |
| 161 | + return findDirectory(path.join(startDir, '..'), targetDir); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments