-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from vtexdocs/add-delete-markdown-files-function
feat: make all scripts run from index.js
- Loading branch information
Showing
1,294 changed files
with
3,160 additions
and
2,013 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,98 @@ | ||
const fs = require('fs'); | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
|
||
// Path to the 'docs' directory | ||
const docsDir = path.resolve(__dirname, '../docs'); | ||
const MAX_CONCURRENT_FILES = 100; | ||
|
||
let activeFiles = 0; | ||
let fileQueue = []; | ||
|
||
// Function to process each Markdown file | ||
const processFile = (filePath) => { | ||
const data = fs.readFileSync(filePath, 'utf8'); | ||
const lines = data.split('\n'); | ||
async function processFile(filePath) { | ||
try { | ||
const data = await fs.readFile(filePath, 'utf8'); | ||
const lines = data.split('\n'); | ||
|
||
if (lines[1] && lines[1].startsWith('title:')) { | ||
let titleLine = lines[1].trim(); | ||
const titleContentMatch = titleLine.match(/^title:\s*(['"])(.*)\1$/); | ||
|
||
if (lines[1] && lines[1].startsWith('title:')) { | ||
let titleLine = lines[1].trim(); | ||
const titleContentMatch = titleLine.match(/^title:\s*(['"])(.*)\1$/); | ||
if (titleContentMatch) { | ||
const currentOuterQuote = titleContentMatch[1]; | ||
const originalTitleContent = titleContentMatch[2]; | ||
let updatedTitleContent; | ||
let updatedTitleLine; | ||
|
||
if (titleContentMatch) { | ||
const currentOuterQuote = titleContentMatch[1]; | ||
const originalTitleContent = titleContentMatch[2]; | ||
let updatedTitleContent; | ||
let updatedTitleLine; | ||
if (currentOuterQuote === `'`) { | ||
if (originalTitleContent.includes('"')) { | ||
updatedTitleContent = originalTitleContent.replace(/"/g, "'"); | ||
updatedTitleLine = `title: "${updatedTitleContent}"`; | ||
} else { | ||
updatedTitleLine = titleLine; | ||
} | ||
} else if (currentOuterQuote === `"`) { | ||
if (originalTitleContent.includes('"')) { | ||
updatedTitleContent = originalTitleContent.replace(/"(?![^"]*")/g, "'"); | ||
updatedTitleLine = `title: "${updatedTitleContent}"`; | ||
} else { | ||
updatedTitleLine = titleLine; | ||
} | ||
} | ||
|
||
if (currentOuterQuote === `'`) { | ||
// Outer quotes are single | ||
if (originalTitleContent.includes('"')) { | ||
// Inner double quotes present | ||
updatedTitleContent = originalTitleContent.replace(/"/g, "'"); | ||
updatedTitleLine = `title: "${updatedTitleContent}"`; | ||
} else { | ||
// No inner double quotes | ||
updatedTitleLine = titleLine; | ||
if (updatedTitleLine && updatedTitleLine !== titleLine) { | ||
lines[1] = updatedTitleLine; | ||
const newData = lines.join('\n'); | ||
await fs.writeFile(filePath, newData, 'utf8'); | ||
// console.log(`Quotes replaced in file: ${filePath}`); | ||
} | ||
} | ||
} | ||
} else if (currentOuterQuote === `"`) { | ||
// Outer quotes are double | ||
if (originalTitleContent.includes('"')) { | ||
// Inner double quotes present | ||
updatedTitleContent = originalTitleContent.replace(/"(?![^"]*")/g, "'"); | ||
updatedTitleLine = `title: "${updatedTitleContent}"`; | ||
} else { | ||
// No inner double quotes | ||
updatedTitleLine = titleLine; | ||
} catch (error) { | ||
console.error(`Error processing file: ${filePath}`, error); | ||
} finally { | ||
activeFiles--; | ||
if (fileQueue.length > 0) { | ||
const nextFile = fileQueue.shift(); | ||
processFile(nextFile); | ||
} | ||
} | ||
|
||
// Only update if necessary | ||
if (updatedTitleLine && updatedTitleLine !== titleLine) { | ||
lines[1] = updatedTitleLine; | ||
const newData = lines.join('\n'); | ||
fs.writeFileSync(filePath, newData, 'utf8'); | ||
console.log(`Updated file: ${filePath}`); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
// Function to recursively process files in directories | ||
const processDirectory = (dirPath) => { | ||
fs.readdir(dirPath, (err, files) => { | ||
if (err) { | ||
console.error('Unable to scan directory:', err); | ||
return; | ||
} | ||
async function processDirectory(dirPath) { | ||
try { | ||
const files = await fs.readdir(dirPath); | ||
|
||
for (const file of files) { | ||
const filePath = path.join(dirPath, file); | ||
const stats = await fs.stat(filePath); | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(dirPath, file); | ||
fs.stat(filePath, (err, stats) => { | ||
if (err) { | ||
console.error('Unable to stat file:', err); | ||
return; | ||
if (stats.isDirectory()) { | ||
await processDirectory(filePath); | ||
} else if (path.extname(file) === '.md') { | ||
if (activeFiles < MAX_CONCURRENT_FILES) { | ||
activeFiles++; | ||
processFile(filePath); | ||
} else { | ||
fileQueue.push(filePath); | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error processing directory:', error); | ||
} | ||
} | ||
|
||
if (stats.isDirectory()) { | ||
processDirectory(filePath); | ||
} else if (path.extname(file) === '.md') { | ||
processFile(filePath); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
async function replaceQuotes() { | ||
console.log("Replacing quotation marks..."); | ||
await processDirectory(docsDir); | ||
|
||
// Wait until all files are processed | ||
while (activeFiles > 0 || fileQueue.length > 0) { | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
} | ||
|
||
console.log("Finished replacing quotation marks in markdown files."); | ||
} | ||
|
||
// Start processing from the docs directory | ||
processDirectory(docsDir); | ||
module.exports = { replaceQuotes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.