-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getCurrentPackageManager (#9505)
Co-authored-by: Niklas Mischkulnig <[email protected]>
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/core/package-manager/src/getCurrentPackageManager.js
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,17 @@ | ||
// @flow | ||
|
||
export default function getCurrentPackageManager( | ||
userAgent: ?string = process.env.npm_config_user_agent, | ||
): ?{|name: string, version: string|} { | ||
if (!userAgent) { | ||
return undefined; | ||
} | ||
|
||
const pmSpec = userAgent.split(' ')[0]; | ||
const separatorPos = pmSpec.lastIndexOf('/'); | ||
const name = pmSpec.substring(0, separatorPos); | ||
return { | ||
name: name, | ||
version: pmSpec.substring(separatorPos + 1), | ||
}; | ||
} |
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
28 changes: 28 additions & 0 deletions
28
packages/core/package-manager/test/getCurrentPackageManager.test.js
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 @@ | ||
// @flow | ||
import assert from 'assert'; | ||
import getCurrentPackageManager from '../src/getCurrentPackageManager'; | ||
|
||
describe('getCurrentPackageManager', () => { | ||
it('yarn', () => { | ||
const npm_config_user_agent = 'yarn/1.22.21 npm/? node/v21.1.0 darwin x64'; | ||
const currentPackageManager = getCurrentPackageManager( | ||
npm_config_user_agent, | ||
); | ||
assert(currentPackageManager?.name, 'yarn'); | ||
}); | ||
it('npm', () => { | ||
const npm_config_user_agent = | ||
'npm/10.2.0 node/v21.1.0 darwin x64 workspaces/true'; | ||
const currentPackageManager = getCurrentPackageManager( | ||
npm_config_user_agent, | ||
); | ||
assert(currentPackageManager?.name, 'npm'); | ||
}); | ||
it('pnpm', () => { | ||
const npm_config_user_agent = 'pnpm/8.14.2 npm/? node/v18.17.1 darwin x64'; | ||
const currentPackageManager = getCurrentPackageManager( | ||
npm_config_user_agent, | ||
); | ||
assert(currentPackageManager?.name, 'pnpm'); | ||
}); | ||
}); |