diff --git a/package.json b/package.json index e157312..280eefc 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/src/updateTours/index.ts b/src/updateTours/index.ts new file mode 100644 index 0000000..1318900 --- /dev/null +++ b/src/updateTours/index.ts @@ -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); +})(); diff --git a/src/updateTours/searchStringDir/.example.ts b/src/updateTours/searchStringDir/.example.ts new file mode 100644 index 0000000..0a78874 --- /dev/null +++ b/src/updateTours/searchStringDir/.example.ts @@ -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 + '', + '', + '2', // It means line number 2 + '' +] \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 6123355..3a45abe 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { + + 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"); +}