Created
February 16, 2024 15:50
-
-
Save vv13/937b77b40085dff0118e3f4c1a645da5 to your computer and use it in GitHub Desktop.
Simple remove comments in project
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"); | |
const IgnoreFiles = ["node_modules", ".idea", ".vscode", "package-lock.json", "yarn.lock"]; | |
class RemoveComments { | |
sourcePath = ""; | |
targetPath = ""; | |
constructor(sourcePath, targetPath) { | |
this.sourcePath = sourcePath; | |
this.targetPath = targetPath; | |
} | |
static deleteFolderRecursive(folderPath) { | |
if (fs.existsSync(folderPath)) { | |
fs.readdirSync(folderPath).forEach((file, index) => { | |
const curPath = path.join(folderPath, file); | |
if (fs.lstatSync(curPath).isDirectory()) { | |
this.deleteFolderRecursive(curPath); | |
} else { | |
fs.unlinkSync(curPath); | |
} | |
}); | |
fs.rmdirSync(folderPath); | |
} | |
} | |
static copyDirectorySync(source, target) { | |
// 创建目标目录 | |
if (!fs.existsSync(target)) { | |
fs.mkdirSync(target, { recursive: true }); | |
} | |
// 获取源目录中的所有文件和子目录 | |
const files = fs.readdirSync(source); | |
// 遍历源目录中的所有文件和子目录 | |
files.forEach((file) => { | |
if (IgnoreFiles.includes(file)) return; | |
const sourcePath = path.join(source, file); | |
const targetPath = path.join(target, file); | |
// 获取文件/目录的状态 | |
const stats = fs.statSync(sourcePath); | |
if (stats.isDirectory()) { | |
// 递归复制子目录 | |
this.copyDirectorySync(sourcePath, targetPath); | |
} else { | |
// 复制文件 | |
fs.copyFileSync(sourcePath, targetPath); | |
} | |
}); | |
} | |
static removeCommentsFromFile(filePath) { | |
let fileContent = fs.readFileSync(filePath, "utf8"); | |
// 移除单行注释,eg: const a = 1; // comment | |
fileContent = fileContent.replace(/\/\/.*$/gm, ""); | |
// 移除单行注释 | |
fileContent = fileContent.replace(/^\/\/.*\n?/gm, ""); | |
// 移除多行注释 | |
fileContent = fileContent.replace(/\/\*[\s\S]*?\*\//gm, ""); | |
// 移除连续的空白行 | |
fileContent = fileContent.replace(/\n+/g, "\n"); | |
fs.writeFileSync(filePath, fileContent, "utf8"); | |
} | |
static removeCommentsFromDirectory(directoryPath) { | |
fs.readdir(directoryPath, (err, files) => { | |
if (err) { | |
console.error("Error reading directory:", err); | |
return; | |
} | |
files.forEach((file) => { | |
const filePath = path.join(directoryPath, file); | |
const stats = fs.statSync(filePath); | |
if (stats.isDirectory()) { | |
this.removeCommentsFromDirectory(filePath); | |
} else if (stats.isFile() && path.extname(filePath) === ".js") { | |
this.removeCommentsFromFile(filePath); | |
} | |
}); | |
}); | |
} | |
run() { | |
if (fs.existsSync(this.targetPath)) { | |
RemoveComments.deleteFolderRecursive(this.targetPath); | |
} | |
RemoveComments.copyDirectorySync(this.sourcePath, this.targetPath); | |
RemoveComments.removeCommentsFromDirectory(this.targetPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After removing comments, do format to fix empty-line problems:
npx prettier --write "**/*.js"