Skip to content

Commit ac02da2

Browse files
committed
Node-package-manager runner
1 parent 88f75df commit ac02da2

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

packages/cli-kit/src/public/node/node-package-manager.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import {
2121
inferPackageManager,
2222
PackageManager,
2323
npmLockfile,
24+
pnpmLockfile,
25+
yarnLockfile,
26+
bunLockfile,
27+
packageManagerBinaryCommandForDirectory,
2428
} from './node-package-manager.js'
2529
import {captureOutput, exec} from './system.js'
2630
import {inTemporaryDirectory, mkdir, touchFile, writeFile} from './fs.js'
@@ -917,6 +921,87 @@ describe('getPackageManager', () => {
917921
})
918922
})
919923

924+
describe('packageManagerBinaryCommandForDirectory', () => {
925+
test('returns npm exec arguments for npm projects', async () => {
926+
await inTemporaryDirectory(async (tmpDir) => {
927+
await writePackageJSON(tmpDir, {name: 'mock name'})
928+
await writeFile(joinPath(tmpDir, npmLockfile), '')
929+
930+
const command = await packageManagerBinaryCommandForDirectory(
931+
tmpDir,
932+
'graphql-code-generator',
933+
'--config',
934+
'package.json',
935+
)
936+
937+
expect(command).toEqual({
938+
command: 'npm',
939+
args: ['exec', '--', 'graphql-code-generator', '--config', 'package.json'],
940+
})
941+
})
942+
})
943+
944+
test('returns pnpm exec arguments for nested pnpm workspace packages', async () => {
945+
await inTemporaryDirectory(async (tmpDir) => {
946+
await writePackageJSON(tmpDir, {name: 'root'})
947+
await writeFile(joinPath(tmpDir, pnpmLockfile), '')
948+
const nested = joinPath(tmpDir, 'extensions', 'cart-transformer')
949+
await mkdir(nested)
950+
await writePackageJSON(nested, {name: 'cart-transformer'})
951+
952+
const command = await packageManagerBinaryCommandForDirectory(
953+
nested,
954+
'graphql-code-generator',
955+
'--config',
956+
'package.json',
957+
)
958+
959+
expect(command).toEqual({
960+
command: 'pnpm',
961+
args: ['exec', 'graphql-code-generator', '--config', 'package.json'],
962+
})
963+
})
964+
})
965+
966+
test('returns yarn run arguments for yarn projects', async () => {
967+
await inTemporaryDirectory(async (tmpDir) => {
968+
await writePackageJSON(tmpDir, {name: 'mock name'})
969+
await writeFile(joinPath(tmpDir, yarnLockfile), '')
970+
971+
const command = await packageManagerBinaryCommandForDirectory(
972+
tmpDir,
973+
'graphql-code-generator',
974+
'--config',
975+
'package.json',
976+
)
977+
978+
expect(command).toEqual({
979+
command: 'yarn',
980+
args: ['run', 'graphql-code-generator', '--config', 'package.json'],
981+
})
982+
})
983+
})
984+
985+
test('returns bun x arguments for bun projects', async () => {
986+
await inTemporaryDirectory(async (tmpDir) => {
987+
await writePackageJSON(tmpDir, {name: 'mock name'})
988+
await writeFile(joinPath(tmpDir, bunLockfile), '')
989+
990+
const command = await packageManagerBinaryCommandForDirectory(
991+
tmpDir,
992+
'graphql-code-generator',
993+
'--config',
994+
'package.json',
995+
)
996+
997+
expect(command).toEqual({
998+
command: 'bun',
999+
args: ['x', 'graphql-code-generator', '--config', 'package.json'],
1000+
})
1001+
})
1002+
})
1003+
})
1004+
9201005
describe('addNPMDependencies', () => {
9211006
test('when using npm with multiple dependencies they should be installed one by one, adding --save-exact if needed', async () => {
9221007
await inTemporaryDirectory(async (tmpDir) => {

packages/cli-kit/src/public/node/node-package-manager.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ export async function getPackageManager(fromDirectory: string): Promise<PackageM
137137
return 'npm'
138138
}
139139

140+
export async function packageManagerBinaryCommandForDirectory(
141+
directory: string,
142+
binary: string,
143+
...args: string[]
144+
): Promise<{command: string; args: string[]}> {
145+
const packageManager = await getPackageManager(directory)
146+
147+
switch (packageManager) {
148+
case 'npm':
149+
return {command: 'npm', args: ['exec', '--', binary, ...args]}
150+
case 'pnpm':
151+
return {command: 'pnpm', args: ['exec', binary, ...args]}
152+
case 'yarn':
153+
return {command: 'yarn', args: ['run', binary, ...args]}
154+
case 'bun':
155+
return {command: 'bun', args: ['x', binary, ...args]}
156+
case 'homebrew':
157+
case 'unknown':
158+
throw new UnknownPackageManagerError()
159+
}
160+
}
161+
140162
interface InstallNPMDependenciesRecursivelyOptions {
141163
/**
142164
* The dependency manager to use to install the dependencies.

0 commit comments

Comments
 (0)