This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.js
82 lines (73 loc) · 2.35 KB
/
prebuild.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* eslint-disable */
/**
* 此脚本用于在开始打包前处理配置信息
* @author crrashh1542
* @version 1.5.0
*/
// STEP1 -------- 导入依赖
const fs = require('fs')
const childProcess = require('child_process')
const moment = require('moment')
// STEP2 -------- 获取构建时间
function getTime() {
const buildTime = moment(Date.now()).format()
console.log('[buildInfo] 已获取构建时间:' + buildTime)
return buildTime
}
// STEP3 -------- 获取构建 hash
function getHash(params) {
const buildHash = childProcess.execSync('git rev-parse --short HEAD', { 'encoding': 'utf8' }).split('\n')[0]
console.log('[buildInfo] 已获取构建 commit hash:' + buildHash)
return buildHash
}
// STEP4 -------- 获取构建环境
function getEnv() {
let buildEnv
if (process.env.NODE_ENV === "development") {
buildEnv = 'dev'
} else if (process.env.NODE_ENV === "production") {
buildEnv = 'prod'
} else {
buildEnv = process.env.NODE_ENV
}
console.log('[buildInfo] 已获取构建环境:' + buildEnv)
return buildEnv
}
// STEP5 -------- 获取构建次数
function getBuild() {
const buildHash = childProcess.execSync('git rev-list HEAD --count', { 'encoding': 'utf8' }).split('\n')[0]
console.log('[buildInfo] 已获取构建数:' + buildHash)
return buildHash
}
// STEP6 -------- 组装并输出到文件
function writeInfo() {
// 组装要输出的内容
const content = ` {
"time": "` + getTime() + `",
"hash": "` + getHash() + `",
"env": "` + getEnv() + `",
"build": ` + getBuild() + `
}`
// 新建 temp 文件夹
fs.mkdir('./temp', err => {
if (err === null) {
console.log('\n[buildInfo] temp 文件夹创建成功!')
} else {
if (fs.existsSync('temp')) {
console.log('\n[buildInfo] temp 文件夹已存在!')
} else {
console.warn('\n[buildInfo] 构建信息写入失败,详情请参阅:\n' + err)
}
}
})
// 将 buildInfo 内容写入文件
fs.writeFile('./temp/buildInfo.json', content, err => {
if (err === null) {
console.log('[buildInfo] 构建信息写入成功!')
} else {
console.log('[buildInfo] 构建信息写入失败,详情请参阅:\n' + err)
}
})
}
// STEP6 -------- 导出函数
module.exports = writeInfo