• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nebrelbug / npm-to-yarn / 6100770941

06 Sep 2023 06:06PM UTC coverage: 98.294% (-0.3%) from 98.555%
6100770941

push

github

web-flow
Merge pull request #42 from colinhacks/colinhacks/add-bun

173 of 178 branches covered (0.0%)

Branch coverage included in aggregate %.

68 of 68 new or added lines in 2 files covered. (100.0%)

288 of 291 relevant lines covered (98.97%)

406.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.46
/src/npmToBun.ts
1
import { parse } from './command'
3✔
2

3
function convertInstallArgs (args: string[]) {
4
  // bun uses -g and --global flags
5
  // bun mostly conforms to Yarn's CLI
6
  return args.map(item => {
120✔
7
    switch (item) {
291✔
8
      case '--save-dev':
9
      case '--development':
10
      case '-D':
11
        return '--dev'
9✔
12
      case '--save-prod':
13
      case '-P':
14
        return '--production'
6✔
15
      case '--no-package-lock':
16
        return '--no-save'
3✔
17
      case '--save-optional':
18
      case '-O':
19
        return '--optional'
6✔
20
      case '--save-exact':
21
      case '-E':
22
        return '--exact'
6✔
23
      case '--save':
24
      case '-S':
25
        // this is default in bun
26
        return ''
6✔
27
      case '--global':
28
      case '-g':
29
        return '--global'
12✔
30
      default:
31
        return item
243✔
32
    }
33
  })
34
}
35

36
export const bunCLICommands = [
3✔
37
  'init',
38
  'run',
39
  'add',
40
  'pm',
41
  'help',
42
  'install',
43
  'remove',
44
  'upgrade',
45
  'version'
46
] as const
47
type bunCLICommands = typeof bunCLICommands[number]
48

49
export function npmToBun (_m: string, command: string): string {
3✔
50
  let args = parse((command || '').trim())
261!
51

52
  const index = args.findIndex(a => a === '--')
546✔
53
  if (index >= 0) {
261✔
54
    args.splice(index, 1)
15✔
55
  }
56

57
  let cmd = 'bun'
261✔
58
  switch (args[0]) {
261✔
59
    case 'install':
60
    case 'i':
61
      if (args.length === 1) {
57✔
62
        args = ['install']
6✔
63
      } else {
64
        args[0] = 'add'
51✔
65
      }
66
      args = convertInstallArgs(args)
57✔
67
      break
57✔
68
    case 'uninstall':
69
    case 'un':
70
    case 'remove':
71
    case 'r':
72
    case 'rm':
73
      args[0] = 'remove'
15✔
74
      args = convertInstallArgs(args)
15✔
75
      break
15✔
76
    case 'cache':
77
      if (args[1] === 'clean') {
3!
78
        args = ['pm', 'cache', 'rm'].concat(args.slice(2))
3✔
79
      } else {
80
        cmd = 'npm'
×
81
      }
82
      break
3✔
83
    case 'rebuild':
84
    case 'rb':
85
      args[0] = 'add'
12✔
86
      args.push('--force')
12✔
87
      break
12✔
88
    case 'run':
89
      break
24✔
90
    case 'list':
91
    case 'ls':
92
      // 'npm ls' => 'bun pm ls'
93
      args = convertInstallArgs(args)
36✔
94
      args[0] = 'ls'
36✔
95
      args.unshift('pm')
36✔
96
      break
36✔
97
    case 'init':
98
    case 'create':
99
      if (args[1]) {
27✔
100
        if (args[1].startsWith('@')) {
24✔
101
          cmd = 'bunx'
3✔
102

103
          args[1] = args[1].replace('/', '/create-')
3✔
104
          args = args.slice(1)
3✔
105
        } else if (!args[1].startsWith('-')) {
21✔
106
          cmd = 'bunx'
6✔
107
          args[1] = `create-${args[1]}`
6✔
108
          args = args.slice(1)
6✔
109
        } else {
110
          args[0] = 'init'
15✔
111
        }
112
      }
113
      break
27✔
114

115
    case 'link':
116
    case 'ln':
117
      args = convertInstallArgs(args)
12✔
118
      args[0] = 'link'
12✔
119
      break
12✔
120
    case 'stop':
121
    case 'start':
122
    case 'unlink':
123
      break
12✔
124
    case 'test':
125
    case 't':
126
    case 'tst':
127
      args[0] = 'test'
15✔
128
      args.unshift('run')
15✔
129
      break
15✔
130
    case 'exec':
131
      cmd = 'bunx'
15✔
132
      args.splice(0, 1)
15✔
133
      break
15✔
134
    default:
135
      // null == keep `npm` command
136
      cmd = 'npm'
33✔
137
      break
33✔
138
  }
139

140
  const filtered = args.filter(Boolean).filter(arg => arg !== '--')
582✔
141
  return `${cmd} ${filtered.join(' ')}${
261✔
142
    cmd === 'npm' ? "\n# couldn't auto-convert command" : ''
261✔
143
  }`.replace('=', ' ')
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc