wdp辅助功能
# 单独构建子项目
# 背景
单独传入参数,构建想要的模块,提高构建速度;
# 实现
if [ $# -gt 0 ]; then
for i in "$@"; do
case $i in
a | admin )
buildAdmin
;;
w | web)
buildWeb
;;
c | capacity)
buildCapacity
;;
f | files)
buildFiles
;;
*)
echo "项目命令不存在"
;;
esac
done
else
echo '=====build all pro====='
buildAdmin
buildWeb
buildCapacity
buildFiles
fi
# build.sh a 或者 build.sh admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 加入版本定位功能
# 背景
方便放出去的现场版本,通过版本号方便定位问题;
# 实现
curDir=$(
cd "$(dirname "$0")"
pwd
)
webDir=${curDir}/web
versionDist=dist/web/version.txt
rm -rf ${curDir}/dist ${curDir}/dist.zip
mkdir -p ${curDir}/${admDirDist} ${curDir}/${webDirDist}
printVersion() {
cd ${curDir}
echo "==================================printVersion===================================="
curBranch=`git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)|\1|' | sort -u | grep -v HEAD`
commitID=`git rev-parse --short HEAD`
commitTime=`git show --pretty=format:"%ci" | head -1`
version=${curBranch}@${commitID}@${commitTime}
echo "${version}" > ${versionDist}
echo "=====版本记录生产version.txt成功====${version}======"
}
printVersion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
最终结果:
优化后,丰富版本构建信息
printVersion() {
cd ${curDir}
echo "==================================printVersion===================================="
curBranch=$(git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)|\1|' | sort -u | grep -v HEAD)
commitID=$(git rev-parse --short HEAD)
commitTime=$(git show --pretty=format:"%ci" | head -1)
# version=${curBranch}@${commitID}@${commitTime}
buildTime=$(date "+%Y-%m-%d %H:%M:%S")
echo "${version}" > ${versionDist}
echo "{
\"version\":{
\"buildTime\": \"${buildTime}\",
\"curBranch\": \"${curBranch}\",
\"commitID\": \"${commitID}\",
\"commitTime\": \"${commitTime}\"
},
\"buildCfg\":{
\"isPro\":${isPro},
\"defaultLang\":${defaultLang},
\"isHideLang\":${isHideLang},
\"isTokenAuth\":${isTokenAuth}
}
}" > $versionDist
echo "=====版本记录生产version.json成功========"
cat $versionDist
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
打印:
{
"version":{
"buildTime": "2022-01-13 14:49:07",
"curBranch": "dev",
"commitID": "6c831a22",
"commitTime": "2022-01-12 17:32:14 +0800"
},
"buildCfg":{
"isPro":true,
"defaultLang":"zh",
"isHideLang":false,
"isTokenAuth":true
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 配置灵活性env
# 背景
通过构建灵活配置,实现不同场景的默认初始化定制;
# 实现
# sh脚本方式
通过各个子项目,抽取到config.js文件中;再通过脚本读取env.的配置,再去替换js文件;再构建生产环境包;
#defaultFile=$(pwd)/env/.env.default
#targetFile=$(pwd)/env/.env.$1 #得优化使用绝对路径;
curDir=$(
cd "$(dirname "$0")"
pwd
)
defaultFile=${curDir}/env/.env.default
targetFile=${curDir}/env/.env.$1
replacePro() {
varKey=$2
varValue=$3
echo $varKey $varValue
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/\(${varKey} =\).*/\1 ${varValue};/" $1 #注意等号前后有空格
else
sed -i "s/\(${varKey} =\).*/\1 ${varValue};/" $1 #注意等号前后有空格
fi
}
function getEnvConf() {
param=$1
value1=$(sed -E '/^#.*|^ *$/d' $defaultFile | sed s/[[:space:]]//g | awk -F "${param}=" "/${param}=/{print \$2}" | tail -n1)
if [ -f $targetFile ]; then
value2=$(sed -E '/^#.*|^ *$/d' $targetFile | sed s/[[:space:]]//g | awk -F "${param}=" "/${param}=/{print \$2}" | tail -n1)
fi
if [ ! -n "$value2" ]; then
value=$value1
else
value=$value2
fi
echo $value
}
echo '=====检查参数env文件合法性====='
if [ -n "$1" ]; then
if [ ! -f $targetFile ]; then
echo "File env not exist ${targetFile}" #添加文件提示
echo "Please enter a legal ENV configuration!!!"
exit $E_NO_FILES
fi
fi
echo '=====初始化env配置====='
isPro=$(getEnvConf isPro)
defaultLang=$(getEnvConf defaultLang)
isOnlyEn=$(getEnvConf isOnlyEn)
echo "====isPro=${curPro}====defaultLang=${defaultLang}=====isOnlyEn=${isOnlyEn}======"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
.env及conf.js
# dotenv
库方式
如果是用js实现的话:可以通过dotenv
库实现;
const path = require('path');
const fs = require('fs');
const dotenv = require('dotenv');
const { join } = path;
const { readFileSync, existsSync } = fs;
const { parse } = dotenv;
const loadEnv = (envPath, isBase) => {
if (existsSync(envPath)) {
const parsed = parse(readFileSync(envPath, 'utf-8'));
Object.keys(parsed).forEach(key => {
// eslint-disable-next-line no-prototype-builtins
if (isBase || process.env.hasOwnProperty(key)) {
process.env[key] = parsed[key];
}
});
}
};
// 补充.env.local自定义的参数,umi本身的代码有点问题(node_modules/umi/lib/buildDevOpts.js)
function loadLocalDotEnv() {
const baseEnvPath = join(process.cwd(), '.env');
const localEnvPath = `${baseEnvPath}.local`;
loadEnv(localEnvPath, false);
}
// 补充指定的env
function loadSpecDotEnv(env) {
const baseEnvPath = join(process.cwd(), '../env/.env');
const localEnvPath = `${baseEnvPath}.${env}`;
loadEnv(localEnvPath, false);
}
// 加载env(通过umi不需要做,umi已经加载完)
function loadBaseEnv() {
const baseEnvPath = join(process.cwd(), '../env/.env');
loadEnv(baseEnvPath, true);
}
module.exports = {
loadEnv,
loadBaseEnv,
loadLocalDotEnv,
loadSpecDotEnv,
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
上次更新: 2023/11/17, 05:08:19