bundle.js
3.9 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* bundle.js
*@author dennis
*@createtime 3/26/19
*@description
*/
'use strict'
const exec = require('child_process').exec
const spawn = require('child_process').spawn
const execSync = require('child_process').execSync
const readline = require('readline')
const path = require('path')
const fs = require('fs-extra')
const args = process.argv;
const platform = args.length > 2 ? args[2]: 'ios'
//是否为hotfix包
const isFix = args.length > 3 ? args[3] : false
fs.ensureDirSync('./build/ios_output/')
fs.ensureDirSync('./build/android_output/')
const ouputDir = `./build/${platform}_output`
let bundlename = 'main.jsbundle'
if (platform !== 'ios') {
bundlename = 'index.android.bundle'
}
console.log('remove old bundle files')
execSync(`rm -rf ${ouputDir}/*`)
console.log('remove complete')
let exeStr = `react-native bundle --dev false --platform ${platform} --entry-file index.${platform}.js --bundle-output ${ouputDir}/${bundlename} --assets-dest ${ouputDir} -- reset cache `
console.log(exeStr)
let build = exec(exeStr)
build.stdout.on('data', data => console.log('======: ', data))
build.stderr.on('data',data => console.log('error======: ', data))
build.stdout.on('close', zipPackage)
function zipPackage(e) {
if (e) {
console.log(e)
}
console.log('bundle complete')
if (!isFix) {
return
}
console.log('start zip')
let zipexec = exec(`zip -r main.zip *`,{cwd:ouputDir})
zipexec.stdout.on('data', data => console.log('======: ', data))
zipexec.stdout.on('close', md5)
}
function md5(e) {
if (e) {
console.log(e)
}
console.log('zip complete')
console.log(' start compute md5')
let type = ''
if (platform == 'ios') {
type = 'rn'
}
let md5exec = exec(`python md5.py ${ouputDir}/main.zip ${type}`)
md5exec.stdout.on('data', data => console.log('======: ', data))
md5exec.stdout.on('close', function (e) {
if (e) {
console.log(e)
}
console.log(' complete compute md5')
})
}
function checkHtmlDiff(dirOld, dirNew) {
return new Promise((resolve, reject) => {
exec(`git diff --no-index ${dirOld} ${dirNew}`, {cwd: __dirname}, function (error, stdout, stderr) {
if (!stdout) {
console.log('nothing changed for html files')
resolve(false)
} else {
let diffDir = dirNew.replace('./build/new/','./build/diff/')
console.log(`copying htmls from ${dirNew}, to ${diffDir}`)
fs.copySync(dirNew,diffDir)
console.log('copying done')
resolve(true)
}
})
})
}
function checkAssetsDiff(dirOld, dirNew, callback) {
const cspr = spawn('git', ['diff', '--no-index', dirOld, dirNew], {cwd: __dirname});
const rl = readline.createInterface({ input: cspr.stdout });
let differlist = []
rl.on('line', line => {
// console.log('line====>\n',line)
let regexStart = RegExp('Binary*')
if (regexStart.test(line)) { //查询含有差异文件的行
let regexContent = /and\s.*\s/g
let found = line.match(regexContent)
let foundText = found[0].replace('and', '').replace(/\s/g, '').replace('b/', './')
if (foundText !=='/dev/null') {
differlist.push(foundText)
}
}
})
.on('close', () => {
if (differlist.length > 0) {
console.log('differ file list\n', differlist)
console.log('copying differ files')
differlist.forEach(item => {
console.log(`copy from ${item}, to ./build/diff/assets/img/${path.basename(item)}`)
fs.copySync(item, './build/diff/assets/img/' + path.basename(item))
})
console.log('copying differ done')
}
callback && callback(differlist.length)
})
}