11import { PackageManager } from './package-manager-config' ;
22import { hasScript } from './package-json' ;
3+ import { INSTALL_COMMANDS , UNINSTALL_COMMANDS , UPDATE_COMMANDS , NPM_BUILTIN_COMMANDS } from './command-constants' ;
4+
5+ // Helper to create mappings for all aliases
6+ function createMappings ( aliases : readonly string [ ] , mappings : Partial < Record < PackageManager , [ string , ...string [ ] ] > > ) {
7+ const result : Record < string , Partial < Record < PackageManager , [ string , ...string [ ] ] > > > = { } ;
8+ for ( const alias of aliases ) {
9+ result [ alias ] = mappings ;
10+ }
11+ return result ;
12+ }
13+
14+ // Command mappings - what differs between package managers
15+ const installMappings = createMappings (
16+ INSTALL_COMMANDS ,
17+ { yarn : [ 'add' ] , pnpm : [ 'add' ] , bun : [ 'add' ] }
18+ ) ;
19+
20+ const uninstallMappings = createMappings (
21+ UNINSTALL_COMMANDS ,
22+ { yarn : [ 'remove' ] , pnpm : [ 'remove' ] , bun : [ 'remove' ] }
23+ ) ;
24+
25+ const updateMappings = createMappings (
26+ UPDATE_COMMANDS ,
27+ { yarn : [ 'update' ] , bun : [ 'update' ] }
28+ ) ;
329
4- // Simplified command mappings - only what differs between package managers
530const commandMappings : Record < string , Partial < Record < PackageManager , [ string , ...string [ ] ] > > > = {
6- // Install/add commands
7- 'install' : { yarn : [ 'add' ] , pnpm : [ 'add' ] , bun : [ 'add' ] } ,
8- 'i' : { yarn : [ 'add' ] , pnpm : [ 'add' ] , bun : [ 'add' ] } ,
9-
10- // Remove commands
11- 'uninstall' : { yarn : [ 'remove' ] , pnpm : [ 'remove' ] , bun : [ 'remove' ] } ,
12- 'rm' : { yarn : [ 'remove' ] , pnpm : [ 'remove' ] , bun : [ 'remove' ] } ,
13-
14- // Update commands
15- 'upgrade' : { yarn : [ 'update' ] , bun : [ 'update' ] } ,
16- 'up' : { yarn : [ 'update' ] , bun : [ 'update' ] } ,
17-
31+ ...installMappings ,
32+ ...uninstallMappings ,
33+ ...updateMappings ,
1834 // Execute commands
1935 'exec' : { bun : [ "run" ] } ,
2036} ;
2137
22- // Known npm commands that shouldn't be treated as scripts
23- const npmBuiltinCommands = new Set ( [
24- 'access' , 'adduser' , 'audit' , 'bin' , 'bugs' , 'cache' , 'ci' , 'completion' ,
25- 'config' , 'dedupe' , 'deprecate' , 'diff' , 'dist-tag' , 'docs' , 'doctor' ,
26- 'edit' , 'exec' , 'explain' , 'explore' , 'fund' , 'help' , 'hook' , 'init' ,
27- 'install' , 'install-ci-test' , 'install-test' , 'link' , 'll' , 'login' ,
28- 'logout' , 'ls' , 'org' , 'outdated' , 'owner' , 'pack' , 'ping' , 'pkg' ,
29- 'prefix' , 'profile' , 'prune' , 'publish' , 'query' , 'rebuild' , 'repo' ,
30- 'restart' , 'root' , 'run' , 'run-script' , 'search' , 'set' , 'shrinkwrap' ,
31- 'star' , 'stars' , 'start' , 'stop' , 'team' , 'test' , 'token' , 'uninstall' ,
32- 'unpublish' , 'unstar' , 'update' , 'version' , 'view' , 'whoami' , 'i'
33- ] ) ;
3438
3539export function mapCommand ( command : string , args : string [ ] , packageManager : PackageManager , projectRoot ?: string ) : { command : string ; args : string [ ] } {
3640 // Special case: install without args always uses 'install'
37- if ( ( command === 'install' || command === 'i' ) && args . length === 0 ) {
41+ if ( INSTALL_COMMANDS . includes ( command as any ) && args . length === 0 ) {
3842 return { command : 'install' , args : [ ] } ;
3943 }
4044
4145 // Only map dev flags for install/add commands
42- const isInstallCommand = [ 'install' , 'i' , 'add' ] . includes ( command ) ;
46+ const isInstallCommand = INSTALL_COMMANDS . includes ( command as any ) ;
4347 const mappedArgs = isInstallCommand ? args . map ( arg => {
4448 // Map all dev flags to package manager specific format
4549 if ( arg === '-D' || arg === '--save-dev' || arg === '--dev' ) {
@@ -57,7 +61,7 @@ export function mapCommand(command: string, args: string[], packageManager: Pack
5761 }
5862
5963 // npm needs 'run' prefix for scripts not in built-in commands
60- if ( packageManager === 'npm' && ! npmBuiltinCommands . has ( command ) && hasScript ( command , projectRoot ) ) {
64+ if ( packageManager === 'npm' && ! NPM_BUILTIN_COMMANDS . has ( command ) && projectRoot && hasScript ( command , projectRoot ) ) {
6165 return { command : 'run' , args : [ command , ...args ] } ;
6266 }
6367
0 commit comments