打开新的集成终端VSCode Mac时如何运行install node.js?

问题描述 投票:0回答:1

我希望能够在VSCode for MacOS上打开新的集成终端窗口时初始化Node.js。当前,我每次都使用“ nvm(节点版本管理器)”来初始化Node.js。

当我在Mac OS的VSCode上打开新的集成终端窗口时,是否有方法可以自动更新settings.json

我尝试添加:

"terminal.integrated.shellArgs.osx": [
    "nvm use 10"
]

settings.json,尽管这不起作用。

node.js macos visual-studio-code nvm
1个回答
2
投票

我不认为VSCode在您第一次打开外壳程序时就不应在外壳程序上运行任何东西,因为这正是.bashrc.zshrc和其他此类文件的作用。

对于我自己的用例,我将此代码段添加到我的.bashrc中(每次我打开新的交互式非登录外壳程序时都会运行)

find-up () {
    path=$(pwd)
    while [[ "$path" != "" && ! -e "$path/$1" ]]; do
        path=${path%/*}
    done
    echo "$path"
}

cdnvm(){
    cd "$@";
    nvm_path=$(find-up .nvmrc | tr -d '[:space:]')

    # If there are no .nvmrc file, use the default nvm version
    if [[ ! $nvm_path = *[^[:space:]]* ]]; then

        declare default_version;
        default_version=$(nvm version default);

        # If there is no default version, set it to `node`
        # This will use the latest version on your machine
        if [[ $default_version == "N/A" ]]; then
            nvm alias default node;
            default_version=$(nvm version default);
        fi

        # If the current version is not the default version, set it to use the default version
        if [[ $(nvm current) != "$default_version" ]]; then
            nvm use default;
        fi

        elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
        declare nvm_version
        nvm_version=$(<"$nvm_path"/.nvmrc)

        declare locally_resolved_nvm_version
        # `nvm ls` will check all locally-available versions
        # If there are multiple matching versions, take the latest one
        # Remove the `->` and `*` characters and spaces
        # `locally_resolved_nvm_version` will be `N/A` if no local versions are found
        locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')

        # If it is not already installed, install it
        # `nvm install` will implicitly use the newly-installed version
        if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
            nvm install "$nvm_version";
        elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
            nvm use "$nvm_version";
        fi
    fi
}
alias cd='cdnvm'
cd .

我实际上使用此代码段将PR存入nvm-sh/nvm存储库,现在它已成为nvm-sh/nvm的一部分。我会在那里使用版本。

也支持documentation。如果使用其他外壳,则可能必须复制相同的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.