Skip to content

Commit

Permalink
Refactor script and command, revert config modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Jan 3, 2025
1 parent e48271e commit 34388e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 73 deletions.
8 changes: 2 additions & 6 deletions config/wp.config.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const WP_ADMIN_USER = {
* WP_SSH_ADDRESS: string;
* WP_SSH_KEY: string;
* WP_SSH_ROOT_DIR: string;
* ENVIRONMENT_USERNAME: string;
* }}
*/
const {
Expand All @@ -43,8 +42,7 @@ const {
WP_SSH_USERNAME = '',
WP_SSH_ADDRESS = '',
WP_SSH_KEY = '',
WP_SSH_ROOT_DIR = '',
ENVIRONMENT_USERNAME = ''
WP_SSH_ROOT_DIR = ''
} = process.env;

/**
Expand Down Expand Up @@ -81,7 +79,6 @@ const {
* noJsLlcss: string;
* elementorLlcss: string;
* },
* ENVIRONMENT_USERNAME: string;
* }}
*/
export {
Expand All @@ -95,6 +92,5 @@ export {
WP_SSH_USERNAME,
WP_SSH_ADDRESS,
WP_SSH_KEY,
WP_SSH_ROOT_DIR,
ENVIRONMENT_USERNAME
WP_SSH_ROOT_DIR
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"scripts": {
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test:e2e": "$npm_package_config_testCommand && npm run push-report --tag=e2e",
"test:smoke": "$npm_package_config_testCommand --tags @smoke && npm run push-report --tag=smoke",
"test:e2e": "$npm_package_config_testCommand && npm run push-report --tag=$npm_config_tag",
"test:smoke": "$npm_package_config_testCommand --tags @smoke && npm run push-report --tag=$npm_config_tag",
"test:local": "$npm_package_config_testCommand --tags @local",
"test:online": "$npm_package_config_testCommand --tags @online",
"test:vr": "$npm_package_config_testCommand --tags @vr",
Expand Down
75 changes: 10 additions & 65 deletions report.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
import path from "path";
import {promises as fs} from "fs";
import {ENVIRONMENT_USERNAME} from "./config/wp.config";

interface MoveReportOptions {
destinationDir?: string;
newName?: string;
}

/**
* Generates a default name using timestamp and username
*
* @returns string Generated name in format "YYYYMMDD_HHMMSS_username"
*/
async function generateDefaultName() : Promise<string> {
const timestamp = new Date().toISOString()
.replace(/[-:]/g, '')
.replace(/T/, '_')
.replace(/\..+/, '');

const username = ENVIRONMENT_USERNAME || 'unknown';
return `${timestamp}_${username}`;
}

/**
* Moves test results to a destination directory with optional renaming
*
* @param options Configuration options for moving the report
* @param {string} testTag Tag name of the report generated.
* @returns Promise<string> Path where the folder was moved to
*/
export async function moveTestReport(options: MoveReportOptions = {}): Promise<string> {
export async function moveTestReport(testTag: string): Promise<string> {
const SOURCE_FOLDER = 'test-results';
const DEFAULT_DESTINATION = '/var/shared/rocket-e2e-reports';
const destinationDir = '/var/shared/rocket-e2e-reports';

// Validate source folder exists
try {
Expand All @@ -39,20 +18,14 @@ export async function moveTestReport(options: MoveReportOptions = {}): Promise<s
throw new Error(`Source folder '${SOURCE_FOLDER}' does not exist`);
}

// Determine destination directory
const destinationDir = options.destinationDir || DEFAULT_DESTINATION;

// Ensure destination directory exists
try {
await fs.access(destinationDir);
} catch (error) {
throw new Error(`Destination directory '${destinationDir}' does not exist`);
}

// Generate new folder name if not provided
const newName = options.newName || await generateDefaultName();

const newTestReportPath = path.join(destinationDir, newName);
const newTestReportPath = path.join(destinationDir, testTag);

try {
await fs.rename(SOURCE_FOLDER, newTestReportPath);
Expand All @@ -63,44 +36,16 @@ export async function moveTestReport(options: MoveReportOptions = {}): Promise<s
}
}

// Example of how to update your main execution code:
export async function main(): Promise<void> {
(async (): Promise<void> => {
try {
const destination = process.argv[2];
const tag = process.env.npm_config_tag;

const options: MoveReportOptions = {
destinationDir: destination,
newName: tag ? `${tag}_test` : undefined
};

const newPath = await moveTestReport(options);
console.log(`Folder successfully moved to '${newPath}'`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}

/**
* Get the script path and arguments for moving the report.
*
* @return MoveReportOptions
*/
async function initialization(): Promise<MoveReportOptions> {
const destination = process.argv[2];
const tag = process.env.npm_config_tag;

return {
destinationDir: destination,
newName: tag ? `${tag}_test` : undefined
};
}
//If tag is not provided, then the report shouldn't be moved or renamed.
if(! tag) {
process.exit(1);
}

(async (): Promise<void> => {
try {
const options = await initialization();
await moveTestReport(options);
await moveTestReport(tag);
} catch (err) {
console.error(`Failed to execute the script: ${err.message}`);
process.exit(1);
Expand Down

0 comments on commit 34388e6

Please sign in to comment.