Created
June 22, 2023 18:18
-
-
Save isurfer21/18b04ed7def4f00d6b2d5e4eb2f9d082 to your computer and use it in GitHub Desktop.
It can remove the single and multiline comments from any JavaScript or node.js files
This file contains hidden or 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").promises; | |
const path = require("path"); | |
// A class to remove all comments from a js file | |
class CommentRemover { | |
// A constructor that takes a file name as an argument | |
constructor(filePath) { | |
// A property to store the file name | |
this.filePath = filePath; | |
// A property to store the file content | |
this.fileContent = ""; | |
// A property to store the regular expression for matching comments and strings | |
this.commentRegex = /\/\*[\s\S]*?\*\/|\/\/.*|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/g; | |
} | |
// A method to read the file content and store it in the fileContent property | |
async readFile() { | |
try { | |
// Use the fs module to read the file asynchronously | |
const contents = await fs.readFile(this.filePath, { encoding: 'utf8' }); | |
this.fileContent = contents; | |
} catch (err) { | |
// If there is an error, throw it | |
console.error(err.message); | |
} | |
} | |
// A method to remove all comments from the file content and return the result | |
removeComments() { | |
// Use the replace method with the commentRegex to replace all comments with an empty string | |
return this.checkComments(this.fileContent); | |
} | |
// A function to iterate over the matches and check the groups | |
checkComments(text) { | |
var match; | |
while (match = this.commentRegex.exec(text)) { | |
if (match[0].startsWith("/*") || match[0].startsWith("//")) { | |
text = text.replace(match[0], ""); | |
} | |
} | |
return text; | |
}; | |
// A method to write the result to a new file with the same name but with a .clean.js extension | |
async writeFile() { | |
try { | |
// Use the fs module to write the file asynchronously | |
await fs.writeFile(this.filePath + ".clean.js", this.removeComments()); | |
// Otherwise, log a success message | |
console.log("The file has been saved without comments!"); | |
} catch (err) { | |
// If there is an error, throw it | |
console.error(err.message); | |
} | |
} | |
} | |
// A function to print the help menu | |
function printHelp() { | |
console.log("Usage: node script.js <filepath>"); | |
console.log("Options:"); | |
console.log(" -h, --help Show this help message and exit"); | |
} | |
async function main() { | |
// Get the command line arguments | |
let args = process.argv.slice(2); | |
// If there is no argument or the argument is -h or --help, print the help menu and exit | |
if (args.length === 0 || args[0] === "-h" || args[0] === "--help") { | |
printHelp(); | |
process.exit(0); | |
} | |
// Otherwise, get the filePath from the first argument | |
let filePath = args[0]; | |
// If the filepath is a relative path, resolve it from the current working directory | |
if (!path.isAbsolute(filePath)) { | |
filePath = path.resolve(process.cwd(), filePath); | |
} | |
// Do something with the filePath | |
console.log("The filePath is " + filePath); | |
// Create an instance of the CommentRemover class with a sample file name | |
let remover = new CommentRemover(filePath); | |
// Call the readFile method to read the file content | |
await remover.readFile(); | |
// Call the writeFile method to write the result to a new file | |
await remover.writeFile(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment