1 | const { promisify } = require('util')
|
2 | const npmRoot = promisify(require('npm-root'))
|
3 | const chalk = require('chalk')
|
4 | const fs = require('fs-extra')
|
5 | const { indent } = require('./indent-bullet')
|
6 | const indent2 = indent.bind(null, 2)
|
7 |
|
8 | const chownNpm = 'sudo chown -R `whoami` <npm dir path>'
|
9 | const findNpmCmd = 'npm root -g'
|
10 | const lsNpm = 'ls -ld <npm dir path>'
|
11 | const noNpm = 'Currently cannot locate your npm global directory'
|
12 | const findNpm = `Find your global npm node_modules directory with the following command:
|
13 | ${chalk.italic.gray(findNpmCmd)}`
|
14 | const logNpmPerm = `Check permissions with the following command:
|
15 | ${chalk.italic.gray(lsNpm)}`
|
16 | const npmPermissions = 'Check permissions for the npm global directory'
|
17 | const fixNpmPerm = `If the user shows as 'root' and/or not your username, try the following:
|
18 | ${chalk.italic.gray(chownNpm)}
|
19 | The permissions should match "drwxr-xr-x"`
|
20 | const permFormat = `Output should resemble the following:
|
21 | [permissions] [links] [user] ...`
|
22 |
|
23 | module.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 | }
|