provider/nodejs: fix npm,yarn detection

For each package manager,
1. Check if they are executable before starting a job.
2. Check if the job opts are set before checking the job status
   TODO: Validate the job id.

Prioritize npm because it's faster than yarn.
Reindent the code to 2-space indent.
This commit is contained in:
Jan Edmund Lazo
2018-10-19 01:59:58 -04:00
committed by Justin M. Keyes
parent db6521a560
commit b23759b4d7

View File

@@ -22,24 +22,14 @@ function! s:is_minimum_version(version, min_major, min_minor) abort
\ && str2nr(v_list[1]) >= str2nr(a:min_minor)))
endfunction
let s:NodeHandler = {}
let s:NodeHandler = {
\ 'stdout_buffered': v:true,
\ 'result': ''
\ }
function! s:NodeHandler.on_exit(job_id, data, event)
let bin_dir = join(self.stdout, '')
let entry_point = bin_dir . self.entry_point
if filereadable(entry_point)
let self.result = entry_point
else
let self.result = ''
end
endfunction
function! s:NodeHandler.new()
let obj = copy(s:NodeHandler)
let obj.stdout_buffered = v:true
let obj.result = ''
return obj
let bin_dir = join(self.stdout, '')
let entry_point = bin_dir . self.entry_point
let self.result = filereadable(entry_point) ? entry_point : ''
endfunction
" Support for --inspect-brk requires node 6.12+ or 7.6+ or 8+
@@ -65,31 +55,42 @@ function! provider#node#Detect() abort
return ''
endif
let yarn_subpath = '/node_modules/neovim/bin/cli.js'
let npm_subpath = '/neovim/bin/cli.js'
" `yarn global dir` is slow (> 250ms), try the default path first
if filereadable('$HOME/.config/yarn/global' . yarn_subpath)
return '$HOME/.config/yarn/global' . yarn_subpath
end
" try both npm and yarn simultaneously
let yarn_opts = s:NodeHandler.new()
let yarn_opts.entry_point = yarn_subpath
let yarn_opts.job_id = jobstart(['yarn', 'global', 'dir'], yarn_opts)
let npm_opts = s:NodeHandler.new()
let npm_opts.entry_point = npm_subpath
let npm_opts.job_id = jobstart(['npm', '--loglevel', 'silent', 'root', '-g'], npm_opts)
" npm returns the directory faster, so let's check that first
let result = jobwait([npm_opts.job_id])
if result[0] == 0 && npm_opts.result != ''
return npm_opts.result
let npm_opts = {}
if executable('npm')
let npm_opts = deepcopy(s:NodeHandler)
let npm_opts.entry_point = '/neovim/bin/cli.js'
let npm_opts.job_id = jobstart('npm --loglevel silent root -g', npm_opts)
endif
let result = jobwait([yarn_opts.job_id])
if result[0] == 0 && yarn_opts.result != ''
let yarn_opts = {}
if executable('yarn')
let yarn_opts = deepcopy(s:NodeHandler)
let yarn_opts.entry_point = '/node_modules/neovim/bin/cli.js'
" `yarn global dir` is slow (> 250ms), try the default path first
" XXX: The following code is not portable
" https://github.com/yarnpkg/yarn/issues/2049#issuecomment-263183768
if has('unix')
let yarn_default_path = $HOME . '/.config/yarn/global/' . yarn_opts.entry_point
if filereadable(yarn_default_path)
return yarn_default_path
endif
endif
let yarn_opts.job_id = jobstart('yarn global dir', yarn_opts)
endif
" npm returns the directory faster, so let's check that first
if !empty(npm_opts)
let result = jobwait([npm_opts.job_id])
if result[0] == 0 && npm_opts.result != ''
return npm_opts.result
endif
endif
if !empty(yarn_opts)
let result = jobwait([yarn_opts.job_id])
if result[0] == 0 && yarn_opts.result != ''
return yarn_opts.result
endif
endif
return ''