From 804134edac64bb9937e0e50929658ce75ddb456b Mon Sep 17 00:00:00 2001 From: Khadreal Date: Mon, 30 Dec 2024 15:22:49 +0100 Subject: [PATCH] Remove unused variable, clean up report file -- #143 --- config/wp.config.sample.ts | 6 +-- report.ts | 96 +++++++++++++++++++++++++++++--------- 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/config/wp.config.sample.ts b/config/wp.config.sample.ts index f78fec9..6eb476b 100755 --- a/config/wp.config.sample.ts +++ b/config/wp.config.sample.ts @@ -44,8 +44,7 @@ const { WP_SSH_USERNAME = '', WP_SSH_ADDRESS = '', WP_SSH_KEY = '', - WP_SSH_ROOT_DIR = '', - GITHUB_PAT = '' + WP_SSH_ROOT_DIR = '' } = process.env; /** @@ -99,6 +98,5 @@ export { WP_SSH_ADDRESS, WP_SSH_KEY, WP_SSH_ROOT_DIR, - SCENARIO_URLS, - GITHUB_PAT + SCENARIO_URLS }; \ No newline at end of file diff --git a/report.ts b/report.ts index b3afce8..2d231d0 100644 --- a/report.ts +++ b/report.ts @@ -1,30 +1,82 @@ import path from "path"; -import {execFile} from "child_process"; +import { execFile } from "child_process"; +import {promises as fs} from "fs"; -const scriptPath = path.resolve('move_report.sh'); +/** + * Get the script path and arguments for moving the report. + * + * @return {Promise<{ scriptPath: string; args: string[] }>} + */ +async function initialization(): Promise<{ scriptPath: string; args: string[] }> { + const scriptPath = path.resolve('move_report.sh'); + const destination = process.argv[2]; + const newName = process.argv[3]; -const destination = process.argv[2]; -const newName = process.argv[3]; -if (!destination) { - console.error('Usage: npm run push-report [new_name]'); - process.exit(1); + if (!destination) { + console.error('Usage: npm run push-report [new_name]'); + process.exit(1); + } + + // Prepare the arguments for the script + const args = [destination]; + if (newName) { + args.push(newName); + } + + return { scriptPath, args }; +} +/** + * Check if script has permission and if not add the right permission, so it can be executed. + * @param {string} scriptPath The script path. + * + * @return {Promise} +*/ +export async function checkScriptPermission(scriptPath: string): Promise { + try { + await fs.access(scriptPath, fs.constants.X_OK); + console.log(`Script already has execute permission: ${scriptPath}`); + } catch (err) { + console.log(`Adding execute permission to ${scriptPath}`); + try { + await fs.chmod(scriptPath, 0o755); + console.log(`Execute permission added successfully: ${scriptPath}`); + } catch (chmodErr) { + console.error(`Failed to set execute permission: ${chmodErr.message}`); + throw chmodErr; + } + } } -// Prepare the arguments for the script -const args = [destination]; -if (newName) { - args.push(newName); +/** + * Move the test-results directory to the given directory. + * @param {string} scriptPath The script path. + * @param args Script argument. + * + * @return {Promise} + */ +export async function moveTestReport(scriptPath: string, args: readonly string[]): Promise { + execFile(scriptPath, args, (error, stdout, stderr) => { + if (error) { + console.error(`Error: ${error.message}`); + return; + } + if (stderr) { + console.error(`Stderr: ${stderr}`); + return; + } + console.log(`Output: ${stdout}`); + }); } -// Execute the bash script -execFile(scriptPath, args, (error, stdout, stderr) => { - if (error) { - console.error(`Error: ${error.message}`); - return; +(async (): Promise => { + try { + const { scriptPath, args } = await initialization(); + await checkScriptPermission(scriptPath); + await moveTestReport(scriptPath, args); + } catch (err) { + console.error(`Failed to execute the script: ${err.message}`); + process.exit(1); } - if (stderr) { - console.error(`Stderr: ${stderr}`); - return; - } - console.log(`Output: ${stdout}`); -}); \ No newline at end of file + + process.exit(0); +})(); \ No newline at end of file