【问题标题】:How to run install node.js when opening new integrated terminal VSCode Mac?打开新的集成终端VSCode Mac时如何运行安装node.js?
【发布时间】:2020-06-18 19:13:47
【问题描述】:

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

当我在 VSCode for MacOS 上打开一个新的集成终端窗口时,是否可以更新 settings.json 以自动执行此操作?

我尝试添加:

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

settings.json,虽然这不起作用。

【问题讨论】:

    标签: node.js macos visual-studio-code nvm


    【解决方案1】:

    我不认为 VSCode 应该在你第一次打开它时在你的 shell 上运行任何东西,因为这正是 .bashrc.zshrc 和其他此类文件的作用。

    对于我自己的用例,我将此 sn-p 添加到我的 .bashrc(每次打开新的交互式非登录 shell 时都会运行)

    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 .
    

    我实际上用这个 sn-p 向nvm-sh/nvm 存储库创建了一个PR,它现在是他们documentation 的一部分。我会使用那里的版本。

    还支持zsh。如果您使用另一个 shell,您可能必须复制相同的逻辑。

    【讨论】:

    • 太好了,谢谢!我刚刚更新了我的 nvm,它现在可以正常工作了。感谢您的 PR。
    猜你喜欢
    • 2015-07-15
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2012-12-23
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多