Script para remover os console.logs do projeto

Jarod Cavalcante
2022-10-11T18:12:46Z

removeLogs.js

| const fs = require('fs');
| const path = require('path');
| const readline = require('readline');
|
| const rl = readline.createInterface({
| input: process.stdin,
| output: process.stdout,
| terminal: false
| });
|
| rl.on('line', (folder) => {
| let _folder = `${__dirname}/${folder}/`
|
| if (!_folder) {
| _folder = process.cwd()
| }
|
| readFiles(_folder, function (err) {
| throw err;
| });
| });
|
| const validExtFiles = [
| '.js', '.ts', '.tsx'
| ]
|
| const invalidFolders = [
| '.git', '.config',
| ]
|
|
| function readFiles(dirname, onError) {
| fs.readdir(dirname, function (err, filenames) {
| if (err) {
| onError(err);
| return;
| }
|
|
| const files = filenames.filter((res) => {
| const file = fs.lstatSync(path.resolve(dirname, res))
| if (validExtFiles.includes((path.extname(res)))) {
| if (file.isFile())
| return file
| }
| })
|
| files.forEach(function (filename) {
| fs.readFile(dirname + filename, 'utf-8', function (err, content) {
| if (err) {
| onError(err);
| return;
| }
| console.log(`Acessando arquivo ${filename}`)
| const newValue = content.replace(/(console\.log\(([^)]+)\);)/g, '');
|
| fs.writeFile(dirname + filename, newValue, 'utf-8', function (err) {
| if (err) throw err;
| });
| });
| });
|
| const folders = filenames.filter((res) => fs.lstatSync(path.resolve(dirname, res)).isDirectory())
|
| folders.forEach((folder) => {
| if (!invalidFolders.includes(folder)) {
| console.log(`acessando pasta ${folder}`)
| readFiles(`${dirname}/${folder}/`, function (err) {
| throw err;
| });
| }
| })
|
| rl.close()
| });
| }