bundle.js 3.9 KB
/**
 * 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)

        })


}