【发布时间】:2019-05-27 19:06:49
【问题描述】:
我正在尝试编写我的第一个批处理文件,该批处理文件构建嵌套在 SilverStripe 项目中的 React 应用程序(使用节点包管理器),并重命名 js 和 css 构建文件,并将媒体构建文件移动到其中的新目录SilverStripe 项目(因此它们可以被配置为指向 SilverStripe 项目的虚拟主机拾取)。
要在 cmd 中启动脚本,我使用以下脚本名称和两个参数:
updateReactInSS "C:\wamp64\www\example5" "C:\wamp64\www\example5\app\moe-card-app"
脚本确实成功地执行了 npm run build 但之后不再继续。我的预感是:
- 可能发生了异步事件?我为此添加了超时,但可能语法错误。以下行永远不会运行: echo "超时结束。脚本继续..."
- 我的语法可能有误,无法访问正确的目录?
以下是完整的脚本:
Rem ============================================================================================
Rem This batch script builds a React Application inside SilverStripe and
Rem moves media files if they exist to the public directory of the SilverStripe project.
Rem command line argument 1 = full path to the nested SilverStripe project folder
Rem command line argument 2 = full path to the nested React application
Rem It relies on node package manager and composer being installed.
Rem The result works with a virtual host configured to point at the SilverStripe project folder.
Rem =============================================================================================
@echo off
Rem Checking argument 1 entered correctly.
if "%~1"=="" (
echo You forgot to specify the full path to the SilverStripe project folder:
echo argument 1 = ?
goto finished
) else (
echo argument 1 = The path to the SilverStripe project folder is:
echo %1
)
Rem Checking argument 2 entered correctly.
if "%~2"=="" (
echo You forgot to specify the full path to the nested React application:
echo argument 2 = ?
goto finished
) else (
echo argument 2 = The path to the nested React application is:
echo %2
)
Rem Going to React application path. NOTE: could also add condition to check npm install if 1st time run
cd %2
cd
Rem Delete build directory if it exists. Using node package manager to build the React application.
if exist build\ del build /s /e
npm run build
Rem Adding time for npm run build to finish before continuing
TIMEOUT /T 20
echo TIMEOUT finished. Script continuing...
Rem Rename main.hashcode.js to main.bundle.js so consistent with requirements in related SS page controller.
cd %2\build\static\js
cd
rename main.*.js "main.bundle.js"
Rem Rename main.hashcode.css to main.bundle.css so consistent with requirements in related SS page controller.
cd %2\build\static\css
cd
rename main.*.css "main.bundle.css"
Rem Going to to SilverStripe project root to run composer vendor-expose command creating sym-links if not there.
cd %1
composer vendor-expose
Rem Copying media build files if media directory exists in React build files to the public folder in SilverStripe. project.
if exist %2\build\static\media (
MD \public\static\media
xcopy %2\build\static\media %1\public\static\media /s /e
) else (
goto finished
)
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
:finished
echo The programme updateReactInSS has completed.
:exit
这是我在 StackOverFlow 上提出的一个相关问题,其中详细说明了我想要实现的目标。如果你回答了这个问题,你也回答了 1 = 2 只鸟用一块石头!!!
【问题讨论】:
-
目前我能提供的最好的方法就是让您了解每个命令的工作原理。为此,您可以通过在命令提示符处输入命令名称和其帮助选项
/?来访问每个命令的帮助信息。此外,您应该使用Rem进行备注,而不是格式错误的标签::。要检查空参数,您应该使用If "%~1" == "",要检查是否存在目录而不是文件,您应该使用尾部反斜杠if exist build\。我还建议您不要用双引号括住任何echo字符串,例如If Exist "build\"和CD "%~1". -
非常感谢!我将对脚本进行这些更改并编辑此问题。
标签: reactjs batch-file silverstripe-4