我的程序安装程序脚本在安装 Node.js 的 npm 后停止并且无法继续

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

我有一个安装脚本,用于安装特定的 github 存储库及其所有要求。

这是安装程序的snipit,似乎不一致:

@echo off

REM Check if Python 3.11 is installed
python --version 2>&1 | findstr /I "3.11"
if %ERRORLEVEL% NEQ 0 (
    echo Python 3.11 is not installed. Please install Python 3.11 from the official website.
    exit /b 1
)

REM Check if Git is installed
where git > nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo Git is not installed. Installing Git...
    powershell -Command "Start-Process https://git-scm.com/download/win -Wait"
)

REM Check if Node.js and npm are installed
node --version 2>&1 | findstr /I "v"
if %ERRORLEVEL% NEQ 0 (
    echo Node.js is not installed. Please install Node.js with npm from the official website.
    exit /b 1
)

REM Check if Python requests library is installed
pip show requests | findstr /I "Name: requests"
if %ERRORLEVEL% NEQ 0 (
    echo Installing requests...
    pip install requests
)

REM Set the installation directory to the user's home directory
set "repo_dir=%USERPROFILE%\algorithm-trader-warframe"
mkdir "%repo_dir%" 2>nul
cd /d "%repo_dir%"

REM Clone the Git repository into the specified directory
if exist "%repo_dir%\Warframe-Algo-Trader" (
    echo Repository is already cloned in %repo_dir%\Warframe-Algo-Trader.
) else (
    echo Cloning the Git repository into %repo_dir%\Warframe-Algo-Trader...
    git clone https://github.com/akmayer/Warframe-Algo-Trader
)

REM Install Python dependencies
pip install -r "%repo_dir%\Warframe-Algo-Trader\requirements.txt"
pip install uvicorn

REM Set the installation directory to the 'my-app' folder
cd /d "%repo_dir%\Warframe-Algo-Trader\my-app"

REM Check if Node.js dependencies are already installed
if exist "node_modules" (
    echo Node.js dependencies are already installed.
) else (
    echo Installing Node.js dependencies...
    npm install --no-fund

    REM Wait for npm installation to complete
    :WAIT_NPM_INSTALL
    if not exist "node_modules" (
        timeout /t 5 /nobreak > nul
        goto WAIT_NPM_INSTALL
    )
)

REM Go back to the main 'Warframe-Algo-Trader' folder
cd /d "%repo_dir%\Warframe-Algo-Trader"

REM Remove the existing config.json file (if it exists)
if exist "config.json" del "config.json"

REM Initialize the tables and create a new config.json file
python init.py

该问题始终发生在不继续执行 python init.py 和其他脚本的地方。一致的解决方案是运行脚本两次。现在我有一个理论,这可能是由于时间不当造成的,但除此之外,我无法在网上或通过 GPT 找到任何表明问题原因及其解决方案的内容。

我已经尝试添加超时和等待循环,但它似乎不起作用。该脚本总是在第二次运行时起作用,我想将其最小化为仅一次,而不需要将所有东西缝合在一起。

node.js batch-file npm-scripts
1个回答
0
投票

嗯..对于常规批量生产商来说,第一个问题是Mingw正在运行中未提及的问题。

Mingw 覆盖了一些标准批处理实用程序的正常操作,因此需要提及。

我相信这不是问题。

第一件事:“第二次就可以了”的原因通常是批处理文件的标准打开

@echo off
setlocal

尚未关注。这会丢弃批处理结束时对环境所做的任何更改,因此由一个批处理文件建立的变量不会影响可能在同一会话中运行的任何其他批处理。

因此结论是,环境变量是在第一次运行时建立的,在第一次运行结束时没有删除,然后在第二次运行中使用。

因此 - 很可能在第一次运行中执行一个

set
语句,该语句建立了先前步骤中使用的变量。这可能是
explicit
(变量在脚本中按名称提及)或
implicit
(变量由下标或实用程序使用)。或者可能是在第一次运行时建立了一个目录,假设该目录在某个时刻存在,或者假设当前目录与其实际位置不同。

所以候选人是

repo_dir

或由下标建立的某些变量或开始时的当前目录,因为似乎存在未撤消的

cd
指令(
setlocal
在批处理结束时恢复原始目录)

所以 - 我建议在脚本开始时设置

repo_dir
,然后创建所需的子目录。

下一个问题是

it doesnt proceed with python init.py and the other scripts
。由于
python init.py
是最后一行,我认为
other scripts
是问题所在 - 但我不知道它们是什么。也许运行它们取决于当前目录是否正确,也许取决于
repo_dir

最后,

REM Check if Node.js dependencies are already installed
if exist "node_modules" (
    echo Node.js dependencies are already installed.
) else (
    echo Installing Node.js dependencies...
    npm install --no-fund

    REM Wait for npm installation to complete
    :WAIT_NPM_INSTALL
    if not exist "node_modules" (
        timeout /t 5 /nobreak > nul
        goto WAIT_NPM_INSTALL
    )
)

不会起作用。

code block
(带括号的语句序列)不能包含标签。

REM Check if Node.js dependencies are already installed
if exist "node_modules" (
    echo Node.js dependencies are already installed.
    goto nodejsinstalled
)
echo Installing Node.js dependencies...
npm install --no-fund

REM Wait for npm installation to complete
:WAIT_NPM_INSTALL
if not exist "node_modules" (
  timeout /t 5 /nobreak > nul
  goto WAIT_NPM_INSTALL
)

:nodejsinstalled

更有可能发挥作用

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