-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79f8ddc
commit 30a10aa
Showing
6 changed files
with
186 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# nodejs Layer | ||
|
||
## Table of Contents | ||
|
||
<!-- vim-markdown-toc GFM --> | ||
* [Description](#description) | ||
* [Install](#install) | ||
* [Key Bindings](#keybindings) | ||
|
||
<!-- vim-markdown-toc --> | ||
|
||
## Description | ||
|
||
This layer adds support for the Node.js tool. | ||
|
||
## Install | ||
|
||
To use this layer, add it to your `~/.spacevim` as follows: | ||
|
||
``` | ||
let g:spacevim_layers = [ | ||
\ 'programming', 'nodejs', | ||
\ ] | ||
``` | ||
|
||
If Node.js is installed system-wide and is version >= 16.10 then that will be used. Otherwise the nodejs layer requires the programming layer to build Node.js as a dependency within vim-plug's plugged directory. | ||
|
||
Node.js built requires approx 900 MiB space, largely intermediate files. | ||
|
||
If you instead wish to use Node.js elsewhere (not on `$PATH`), this package will look at `$NODE` and use that if it is of sufficient version. | ||
|
||
The curated node will be added to `$PATH` for use by other plugins. Node.js >= v16.10 also includes yarn/npm, useful for other plugins to install dependencies. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
" Check for Node.js >= v16.10 | ||
function! s:isNodeJsVersionSufficient(node) | ||
let node_version = system(a:node.' --version') | ||
let node_version_parts = map(filter(split(node_version, '\v\ze\.|v'), 'v:val =~ "\\v\\d+"'), 'matchstr(v:val, "\\v\\d+")') | ||
if len(node_version_parts) >= 2 && (node_version_parts[0] > 16 || (node_version_parts[0] == 16 && node_version_parts[1] >= 10)) | ||
return 1 | ||
endif | ||
return 0 | ||
endfunction | ||
|
||
" Will do the following checks in order of precedence: | ||
" -Use $NODE or $NODE/node if they exist and are executable | ||
" -check for node as vim-plug plugin (user-specific) | ||
" -check for system-wide install of node | ||
" -If all above fail will get node as vim-plug plugin and build it | ||
" Changes $PATH to include node path | ||
function! s:isNewNodeJsRequired() | ||
let pathsep = g:spacevim.os.windows ? ';' : ':' | ||
" Prefer $NODE override | ||
if exists('$NODE') | ||
if executable('$NODE') == 1 && s:isNodeJsVersionSufficient('$NODE') | ||
let $PATH = fnamemodify($NODE, ':h').pathsep.$PATH | ||
return 0 | ||
elseif executable('$NODE/node') == 1 && s:isNodeJsVersionSufficient('$NODE/node') | ||
let $PATH = $NODE.pathsep.$PATH | ||
return 0 | ||
endif | ||
endif | ||
" Prefer user/plugin Node.js if it exists | ||
if executable(g:plug_home.'/node/node') == 1 && s:isNodeJsVersionSufficient(g:plug_home.'/node/node') | ||
let $PATH = g:plug_home.'/node'.pathsep.$PATH | ||
return 0 | ||
endif | ||
" Last check for system-wide Node.js | ||
if executable('node') == 1 && s:isNodeJsVersionSufficient('node') | ||
return 0 | ||
endif | ||
return 1 | ||
endfunction | ||
|
||
" If no valid $NODE or systemwide node.js: build node.js dependency | ||
function! BuildNodeJs(info) | ||
if g:spacevim.os.windows | ||
echom 'Building Node.js automatically is not supported on Windows, Build it manually or send PR' | ||
return | ||
endif | ||
if (!exists('$CC') && executable('gcc') != 1) || executable('make') != 1 || executable('python3') != 1 | ||
echom 'Unable to build Node.js, requires a C++ compiler, make, and python3' | ||
return | ||
endif | ||
if !spacevim#load('programming') | ||
echom "nodejs layer requires the 'programming' layer to build. Add 'programming' layer to init.spacevim" | ||
return | ||
endif | ||
|
||
echom 'Building Node.js, please wait... restart Vim on completion' | ||
let ninja_flag = '' | ||
let jobs_flag = '' | ||
if executable('ninja') | ||
let ninja_flag=' --ninja' | ||
else | ||
" Determine number of cores/threads for make -j, ninja autodetects | ||
if executable('lscpu') | ||
execute 'let num_threads = '.join(split(system("lscpu | grep -E '?^(CPU\\(s\\):|Thread\\(s\\) per core:)' | tr -s ' ' | cut -f 2 -d:")),'*') | ||
else | ||
let num_threads = 4 | ||
endif | ||
let jobs_flag = ' -j '.num_threads | ||
endif | ||
|
||
" This sets up corepack/npm links as default make taget doesn't | ||
" Could alternatively `make install` to prefix and set PATH to prefix/bin | ||
let temp_dir = fnamemodify(tempname(), ':p:h') | ||
call system('./configure --prefix=./build'.ninja_flag.' > '.temp_dir.'/nodejs_configure.log') | ||
|
||
" Cache any existing Make definition and restore | ||
let makeDefinitionTest = split(execute(':command Make'), '\n') | ||
if len(makeDefinitionTest) > 1 | ||
let makeDefinition = split(makeDefinitionTest[2], '\s\s\+') | ||
endif | ||
command! -bang -nargs=* -bar Make AsyncRun -mode=term -pos=tab -program=make @ <args> | ||
execute 'Make'.jobs_flag | ||
delcommand Make | ||
if exists('makeDefinition') | ||
" Restores prior definition. Has inadequacies. | ||
execute 'command! '.(makeDefinition[0] =~ '!' ? '-bang ' : '').(makeDefinition[0] =~ '|' ? '-bar ' : '').(makeDefinition[0] =~ 'b' ? '-buffer ' : '').makeDefinition[0] =~ '\"' ? '-register ' : '').'-nargs='.makeDefinition[1].' Make '.makeDefinition[3] | ||
endif | ||
let $NODE = fnamemodify('.', ':p').'/node' | ||
" TODO: Uncertain if correct node_modules | ||
let $NODE_PATH = fnamemodify('.', ':p').'/tools/node_modules' | ||
let pathsep = g:spacevim.os.windows ? ';' : ':' | ||
let $PATH = g:plug_home.'/node'.pathsep.$PATH | ||
|
||
" Set links which `make install` does, they're necessary for use | ||
call system("ln -s deps/corepack/dist/corepack.js corepack") | ||
call system("ln -s deps/npm/bin/npm-cli.js npm") | ||
call system("ln -s deps/npm/bin/npx-cli.js npx") | ||
" Make Yarn available | ||
call system("node corepack enable") | ||
endfunction | ||
|
||
" isNewNodeJsRequired will add node to $PATH if needed | ||
if s:isNewNodeJsRequired() | ||
MP 'nodejs/node', { 'do': function('BuildNodeJs') } | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters