-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds to & from options in link script (#1917)
- Loading branch information
1 parent
c15ee2a
commit 0100840
Showing
10 changed files
with
190 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable no-console */ | ||
import chalk from 'chalk'; | ||
import { spawn } from 'cross-spawn'; | ||
import path from 'path'; | ||
|
||
import { findDirectory } from './findDirectory'; | ||
import { PackageDetails } from './types'; | ||
|
||
/** | ||
* Runs the yarn link command in a leafygreen-ui package directory | ||
* @returns Promise that resolves when the yarn link command has finished | ||
*/ | ||
export function createLinkFrom( | ||
source: string, | ||
{ scopeName, scopePath, packageName }: PackageDetails, | ||
verbose?: boolean, | ||
): Promise<void> { | ||
const scopeSrc = scopePath; | ||
return new Promise<void>(resolve => { | ||
const packagesDirectory = findDirectory(process.cwd(), scopeSrc); | ||
|
||
if (packagesDirectory) { | ||
verbose && | ||
console.log( | ||
'Creating link for:', | ||
chalk.green(`${scopeName}/${packageName}`), | ||
); | ||
spawn('yarn', ['link'], { | ||
cwd: path.join(packagesDirectory, packageName), | ||
stdio: verbose ? 'inherit' : 'ignore', | ||
}) | ||
.on('close', resolve) | ||
.on('error', () => { | ||
throw new Error(`Couldn't create link for package: ${packageName}`); | ||
}); | ||
} else { | ||
throw new Error( | ||
`Can't find a ${scopeSrc} directory in ${process.cwd()} or any of its parent directories.`, | ||
); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import fse from 'fs-extra'; | ||
import { homedir } from 'os'; | ||
import path from 'path'; | ||
|
||
export function findDirectory( | ||
startDir: string, | ||
targetDir: string, | ||
): string | undefined { | ||
const testDir = path.join(startDir, targetDir); | ||
|
||
if (fse.existsSync(testDir) && fse.lstatSync(testDir).isDirectory()) { | ||
return testDir; | ||
} else { | ||
const parentDir = path.join(startDir, '..'); | ||
|
||
// If we haven't reached the users home directory, recursively look for the packages directory | ||
if (parentDir !== homedir()) { | ||
return findDirectory(path.join(startDir, '..'), targetDir); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { spawn } from 'cross-spawn'; | ||
|
||
export function yarnInstall(path: string) { | ||
return new Promise((resolve, reject) => { | ||
spawn('yarn', ['install'], { | ||
cwd: path, | ||
stdio: 'ignore', | ||
}) | ||
.on('close', resolve) | ||
.on('error', () => { | ||
throw new Error(`Error installing packages`); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import chalk from 'chalk'; | ||
import { spawn } from 'cross-spawn'; | ||
|
||
import { PackageDetails } from './types'; | ||
|
||
/** | ||
* Runs the yarn link <packageName> command in the destination directory | ||
* @returns Promise that resolves when the yarn link <packageName> command has finished | ||
*/ | ||
export function linkPackageTo( | ||
destination: string, | ||
{ scopeName, packageName }: Pick<PackageDetails, 'scopeName' | 'packageName'>, | ||
verbose?: boolean, | ||
): Promise<void> { | ||
const fullPackageName = `${scopeName}/${packageName}`; | ||
return new Promise<void>(resolve => { | ||
// eslint-disable-next-line no-console | ||
verbose && console.log('Linking package:', chalk.blue(fullPackageName)); | ||
spawn('yarn', ['link', fullPackageName], { | ||
cwd: destination, | ||
stdio: verbose ? 'inherit' : 'ignore', | ||
}) | ||
.on('close', resolve) | ||
.on('error', () => { | ||
throw new Error(`Couldn't link package: ${fullPackageName}`); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface PackageDetails { | ||
scopeName: string; | ||
packageName: string; | ||
scopePath: string; | ||
} |
Oops, something went wrong.