Skip to content

Commit

Permalink
improve node.js download script
Browse files Browse the repository at this point in the history
  • Loading branch information
sasaplus1 committed Dec 27, 2024
1 parent 7001ec9 commit 076c097
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions vim/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ set runtimepath&
let g:vimrc_vim_dir = has('nvim') ? expand('~/.nvim') : expand('~/.vim')

" プラグインが使用するnode.jsのパス
let s:node = simplify(g:vimrc_vim_dir . '/node/bin/node')
let s:node_bin = simplify(g:vimrc_vim_dir . '/node/bin/node')
let s:node_dir = fnamemodify(s:node_bin, ':h:h')
let s:node_ver = '20.18.1'

" プラグインが使用するnode.jsをインストールする
" TODO: シェルスクリプトに移した方が良さそう
function! s:install_node() abort
if !executable('uname') || !executable('curl')
let commands = ['curl', 'tar', 'uname']

if commands->map('executable(v:val) ? 0 : 1')->filter('v:val != 0')->len() > 0
return
endif

let ver = '20.18.1'

if has('osxdarwin')
let platform = 'darwin'
elseif has('linux')
let platform = 'linux'
else
return
endif

let uname = trim(system('uname -m'))
Expand All @@ -35,43 +39,27 @@ function! s:install_node() abort
let arch = 'arm64'
elseif uname ==# 'x86_64'
let arch = 'x64'
else
return
endif

let url = printf(
\ 'https://nodejs.org/download/release/v%s/node-v%s-%s-%s.tar.gz',
\ ver,
\ ver,
\ platform,
\ arch,
\ )

let dir = fnamemodify(s:node, ':h:h')

call mkdir(dir, 'p')
call mkdir(s:node_dir, 'p')

let tgz = simplify(g:vimrc_vim_dir . '/node.tar.gz')

let curl = printf(
\ 'curl -fsSL -o "%s" "%s"',
\ tgz,
\ url,
\ )
let tar = printf(
\ 'tar fx "%s" -C "%s" --strip-components 1',
\ tgz,
\ dir,
let script = printf(
\ 'curl -fsSL "https://nodejs.org/download/release/v%s/node-v%s-%s-%s.tar.gz" | ' .
\ 'tar fx - -C "%s" --strip-components 1',
\ s:node_ver, s:node_ver, platform, arch, s:node_dir,
\ )

call system(curl)
call system(tar)
execute '!' . script
endfunction

" プラグインが使用するnode.jsが存在しない場合はインストールする
if empty(glob(s:node))
if empty(glob(s:node_bin))
call s:install_node()
endif

" パスを通す
let $PATH = fnamemodify(s:node, ':h') . ':' . $PATH
let $PATH = fnamemodify(s:node_bin, ':h') . ':' . $PATH

" vim:ft=vim:fdm=marker:fen:

0 comments on commit 076c097

Please sign in to comment.