misc: move execCommand to exec

This commit is contained in:
KOWX712
2025-05-04 19:04:03 +08:00
parent 5a8097b40c
commit 770f107559
8 changed files with 374 additions and 413 deletions

View File

@@ -1,4 +1,4 @@
import { basePath, execCommand, showPrompt } from './main.js';
import { basePath, exec, showPrompt } from './main.js';
const overlay = document.getElementById('security-patch-overlay');
const overlayContent = document.querySelector('.security-patch-card');
@@ -24,32 +24,26 @@ const hideSecurityPatchDialog = () => {
}
// Function to handle security patch operation
async function handleSecurityPatch(mode, value = null) {
function handleSecurityPatch(mode, value = null) {
if (mode === 'disable') {
try {
await execCommand(`
rm -f /data/adb/tricky_store/security_patch_auto_config
rm -f /data/adb/tricky_store/security_patch.txt
`);
showPrompt('security_patch.value_empty');
return true;
} catch (error) {
showPrompt('security_patch.save_failed', false);
return false;
}
exec(`
rm -f /data/adb/tricky_store/security_patch_auto_config || true
rm -f /data/adb/tricky_store/security_patch.txt || true
`).then(({ errno }) => {
const result = errno === 0;
showPrompt(result ? 'security_patch.value_empty' : 'security_patch.save_failed', result);
return result;
});
} else if (mode === 'manual') {
try {
await execCommand(`
rm -f /data/adb/tricky_store/security_patch_auto_config
echo "${value}" > /data/adb/tricky_store/security_patch.txt
chmod 644 /data/adb/tricky_store/security_patch.txt
`);
showPrompt('security_patch.save_success');
return true;
} catch (error) {
showPrompt('security_patch.save_failed', false);
return false;
}
exec(`
rm -f /data/adb/tricky_store/security_patch_auto_config || true
echo "${value}" > /data/adb/tricky_store/security_patch.txt
chmod 644 /data/adb/tricky_store/security_patch.txt
`).then(({ errno }) => {
const result = errno === 0;
showPrompt(result ? 'security_patch.save_success' : 'security_patch.save_failed', result);
return result;
});
}
}
@@ -57,17 +51,17 @@ async function handleSecurityPatch(mode, value = null) {
async function loadCurrentConfig() {
let allValue, systemValue, bootValue, vendorValue;
try {
const autoConfig = await execCommand('[ -f /data/adb/tricky_store/security_patch_auto_config ] && echo "true" || echo "false"');
if (autoConfig.trim() === 'true') {
const { errno } = await exec('[ -f /data/adb/tricky_store/security_patch_auto_config ]');
if (errno === 0) {
allValue = null;
systemValue = null;
bootValue = null;
vendorValue = null;
} else {
// Read values from tricky_store if auto_config is 0
const trickyResult = await execCommand('cat /data/adb/tricky_store/security_patch.txt');
if (trickyResult) {
const trickyLines = trickyResult.split('\n');
// Read values from tricky_store if manual mode
const { stdout } = await exec('cat /data/adb/tricky_store/security_patch.txt');
if (stdout.trim() !== '') {
const trickyLines = stdout.split('\n');
for (const line of trickyLines) {
if (line.startsWith('all=')) {
allValue = line.split('=')[1] || null;
@@ -222,27 +216,25 @@ export function securityPatch() {
});
// Auto config button
autoButton.addEventListener('click', async () => {
try {
const output = await execCommand(`sh ${basePath}/common/get_extra.sh --security-patch`);
if (output.trim() === "not set") {
showPrompt('security_patch.auto_failed', false);
} else {
await execCommand(`touch /data/adb/tricky_store/security_patch_auto_config`);
// Reset inputs
allPatchInput.value = '';
systemPatchInput.value = '';
bootPatchInput.value = '';
vendorPatchInput.value = '';
autoButton.addEventListener('click', () => {
exec(`sh ${basePath}/common/get_extra.sh --security-patch`)
.then(({ errno, stdout }) => {
if (errno !== 0 || stdout.trim() === "not set") {
showPrompt('security_patch.auto_failed', false);
} else {
exec(`touch /data/adb/tricky_store/security_patch_auto_config`)
// Reset inputs
allPatchInput.value = '';
systemPatchInput.value = '';
bootPatchInput.value = '';
vendorPatchInput.value = '';
checkAdvanced(false);
showPrompt('security_patch.auto_success');
}
} catch (error) {
showPrompt('security_patch.auto_failed', false);
}
hideSecurityPatchDialog();
loadCurrentConfig();
checkAdvanced(false);
showPrompt('security_patch.auto_success');
}
hideSecurityPatchDialog();
loadCurrentConfig();
});
});
// Save button
@@ -252,7 +244,7 @@ export function securityPatch() {
const allValue = allPatchInput.value.trim();
if (!allValue) {
// Save empty value to disable auto config
await handleSecurityPatch('disable');
handleSecurityPatch('disable');
hideSecurityPatchDialog();
return;
}
@@ -261,7 +253,7 @@ export function securityPatch() {
return;
}
const value = `all=${allValue}`;
const result = await handleSecurityPatch('manual', value);
const result = handleSecurityPatch('manual', value);
if (result) {
// Reset inputs
systemPatchInput.value = '';
@@ -276,7 +268,7 @@ export function securityPatch() {
if (!bootValue && !systemValue && !vendorValue) {
// Save empty values to disable auto config
await handleSecurityPatch('disable');
handleSecurityPatch('disable');
hideSecurityPatchDialog();
return;
}
@@ -302,7 +294,7 @@ export function securityPatch() {
vendorValue ? `vendor=${vendorValue}` : ''
].filter(Boolean);
const value = config.filter(Boolean).join('\n');
const result = await handleSecurityPatch('manual', value);
const result = handleSecurityPatch('manual', value);
if (result) {
// Reset inputs
allPatchInput.value = '';
@@ -314,19 +306,22 @@ export function securityPatch() {
// Get button
getButton.addEventListener('click', async () => {
try {
showPrompt('security_patch.fetching');
await new Promise(resolve => setTimeout(resolve, 200));
const output = await execCommand(`sh ${basePath}/common/get_extra.sh --get-security-patch`);
showPrompt('security_patch.fetched', true, 1000);
checkAdvanced(true);
showPrompt('security_patch.fetching');
setTimeout(() => {
exec(`sh ${basePath}/common/get_extra.sh --get-security-patch`)
.then(({ errno, stdout }) => {
if (errno !== 0) {
showPrompt('security_patch.get_failed', false);
} else {
showPrompt('security_patch.fetched', true, 1000);
checkAdvanced(true);
allPatchInput.value = output.replace(/-/g, '');
systemPatchInput.value = 'prop';
bootPatchInput.value = output;
vendorPatchInput.value = output;
} catch (error) {
showPrompt('security_patch.get_failed', false);
}
allPatchInput.value = stdout.replace(/-/g, '');
systemPatchInput.value = 'prop';
bootPatchInput.value = stdout;
vendorPatchInput.value = stdout;
}
})
}, 200);
});
}