Skip to content

Commit

Permalink
chore: semrush module adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
robsongajunior committed Jan 26, 2025
1 parent cb4b377 commit aedd5b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 158 deletions.
116 changes: 0 additions & 116 deletions backend/semrush-replace-url/index.content.js

This file was deleted.

70 changes: 28 additions & 42 deletions backend/semrush-replace-url/index.i18n.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
import { parse } from 'csv-parse';
import { promises as fs } from 'fs';
import { createReadStream } from 'fs';
import path from 'path';

import { wwwazioncom, removeHostAndLangFromUrl, isFromRoot } from './helpers/url.js';
import { read as readcsv } from './helpers/csv.js';

let counterFoundLinks = 0;
const PATH = {
csv: './www.azion.com_permanent_redirects_20241225.csv',
i18n: `${process.env.OLDPWD}/src/i18n`
};
const wwwazioncom = 'https://www.azion.com';
const removeLangFromUrl = (url) => url.replace('/pt-br', '').replace('/en', '');
const removeHostFromUrl = (url) => url.replace(wwwazioncom, '');
const removeHostAndLangFromUrl = (url) => removeLangFromUrl(removeHostFromUrl(url));
const isFromRoot = (url) => url === wwwazioncom;

async function loadRedirects() {
const redirects = [];
const parser = createReadStream(PATH.csv).pipe(
parse({
columns: true,
skip_empty_lines: true,
})
);

try {
for await (const record of parser) {
redirects.push({
page: record.page,
initialUrl: record.initial_url,
destinationUrl: record.destination_url,
statusCode: record.status,
discovered: record.discovered
});
}
return redirects;
} catch (error) {
throw new Error(`Error loading redirects: ${error.message}`);
}
}

async function processFile(filePath, redirects) {
try {
const content = await fs.readFile(filePath, 'utf-8');
let newContent = content;
let newContent = await fs.readFile(filePath, 'utf-8');
let fileModified = false;

for (const item of redirects) {
const url30x = isFromRoot(item.initialUrl) ? wwwazioncom : removeHostAndLangFromUrl(item.initialUrl);
const url30x = isFromRoot(item.initialUrl) ? wwwazioncom() : removeHostAndLangFromUrl(item.initialUrl);
const url200 = removeHostAndLangFromUrl(item.destinationUrl);
const isRoot = isFromRoot(url30x);
const rgx = new RegExp(`'${url30x}'`, 'g');

const rgx = new RegExp(`'${url30x}'`, 'g');
const contentMatch = newContent.match(rgx);

if (!contentMatch) continue;
const rgxSingleQuote = new RegExp(`'${url30x}'`, 'g');
const rgxDoubleQuote = new RegExp(`"${url30x}"`, 'g');

const contentMatchSingleQuote = newContent.match(rgxSingleQuote);
const contentMatchDoubleQuote = newContent.match(rgxDoubleQuote);

if (!contentMatchSingleQuote || !contentMatchDoubleQuote) continue;

counterFoundLinks++;
fileModified = true;
Expand All @@ -67,10 +44,19 @@ async function processFile(filePath, redirects) {
processedCount: counterFoundLinks
});

newContent = newContent.replace(
isRoot ? /'https\:\/\/www\.azion\.com\/'/ : rgx,
`'${url200}'`
);
if(contentMatchSingleQuote.length) {
newContent = newContent.replace(
isRoot ? /'https\:\/\/www\.azion\.com\/'/ : rgx,
`'${url200}'`
);
}

if(contentMatchDoubleQuote.length) {
newContent = newContent.replace(
isRoot ? /"https\:\/\/www\.azion\.com\/"çççç/ : rgx,
`'${url200}'`
);
}
}

if (fileModified) {
Expand Down Expand Up @@ -106,7 +92,7 @@ async function processDirectory(directory, redirects) {
async function main() {
try {
console.log('[INFO] Loading redirects...');
const redirects = await loadRedirects();
const redirects = await readcsv(PATH.csv);
console.log(`[INFO] Loaded ${redirects.length} redirects`);

console.log('[INFO] Starting directory processing...');
Expand Down

0 comments on commit aedd5b6

Please sign in to comment.