-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds multiple package managers to
link
script (#2167)
* run link based on destination package manager * Create getPackageManager.spec.ts * adds tests for `installPackages` * add tests for createLinkFrom * add tests for linkPackageTo * add tests for linkPackagesForScope * lint * rm old code * adds changesets --------- Co-authored-by: Brooke Scarlett Yalof <[email protected]>
- Loading branch information
1 parent
c465fff
commit 90bba7b
Showing
15 changed files
with
455 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lg-tools/meta': minor | ||
--- | ||
|
||
Adds `pnpm` as a possible `getPackageManager` return value. |
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 @@ | ||
--- | ||
'@lg-tools/link': minor | ||
--- | ||
|
||
Previously, `lg link` would only install & link packages using `yarn`. Now it will check the destination app's package manager (via lock file) and link packages using the correct package manager. |
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,49 @@ | ||
import { ChildProcess } from 'child_process'; | ||
import xSpawn from 'cross-spawn'; | ||
import fsx from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
import { createLinkFrom } from './createLinkFrom'; | ||
import { MockChildProcess } from './mocks.testutils'; | ||
|
||
describe('tools/link/createLinkFrom', () => { | ||
let spawnSpy: jest.SpyInstance<ChildProcess>; | ||
|
||
beforeAll(() => { | ||
fsx.emptyDirSync('./tmp'); | ||
fsx.rmdirSync('./tmp/'); | ||
fsx.mkdirSync('./tmp/'); | ||
fsx.mkdirSync('./tmp/scope'); | ||
fsx.mkdirSync('./tmp/scope/test-package'); | ||
}); | ||
|
||
beforeEach(() => { | ||
spawnSpy = jest.spyOn(xSpawn, 'spawn'); | ||
spawnSpy.mockImplementation((..._args) => new MockChildProcess()); | ||
}); | ||
|
||
afterEach(() => { | ||
spawnSpy.mockRestore(); | ||
fsx.emptyDirSync('./tmp'); | ||
}); | ||
|
||
afterAll(() => { | ||
fsx.rmdirSync('./tmp/'); | ||
}); | ||
|
||
test('calls `npm link` command from package directory', () => { | ||
createLinkFrom(path.resolve('./tmp/'), { | ||
scopeName: '@example', | ||
scopePath: 'scope', | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(spawnSpy).toHaveBeenCalledWith( | ||
'npm', | ||
['link'], | ||
expect.objectContaining({ | ||
cwd: expect.stringContaining('tmp/scope/test-package'), | ||
}), | ||
); | ||
}); | ||
}); |
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,47 @@ | ||
import { ChildProcess } from 'child_process'; | ||
import xSpawn from 'cross-spawn'; | ||
import fsx from 'fs-extra'; | ||
|
||
import { installPackages } from './install'; | ||
import { MockChildProcess } from './mocks.testutils'; | ||
|
||
describe('tools/link/utils/install', () => { | ||
let spawnSpy: jest.SpyInstance<ChildProcess>; | ||
|
||
beforeAll(() => { | ||
fsx.mkdirSync('./tmp/'); | ||
}); | ||
|
||
beforeEach(() => { | ||
spawnSpy = jest.spyOn(xSpawn, 'spawn'); | ||
spawnSpy.mockImplementation((..._args) => new MockChildProcess()); | ||
}); | ||
|
||
afterEach(() => { | ||
spawnSpy.mockRestore(); | ||
fsx.emptyDirSync('./tmp'); | ||
}); | ||
|
||
afterAll(() => { | ||
fsx.rmdirSync('./tmp/'); | ||
}); | ||
|
||
test('runs `npm install` command', async () => { | ||
await installPackages('./tmp'); | ||
expect(spawnSpy).toHaveBeenCalledWith( | ||
'npm', | ||
['install'], | ||
expect.objectContaining({}), | ||
); | ||
}); | ||
|
||
test('runs install command using local package manager', async () => { | ||
fsx.createFileSync('./tmp/yarn.lock'); | ||
await installPackages('./tmp'); | ||
expect(spawnSpy).toHaveBeenCalledWith( | ||
'yarn', | ||
['install'], | ||
expect.objectContaining({}), | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,30 @@ | ||
import { getPackageManager } from '@lg-tools/meta'; | ||
import { SupportedPackageManager } from '@lg-tools/meta/src/getPackageManager'; | ||
import { spawn } from 'cross-spawn'; | ||
import fsx from 'fs-extra'; | ||
|
||
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`); | ||
}); | ||
export async function installPackages( | ||
path: string, | ||
options?: { | ||
packageManager?: SupportedPackageManager; | ||
verbose?: boolean; | ||
}, | ||
): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
if (fsx.existsSync(path)) { | ||
const pkgMgr = options?.packageManager ?? getPackageManager(path); | ||
|
||
spawn(pkgMgr, ['install'], { | ||
cwd: path, | ||
stdio: options?.verbose ? 'inherit' : 'ignore', | ||
}) | ||
.on('close', resolve) | ||
.on('error', err => { | ||
throw new Error(`Error installing packages\n` + err); | ||
}); | ||
} else { | ||
console.error(`Path ${path} does not exist`); | ||
reject(); | ||
} | ||
}); | ||
} |
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,47 @@ | ||
import { ChildProcess } from 'child_process'; | ||
import xSpawn from 'cross-spawn'; | ||
import fsx from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
import { linkPackageTo } from './linkPackageTo'; | ||
import { MockChildProcess } from './mocks.testutils'; | ||
|
||
describe('tools/link/linkPackageTo', () => { | ||
let spawnSpy: jest.SpyInstance<ChildProcess>; | ||
|
||
beforeAll(() => { | ||
fsx.emptyDirSync('./tmp'); | ||
fsx.rmdirSync('./tmp/'); | ||
fsx.mkdirSync('./tmp/'); | ||
fsx.mkdirSync('./tmp/app'); | ||
}); | ||
|
||
beforeEach(() => { | ||
spawnSpy = jest.spyOn(xSpawn, 'spawn'); | ||
spawnSpy.mockImplementation((..._args) => new MockChildProcess()); | ||
}); | ||
|
||
afterEach(() => { | ||
spawnSpy.mockRestore(); | ||
fsx.emptyDirSync('./tmp'); | ||
}); | ||
|
||
afterAll(() => { | ||
fsx.rmdirSync('./tmp/'); | ||
}); | ||
|
||
test('calls `npm link <package>` from the destination directory', () => { | ||
linkPackageTo(path.resolve('./tmp/app'), { | ||
scopeName: '@example', | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(spawnSpy).toHaveBeenCalledWith( | ||
'npm', | ||
['link', '@example/test-package'], | ||
expect.objectContaining({ | ||
cwd: expect.stringContaining('tmp/app'), | ||
}), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.