Created
December 12, 2022 16:29
-
-
Save perki/bd54a3cec3aef4822dcb9fb1cfb71f0d to your computer and use it in GitHub Desktop.
Realign all comments with heading /**
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
const fs = require('fs'); | |
const path = require('path'); | |
function processFile (filePath) { | |
const content = fs.readFileSync(filePath, 'utf-8'); | |
const lines = content.split('\n'); | |
let identBy = -1; | |
let changeCount = 0; | |
for (let i = 0; i < lines.length; i++) { | |
const line = lines[i]; | |
const endPos = line.indexOf('*/'); | |
if (identBy < 0) { | |
const startPos = line.indexOf('/**'); | |
if (endPos > 0) continue; // single line comment | |
if (startPos < 0) continue; | |
// all characters before are spaces.. | |
if (line.substring(0, startPos).trim().length === 0) identBy = startPos + 1; | |
continue; | |
} | |
const commentPos = line.indexOf('*'); | |
if (commentPos < 0) continue; | |
if (commentPos !== identBy) { | |
changeCount++; | |
lines[i] = ' '.substring(0, identBy) + line.substring(commentPos); | |
} | |
if (endPos > -1) { | |
identBy = -1; | |
} | |
} | |
if (changeCount > 0) { | |
const newContent = lines.join('\n'); | |
fs.writeFileSync(filePath, newContent, 'utf-8'); | |
console.log(changeCount + '\t' + filePath); | |
} | |
} | |
async function loop (dirPath) { | |
const dir = await fs.promises.opendir(dirPath); | |
for await (const dirent of dir) { | |
const direntPath = path.join(dirPath, dirent.name); | |
if (dirent.isDirectory()) { | |
if (dirent.name.indexOf('node_modules') < 0) loop(direntPath); | |
} else { | |
if (dirent.name.endsWith('.js')) { | |
processFile(direntPath); | |
} | |
} | |
} | |
} | |
(async () => { | |
loop(__dirname); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment