1 | const fs = require('fs')
|
2 | const { join } = require('path')
|
3 | const spawn = require('cross-spawn').sync
|
4 |
|
5 | const { fatal } = require('./logger')
|
6 |
|
7 | function isInstalled (cmd) {
|
8 | try {
|
9 | return spawn(cmd, ['--version']).status === 0
|
10 | }
|
11 | catch (err) {
|
12 | return false
|
13 | }
|
14 | }
|
15 |
|
16 | function getPackager (root) {
|
17 | if (fs.existsSync(join(root, 'yarn.lock'))) {
|
18 | return 'yarn'
|
19 | }
|
20 |
|
21 | if (fs.existsSync(join(root, 'package-lock.json'))) {
|
22 | return 'npm'
|
23 | }
|
24 |
|
25 | if (isInstalled('yarn')) {
|
26 | return 'yarn'
|
27 | }
|
28 |
|
29 | if (isInstalled('npm')) {
|
30 | return 'npm'
|
31 | }
|
32 |
|
33 | fatal('⚠️ Please install Yarn or NPM before running this command.\n')
|
34 | }
|
35 |
|
36 | module.exports = getPackager
|