Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added 'UI based' development functionality #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.json",
"tours": "node dist/index.js",
"dev": "npm run build && npm run tours"
"updateTours": "node dist/updateTours/index.js",
"dev": "npm run build && npm run tours",
"update": "npm run build && npm run updateTours"
},
"keywords": [],
"author": "",
Expand Down
13 changes: 13 additions & 0 deletions src/updateTours/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';
import { processAndUpdateTourFiles } from '../utils';

(async () => {

const rocketChatDir = path.resolve(__dirname, "../../..");
const toursDir = path.resolve(rocketChatDir, ".tours");

const rcGuidedToursDir = path.resolve(__dirname, "../..");
const otherDir = path.join(rcGuidedToursDir, "dist/updateTours/searchStringDir");

await processAndUpdateTourFiles(toursDir, otherDir);
})();
8 changes: 8 additions & 0 deletions src/updateTours/searchStringDir/.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const searchStrings = [
'NOTAPPLICABLE', // when you dont have line number in steps
'1', // It means line number 1, Beginning of the file
'<write unique search string here>',
'<write unique search string here>',
'2', // It means line number 2
'<write unique search string here>'
]
93 changes: 93 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,96 @@ export const slugify = (str: string) =>
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');

/**
* Reads all .tour files in the .tours directory, extracts file paths and indices,
* finds the line numbers of search strings in corresponding files, and updates
* the .tour files with the new line numbers.
*
* @param toursDir - Path to the .tours directory.
* @param otherDir - Path to the directory containing files for search.
*/
export async function processAndUpdateTourFiles(toursDir: string, otherDir: string): Promise<void> {

try {
// Check if directories exist
await fs.access(toursDir);
await fs.access(otherDir);
} catch {
throw new Error(`One of the directories does not exist: ${toursDir} or ${otherDir}`);
}

// Step 1: Read all .tour files
const tourFiles = (await fs.readdir(toursDir)).filter(file => file.endsWith(".tour"));

for (const tourFile of tourFiles) {
const filepathAndIndexArr: { file: string; index: number }[] = [];
const newLineNumbers: string[] = [];

const tourFilePath = path.join(toursDir, tourFile);
const fileContent = JSON.parse(await fs.readFile(tourFilePath, "utf-8"));

fileContent.steps.forEach((step: any, index: number) => {
filepathAndIndexArr.push({
file: step.file || "NOTPRESENT",
index,
});
});

const searchStringsPath = path.join(otherDir, tourFile.replace(/\.tour$/, ".js"));
try {
await fs.access(searchStringsPath);
} catch {
// console.warn(`Search strings file does not exist for: ${tourFile}`);
continue;
}

// Dynamically import the search strings
const { searchStrings } = await import(searchStringsPath);

// Process each file and find new line numbers
for (let i = 0; i < filepathAndIndexArr.length; i++) {
const { file, index } = filepathAndIndexArr[i];

if (file === "NOTPRESENT" || searchStrings[index] === "NOTAPPLICABLE") {
newLineNumbers.push("NA");
continue;
}

if (searchStrings[index] === "1" || searchStrings[index] === "2") {
newLineNumbers.push(searchStrings[index]);
continue;
}

const rootDir = path.resolve(__dirname, "../..");
const fullPath = path.join(rootDir, file);

try {
await fs.access(fullPath);
} catch {
newLineNumbers.push("NA");
continue;
}

const targetFileContent = await fs.readFile(fullPath, "utf-8");
const searchString = searchStrings[index];
const lines = targetFileContent.split("\n");

// Find line number for the search string
const lineNumber = lines.findIndex((line) => line.includes(searchString));
newLineNumbers.push(lineNumber !== -1 ? (lineNumber + 1).toString() : "NA");
}

// Step 3: Update the .tour file with new line numbers
fileContent.steps.forEach((step: any, index: number) => {
if (step.line !== undefined) {
const lineNumber = newLineNumbers[index];
step.line = lineNumber !== "NA" ? parseInt(lineNumber, 10) : lineNumber; // Convert to number if valid
}
});

// Write the updated content back to the .tour file
await fs.writeFile(tourFilePath, JSON.stringify(fileContent, null, 2));
}

console.log("Tours Updated successfully");
}