Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect running package manager #120

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export function objectWithoutKeys<T extends object = {}>(obj: T, ...keys: (keyof
* Detects the package manager used in the given directory.
*/
export async function detectNodePackageManager(directory: string) {
const packageManager = process.env.npm_config_user_agent?.split('/')[0]
if (packageManager && ['npm', 'yarn', 'pnpm'].includes(packageManager)) {
debug.utils(`Detected running package manager: ${packageManager}`)
return packageManager
}

const packageLockFiles: Record<string, NodePackageManager> = {
'pnpm-lock.yaml': 'pnpm',
'yarn.lock': 'yarn',
Expand Down
155 changes: 155 additions & 0 deletions packages/core/test/actions/install-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ it('installs the given php package as development dependencies', async() => awai

it('installs the given node package with npm by default', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = undefined
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
Expand All @@ -87,6 +88,91 @@ it('installs the given node package with npm by default', async() => await using
targetStructure: { 'package.json': { type: 'file', content: '{}' } },
}))

it('installs the given node package with npm when npm was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'npm'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
packages: 'debug@4.3.3',
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'package-lock.json': { type: 'file' },
'package.json': {
type: 'file',
json: {
dependencies: {
debug: '^4.3.3',
},
},
},
})
},
targetStructure: { 'package.json': { type: 'file', content: '{}' } },
}))

it('installs the given node package with yarn when yarn was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'yarn'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
packages: 'debug@^4.3.4',
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'yarn.lock': { type: 'file' },
'package.json': {
type: 'file',
json: {
dependencies: {
debug: '^4.3.4',
},
},
},
})
},
targetStructure: { 'package.json': { type: 'file', content: '{}' } },
}))

it('installs the given node package with pnpm when pnpm was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'pnpm'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
packages: 'debug@^4.3.4',
additionalArgs: ['--ignore-workspace'],
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'pnpm-lock.yaml': { type: 'file' },
'package.json': {
type: 'file',
json: {
dependencies: {
debug: '^4.3.4',
},
},
},
})
},
targetStructure: { 'package.json': { type: 'file', content: '{}' } },
}))

it('installs the given node package as development dependencies', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
const { executePreset } = await makeTestPreset({
Expand Down Expand Up @@ -123,6 +209,7 @@ it('installs the given node package with the specified package manager', async()
for: 'node',
packages: 'debug@4.3.3',
packageManager: 'pnpm',
additionalArgs: ['--ignore-workspace'],
}),
})

Expand Down Expand Up @@ -175,6 +262,7 @@ it('installs the given packages at once', async() => await usingSandbox({

it('installs packages already present in package.json with npm', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = undefined
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages(),
})
Expand Down Expand Up @@ -279,3 +367,70 @@ it('installs existing packages with yarn', async() => await usingSandbox({
'package.json': { type: 'file', content: '{"dependencies": {"debug": "^4.3.4"}}' },
},
}))

it('installs existing packages with npm when npm was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'npm'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'package-lock.json': { type: 'file' },
})
},
targetStructure: {
...emptyPackageJsonStructure,
'package.json': { type: 'file', content: '{"dependencies": {"debug": "^4.3.4"}}' },
},
}))

it('installs existing packages with yarn when yarn was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'yarn'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'yarn.lock': { type: 'file' },
})
},
targetStructure: {
...emptyPackageJsonStructure,
'package.json': { type: 'file', content: '{"dependencies": {"debug": "^4.3.4"}}' },
},
}))

it('installs existing packages with pnpm when pnpm was used to run the preset', async() => await usingSandbox({
fn: async({ targetDirectory }, makeTestPreset) => {
process.env.npm_config_user_agent = 'pnpm'
const { executePreset } = await makeTestPreset({
handler: async() => await installPackages({
for: 'node',
additionalArgs: ['--ignore-workspace'],
}),
})

await executePreset()
await expectStructureMatches(targetDirectory, {
'node_modules': { type: 'directory' },
'node_modules/debug': { type: 'directory' },
'pnpm-lock.yaml': { type: 'file' },
})
},
targetStructure: {
...emptyPackageJsonStructure,
'package.json': { type: 'file', content: '{"dependencies": {"debug": "^4.3.4"}}' },
},
}))