Showing
2 changed files
with
287 additions
and
0 deletions
run.sh
0 → 100644
| 1 | +# run.sh | ||
| 2 | +# desc: 该脚本用于一键构建线上代码,并自动提交到远程git仓库 | ||
| 3 | +initContext(){ | ||
| 4 | + # 目标文件目录目录 | ||
| 5 | + source_dir=dist | ||
| 6 | + | ||
| 7 | + # 为app内嵌版本打包的参数 | ||
| 8 | + if [ $# -gt 0 ] && [ $1 = 'beta' ];then | ||
| 9 | + # 生产代码远程仓库地址 | ||
| 10 | + git_url=xx.git | ||
| 11 | + | ||
| 12 | + # 生产代码本地根目录 | ||
| 13 | + dest=".deploy/beta" | ||
| 14 | + | ||
| 15 | + # npm 的脚本名次 | ||
| 16 | + node_script=build_beta | ||
| 17 | + else | ||
| 18 | + # 生产代码远程仓库地址 | ||
| 19 | + git_url=xx.git | ||
| 20 | + | ||
| 21 | + # 生产代码本地根目录 | ||
| 22 | + dest=".deploy/pro" | ||
| 23 | + | ||
| 24 | + # npm 的脚本名次 | ||
| 25 | + node_script=build_pro | ||
| 26 | + fi | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +# 初始化git目录,pull最新代码 | ||
| 30 | +init(){ | ||
| 31 | + echo +++init start; | ||
| 32 | + | ||
| 33 | + if [ ! -d $dest ]; then | ||
| 34 | + git clone $git_url $dest | ||
| 35 | + fi | ||
| 36 | + | ||
| 37 | + # 记录现在的目录位置,最后要回来的 | ||
| 38 | + cur=`pwd` | ||
| 39 | + | ||
| 40 | + # 进入git目录 | ||
| 41 | + cd $dest | ||
| 42 | + | ||
| 43 | + # git checkout . | ||
| 44 | + git add . | ||
| 45 | + git stash | ||
| 46 | + | ||
| 47 | + # reset为线上最新版本,要先pull一下再reset。 | ||
| 48 | + git pull origin master | ||
| 49 | + git reset --hard origin/master | ||
| 50 | + | ||
| 51 | + # 然后再pull一下 | ||
| 52 | + git pull origin master | ||
| 53 | + | ||
| 54 | + # 回到原来的目录 | ||
| 55 | + cd $cur | ||
| 56 | + echo ---init end; | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +# 重置dist目录 | ||
| 60 | +resetDist(){ | ||
| 61 | + echo +++resetDist start | ||
| 62 | + | ||
| 63 | + rsync -a --delete --exclude='.git' $dest/. ./dist | ||
| 64 | + | ||
| 65 | + echo ---resetDist end | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +# 构建 | ||
| 69 | +build(){ | ||
| 70 | + echo +++build start | ||
| 71 | + npm run $node_script | ||
| 72 | + echo ---build end | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +# 检查是否成功 | ||
| 76 | +checkBuild(){ | ||
| 77 | + if [[ ! -f $source_dir/index.html || ! -d $source_dir/static ]]; then | ||
| 78 | + echo error | ||
| 79 | + else | ||
| 80 | + echo ok | ||
| 81 | + fi | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +# 到$dest目录 | ||
| 85 | +cpCode(){ | ||
| 86 | + echo +++cpCode start | ||
| 87 | + # 复制代码,所有文件包含隐藏文件 | ||
| 88 | + rsync -r --delete --exclude='.git' $source_dir/. $dest | ||
| 89 | + | ||
| 90 | + echo ---cpCode end | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +# 提交到远程git仓库 | ||
| 94 | +commit(){ | ||
| 95 | + echo +++commit start | ||
| 96 | + # 记录现在的目录位置,最后要回来的 | ||
| 97 | + cur=`pwd` | ||
| 98 | + | ||
| 99 | + # 进入git目录 | ||
| 100 | + cd $dest | ||
| 101 | + # 提交的字符串 | ||
| 102 | + commit_str="commited in `date '+%Y-%m-%d_%H:%M:%S'`" | ||
| 103 | + | ||
| 104 | + git add . | ||
| 105 | + git commit -am "${commit_str}" | ||
| 106 | + git push origin master | ||
| 107 | + | ||
| 108 | + # 回到原来的目录 | ||
| 109 | + cd $cur | ||
| 110 | + echo ---commit end | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +# 显示帮助信息 | ||
| 114 | +help(){ | ||
| 115 | + echo ./run.sh build "#"构建代码 | ||
| 116 | + echo ./run.sh init "#"初始化git仓库 | ||
| 117 | + echo ./run.sh commit "#"提交到git | ||
| 118 | + echo ./run.sh "#"执行全部任务 | ||
| 119 | + echo ./run.sh hello "#"hello | ||
| 120 | + echo ./run.sh test "#"test | ||
| 121 | + | ||
| 122 | + echo ./run.sh beta "#"一键构建和提交beta版本 | ||
| 123 | + # app内嵌版本 | ||
| 124 | + echo ----app内嵌版本-------- | ||
| 125 | + echo ./run.sh app "#"一键构建和提交app版本 | ||
| 126 | + | ||
| 127 | + echo ----帮助信息-------- | ||
| 128 | + echo ./run.sh help "#"帮助 | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +# 测试用的 | ||
| 132 | +test(){ | ||
| 133 | + echo "a test empty task" | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +# 入口 | ||
| 137 | +if [[ $# -lt 1 || $1 = 'app' || $1 = 'beta' || $1 = 'beta1' || $1 = 'beta2' ]]; then | ||
| 138 | + # 无参数则打pro包,否则打相应类型的包 | ||
| 139 | + if [ $# -lt 1 ];then | ||
| 140 | + type=pro | ||
| 141 | + else | ||
| 142 | + type=$1 | ||
| 143 | + fi | ||
| 144 | + | ||
| 145 | + echo ===\>准备构建${type}版 | ||
| 146 | + initContext $type && init && resetDist | ||
| 147 | + | ||
| 148 | + # 构建代码 | ||
| 149 | + buildRes=$(build) | ||
| 150 | + | ||
| 151 | + # 检查构建结果 | ||
| 152 | + echo -e "$buildRes" | ||
| 153 | + | ||
| 154 | + if [[ $buildRes =~ "ERROR" ]]; then | ||
| 155 | + echo "$(tput setaf 1)xxx\>build error,task abort$(tput sgr0)" | ||
| 156 | + else | ||
| 157 | + # 代码构建成功才继续。 | ||
| 158 | + checkRes=$(checkBuild) | ||
| 159 | + | ||
| 160 | + if [ $checkRes == "ok" ];then | ||
| 161 | + cpCode && commit | ||
| 162 | + echo "$(tput setaf 2)===\>task complete$(tput sgr0)" | ||
| 163 | + else | ||
| 164 | + echo "$(tput setaf 1)xxx\>build error,task abort$(tput sgr0)" | ||
| 165 | + fi | ||
| 166 | + fi | ||
| 167 | + | ||
| 168 | + | ||
| 169 | +elif [ $1 ]; then | ||
| 170 | + # 参数不是包类型的,当中函数处理 | ||
| 171 | + echo ===\>准备执行${1}函数 | ||
| 172 | + initContext beta | ||
| 173 | + | ||
| 174 | + func=$1 | ||
| 175 | + $func | ||
| 176 | + echo ===\>task complete | ||
| 177 | +fi |
task.js
0 → 100644
| 1 | +/* jshint esversion: 6 */ | ||
| 2 | +let fs = require('fs'); | ||
| 3 | +let path = require('path'); | ||
| 4 | +let endOfLine = require('os').EOL; | ||
| 5 | + | ||
| 6 | +module.exports = { | ||
| 7 | + maxHistoryNum: 5, | ||
| 8 | + historyFile: path.resolve(__dirname, './dist/history.js'), | ||
| 9 | + staticDir: path.resolve(__dirname, './dist/'), | ||
| 10 | + | ||
| 11 | + /* | ||
| 12 | + r 打开只读文件,该文件必须存在。 | ||
| 13 | + r+ 打开可读写的文件,该文件必须存在。 | ||
| 14 | + w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。 | ||
| 15 | + w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 | ||
| 16 | + a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。 | ||
| 17 | + a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 | ||
| 18 | + */ | ||
| 19 | + | ||
| 20 | + creataHistoryIfNotExist () { | ||
| 21 | + if (!fs.existsSync(this.historyFile)) { | ||
| 22 | + this.storeHistory([], 'a+'); | ||
| 23 | + } | ||
| 24 | + }, | ||
| 25 | + | ||
| 26 | + // @done 将数据写到 history.js | ||
| 27 | + storeHistory (list, mode) { | ||
| 28 | + let historyFile = this.historyFile; | ||
| 29 | + let outJson = 'module.exports = [' + endOfLine; | ||
| 30 | + let listLen = list.length; | ||
| 31 | + if (list && listLen > 0) { | ||
| 32 | + list.forEach((item, index) => { | ||
| 33 | + if (index === listLen - 1) { | ||
| 34 | + outJson += ` ${item}${endOfLine}`; | ||
| 35 | + } else { | ||
| 36 | + outJson += ` ${item},${endOfLine}`; | ||
| 37 | + } | ||
| 38 | + }); | ||
| 39 | + } | ||
| 40 | + outJson += ']' + endOfLine; | ||
| 41 | + | ||
| 42 | + fs.writeFileSync(historyFile, outJson, { | ||
| 43 | + flag: mode | ||
| 44 | + }); | ||
| 45 | + }, | ||
| 46 | + | ||
| 47 | + // 递归删除目录中的文件 | ||
| 48 | + rmFiles (dirPath, regexp) { | ||
| 49 | + let files; | ||
| 50 | + try { | ||
| 51 | + files = fs.readdirSync(dirPath); | ||
| 52 | + } catch (e) { | ||
| 53 | + return; | ||
| 54 | + } | ||
| 55 | + if (regexp && files && files.length > 0) { | ||
| 56 | + for (let i = 0; i < files.length; i++) { | ||
| 57 | + let filename = files[i]; | ||
| 58 | + let filePath = dirPath + '/' + files[i]; | ||
| 59 | + if (fs.statSync(filePath).isFile() && regexp.test(filename)) { | ||
| 60 | + console.warn('删除过期的历史版本->(' + regexp + '):' + filename); | ||
| 61 | + fs.unlinkSync(filePath); | ||
| 62 | + } else { | ||
| 63 | + this.rmFiles(filePath, regexp); | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + }, | ||
| 68 | + | ||
| 69 | + // @done | ||
| 70 | + cleanOldVersionFilesIfNeed (version) { | ||
| 71 | + let staticDir = this.staticDir; | ||
| 72 | + let maxHistoryNum = this.maxHistoryNum; | ||
| 73 | + | ||
| 74 | + let history = []; | ||
| 75 | + | ||
| 76 | + try { | ||
| 77 | + history = require(this.historyFile); | ||
| 78 | + } catch (e) { | ||
| 79 | + console.error(e); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + // 加入最新的版本,老的的版本删除 | ||
| 83 | + history.push(version); | ||
| 84 | + | ||
| 85 | + // 如果历史版本数超过限制,则删除老的历史版本 | ||
| 86 | + let len = history.length; | ||
| 87 | + if (len > maxHistoryNum) { | ||
| 88 | + let oldVersions = history.slice(0, len - maxHistoryNum); | ||
| 89 | + | ||
| 90 | + for (let i = 0; i < oldVersions.length; i++) { | ||
| 91 | + let ver = oldVersions[i]; | ||
| 92 | + let reg = new RegExp(ver); | ||
| 93 | + this.rmFiles(staticDir, reg); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + // 更新history文件 | ||
| 97 | + let newVersions = history.slice(len - maxHistoryNum); | ||
| 98 | + this.storeHistory(newVersions); | ||
| 99 | + } else { | ||
| 100 | + // 写入history文件 | ||
| 101 | + this.storeHistory(history); | ||
| 102 | + } | ||
| 103 | + }, | ||
| 104 | + | ||
| 105 | + // 入口 | ||
| 106 | + run (version) { | ||
| 107 | + this.creataHistoryIfNotExist(); | ||
| 108 | + this.cleanOldVersionFilesIfNeed(version); | ||
| 109 | + } | ||
| 110 | +}; |
-
Please register or login to post a comment