自动化是快速提高团队效率的一个趋势,在未实现自动化打包部署之前,为提高效率我们也应当尽可能的实现脚本打包,减少错误。
Windows上打包和Mac上有些不同,Windows系统在没有装任何工具的情况下,我们就得借助Windows上的脚本规范来写一些打包脚本了,Mac使用sh即shell,而Windows使用bat。
以下是第一份脚本,我在此命名为build.bat
build.bat
@echo off
echo This file should only modified by Notepad and saved as type ANSI or the Chinese characters would not be shown correctly!!!
echo 本文件只可以用文本文件编辑并以ANSI类型保存,否则中文字符将无法正常显示!!!
echo STEP-1: 编译React模块
echo STEP-2: 安装依赖模块...
call cnpm install
echo STEP-3: 编译打包
call npm run build
echo STEP-4: 生成版本信息文件 version.txt
echo {"version":"1.0.0.%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%","codeVersion":""} > build/version.txt
set zipName=H5V1.0.0_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%.zip
set src=%cd%\build
set fileName=%cd%\%zipName%
echo STEP-8: 压缩生成包 %zipName%
cscript ./zip.vbs %src% %fileName%
echo STEP-9: 打包完成!!!
pause
在build.bat文件中使用了cscript ./zip.vbs
,这是一段VB的脚本,主要作用是将选定的文件夹压缩成zip文件。
zip.vbs Set objArgs = WScript.Arguments zip objArgs(0), objArgs(1) Sub zip(ByVal mySourceDir, ByVal myZipFile) Set fso = CreateObject("Scripting.FileSystemObject") If fso.GetExtensionName(myZipFile) <> "zip" Then Exit Sub ElseIf fso.FolderExists(mySourceDir) Then FType = "Folder" ElseIf fso.FileExists(mySourceDir) Then FType = "File" FileName = fso.GetFileName(mySourceDir) FolderPath = Left(mySourceDir, Len(mySourceDir) - Len(FileName)) Else Exit Sub End If Set f = fso.CreateTextFile(myZipFile, True) f.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0)) f.Close Set objShell = CreateObject("Shell.Application") Select Case Ftype Case "Folder" Set objSource = objShell.NameSpace(mySourceDir) Set objFolderItem = objSource.Items() Case "File" Set objSource = objShell.NameSpace(FolderPath) Set objFolderItem = objSource.ParseName(FileName) End Select Set objTarget = objShell.NameSpace(myZipFile) intOptions = 256 objTarget.CopyHere objFolderItem, intOptions Do WScript.Sleep 1000 Loop Until objTarget.Items.Count > 0 End Sub