UNPKG

661 BJavaScriptView Raw
1const fs = require('fs')
2const { join } = require('path')
3const spawn = require('cross-spawn').sync
4
5const { fatal } = require('./logger')
6
7function isInstalled (cmd) {
8 try {
9 return spawn(cmd, ['--version']).status === 0
10 }
11 catch (err) {
12 return false
13 }
14}
15
16function 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
36module.exports = getPackager