#!/usr/bin/env node var crypto = require('crypto'); var helpers = require('./helpers'); module.exports = function (context) { var path = require('path'); var fs = require('fs'); var cordovaUtil = context.requireCordovaModule('cordova-lib/src/cordova/util'); var platforms = context.requireCordovaModule('cordova-lib/src/platforms/platforms'); var projectRoot = cordovaUtil.isCordova(); process.stdout.write('[完整性检验] Saving a hash for each platforms res \n'); function getHashList() { let arr = str.split(/\n/ig).filter(v => v.trim()) let arr2 = arr.map(v => { // console.log(v) v = v.trim().replace(/\s*put\(/, '').replace(/\)$/, '') vArr = v.split(', ') return { file: vArr[0].replace(/\"/ig, ''), hash: vArr[1].replace(/\"/ig, '') } }) return arr2 } function getPlatformAssets (dir) { var assetsList = []; var list = fs.readdirSync(dir); list.map(function (file) { var filePath = path.join(dir, file); if (fs.statSync(filePath).isDirectory()) { var subDirList = getPlatformAssets(filePath); assetsList = assetsList.concat(subDirList); } if (fs.statSync(filePath).isFile()) { assetsList.push(filePath); } }); return assetsList; } helpers.getPlatformsList(context).forEach(function (platform) { var platformPath = path.join(projectRoot, 'platforms', platform); var platformApi = platforms.getPlatformApi(platform, platformPath); var platformInfo = platformApi.getPlatformInfo(); var platformRes = platformInfo.locations.res; var source = helpers.getFileMapContent(context, platform, 'ResIntegrity'); var content = source.content; console.log("platformRes:",platformRes); if (platform === 'android') { hashes = getPlatformAssets(platformRes).map(function (file) { var fileName = file.replace(/\\/g, '/'); fileName = fileName.replace(platformRes.replace(/\\/g, '/') + '/', ''); var hash; var hashHex; hash = crypto.createHash('sha256'); try { hash.update(fs.readFileSync(file), 'utf8'); } catch (e) { helpers.exit('Unable to read file at path ' + file, e); } hashHex = hash.digest('hex'); if (helpers.isVerbose(context)) { process.stdout.write('Hash: ' + hashHex + ' < ' + fileName + '\n'); } return { // file: Buffer.from(fileName).toString('base64'), file: fileName, hash: hashHex }; }); content = content.replace(/\s*put\("[^"]+",\s"[^"]{64}"\);/g, '') .replace(/hashList\s*=.+\s*new.*(\(\d+\)[^\w]*)\);/, function (match, group) { return match.replace(group, '()\n' + tab()); }) .replace(/hashList\s*=.+\s*new.*(\(.*\))/, function (match, group) { var replace = match.replace(group, '(' + (hashes.length || '') + ')'); if (hashes.length) { replace += ' {{\n' + tab(); hashes.forEach(function (h) { replace += tab(2) + 'put("res/' + h.file + '", "' + h.hash + '");\n' + tab(); }); replace += tab() + '}}'; } return replace; }); // console.log(content) try { fs.writeFileSync(source.path, content, 'utf-8'); } catch (e) { helpers.exit('Unable to write java class source at path ' + source.path, e); } } }); function tab(size) { var str = ''; for (var i = 0; i < (size || 1); i++) { str += ' '; } return str; } };