diff options
| author | Mitermayer Reis <mitermayer.reis@gmail.com> | 2020-02-05 11:08:03 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-05 11:08:03 +1100 |
| commit | 49d91743b2df43f84edd199f877d494b4d8812f4 (patch) | |
| tree | cf856d77c9960a09eb3156937aa1b896b855bed6 /autoload/prettier/resolver/executable.vim | |
| parent | 9eb448e45ef88e90681335fda32bcae52a09d6dc (diff) | |
| parent | b064c6ab82a3c57ea64360d762d661ad7e8ee54c (diff) | |
| download | vim-prettier-49d91743b2df43f84edd199f877d494b4d8812f4.tar.xz | |
Merge pull request #175 from prettier/release/1.x
Release/1.x
Diffstat (limited to 'autoload/prettier/resolver/executable.vim')
| -rw-r--r-- | autoload/prettier/resolver/executable.vim | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/autoload/prettier/resolver/executable.vim b/autoload/prettier/resolver/executable.vim new file mode 100644 index 0000000..8d233ab --- /dev/null +++ b/autoload/prettier/resolver/executable.vim @@ -0,0 +1,74 @@ +let s:ROOT_DIR = fnamemodify(resolve(expand('<sfile>:p')), ':h') + +" By default we will search for the following +" => user defined prettier cli path from vim configuration file +" => locally installed prettier inside node_modules on any parent folder +" => globally installed prettier +" => vim-prettier prettier installation +" => if all fails suggest install +function! prettier#resolver#executable#getPath() abort + let l:user_defined_exec_path = fnamemodify(g:prettier#exec_cmd_path, ':p') + if executable(l:user_defined_exec_path) + return l:user_defined_exec_path + endif + + let l:localExec = s:ResolveExecutable(getcwd()) + if executable(l:localExec) + return fnameescape(l:localExec) + endif + + let l:globalExec = s:ResolveExecutable() + if executable(l:globalExec) + return fnameescape(l:globalExec) + endif + + let l:pluginExec = s:ResolveExecutable(s:ROOT_DIR) + if executable(l:pluginExec) + return fnameescape(l:pluginExec) + endif + + return -1 +endfunction + +function! s:GetExecPath(...) abort + let l:rootDir = a:0 > 0 ? a:1 : -1 + let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : '' + return l:dir . 'prettier' +endfunction + +" Searches for the existence of a directory accross +" ancestral parents +function! s:TraverseAncestorDirSearch(rootDir) abort + let l:root = a:rootDir + let l:dir = 'node_modules' + + while 1 + let l:searchDir = l:root . '/' . l:dir + if isdirectory(l:searchDir) + return l:searchDir + endif + + let l:parent = fnamemodify(l:root, ':h') + if l:parent == l:root + return -1 + endif + + let l:root = l:parent + endwhile +endfunction + +function! s:ResolveExecutable(...) abort + let l:rootDir = a:0 > 0 ? a:1 : 0 + let l:exec = -1 + + if isdirectory(l:rootDir) + let l:dir = s:TraverseAncestorDirSearch(l:rootDir) + if l:dir != -1 + let l:exec = s:GetExecPath(l:dir) + endif + else + let l:exec = s:GetExecPath() + endif + + return l:exec +endfunction |
