UNPKG

1.75 kBJavaScriptView Raw
1const { promisify } = require('util')
2const npmRoot = promisify(require('npm-root'))
3const chalk = require('chalk')
4const fs = require('fs-extra')
5const { indent } = require('./indent-bullet')
6const indent2 = indent.bind(null, 2)
7
8const chownNpm = 'sudo chown -R `whoami` <npm dir path>'
9const findNpmCmd = 'npm root -g'
10const lsNpm = 'ls -ld <npm dir path>'
11const noNpm = 'Currently cannot locate your npm global directory'
12const findNpm = `Find your global npm node_modules directory with the following command:
13 ${chalk.italic.gray(findNpmCmd)}`
14const logNpmPerm = `Check permissions with the following command:
15 ${chalk.italic.gray(lsNpm)}`
16const npmPermissions = 'Check permissions for the npm global directory'
17const fixNpmPerm = `If the user shows as 'root' and/or not your username, try the following:
18 ${chalk.italic.gray(chownNpm)}
19The permissions should match "drwxr-xr-x"`
20const permFormat = `Output should resemble the following:
21[permissions] [links] [user] ...`
22
23module.exports = async () => {
24 const warnings = []
25 try {
26 const npmPath = await npmRoot({ global: true })
27 try {
28 await fs.access(npmPath, fs.constants.R_OK | fs.constants.W_OK)
29 } catch (err) {
30 if (err.code === 'ENOENT') warnings.push(`${noNpm}\n${indent2(findNpm)}\n${indent2(logNpmPerm)}`)
31 else if (err.code === 'EACCES') warnings.push(`${npmPermissions}\n${indent2(`${logNpmPerm.replace(/<.+>/, npmPath)}\n${permFormat}\n${fixNpmPerm.replace(/<.+>/, npmPath)}`)}`)
32 else warnings.push(`${npmPermissions}\n${indent2(`${logNpmPerm.replace(/<.+>/, npmPath)}\n${permFormat}\n${fixNpmPerm.replace(/<.+>/, npmPath)}`)}`)
33 }
34 } catch (err) {
35 warnings.push(`${noNpm}\n${indent2(findNpm)}`)
36 }
37 return { warnings, details: [] }
38}