const debug = false;
const childProcess = require('child_process');
const fs = require('fs');
const iconv = require('iconv-lite');
const shell = require('shelljs');
const current_dir = __dirname;
const node_exe = current_dir + '\\node.exe';
const asar_exe = current_dir + '\\node_modules\\asar\\bin\\asar.js';
function getInstallLocation() {
return new Promise(function(resolve, reject) {
childProcess.exec('REG QUERY "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\2b768da7-d7f2-50e7-bc2f-111a10a890a5" /v "UninstallString"', {encoding: 'buffer'}, function(error,stdout,stderr) {
if(error) {
return reject(error);
}
let t = iconv.decode(stdout, 'gbk');
let t1 = iconv.encode(t, 'utf-8').toString().trim();
let arr = t1.split('REG_SZ');
let path = arr.pop();
path = path.split('\\');
path.pop();
return resolve(path.join('\\').replace('"', '').trim());
});
});
}
function act(installLocation) {
const resource_dir = installLocation + '\\resources';
const index_html = resource_dir + '\\app\\build\\renderer\\index.html';
let accScript = debug ? '<script>(function() {let s = document.createElement("script");s.src = "http://l.qt.hk/xmly-live-acc.js?_t=" + Math.random();document.body.appendChild(s);})();</script>' : '<script>(function() {let s = document.createElement("script");s.src = "https://www.qt06.com/user-scripts/xmly-live-acc.js?_t=" + Math.random();document.body.appendChild(s);})();</script>';
shell.cd(resource_dir);
shell.rm('-rf', 'app');
shell.exec('"' + node_exe + '" "' + asar_exe + '" e app.asar app');
let f = fs.readFileSync(index_html, 'utf8');
if(!f.includes(accScript)) {
console.log('add accessible scripts');
f = f.replace('</body></html>', accScript + '</body></html>');
fs.writeFileSync(index_html, f, 'utf8');
console.log('accessible scripts has added.');
} else {
shell.echo('do not need add accessible scripts');
shell.echo('已经优化过，无需重复优化。');
}
shell.exec('"' + node_exe + '" "' + asar_exe + '" pack app app.asar --unpack-dir "{build/libs/zegoliveroom,build/audios}"');
shell.rm('-rf', 'app');
shell.cd(current_dir);
shell.echo('done.');
shell.echo('完成。');
shell.echo('祝您使用愉快！');
}
getInstallLocation().then(function(installLocation) {
act(installLocation);
}).catch(function(err) {
console.log(err);
});
