Advertisement
WILDAN_IZZUDIN

[JS] MASS STRING REPLACER

Dec 13th, 2022
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs'),
  2.    resolve = require('path').resolve,
  3.    promisify = require('util').promisify,
  4.    readdir = promisify(fs.readdir),
  5.    stat = promisify(fs.stat),
  6.    path = require('path'),
  7.    colors = require('@colors/colors/safe')
  8.  
  9. Scandir = async (dir) => {
  10.    let subdirs = await readdir(dir)
  11.    let files = await Promise.all(subdirs.map(async (subdir) => {
  12.       let res = resolve(dir, subdir)
  13.       return (await stat(res)).isDirectory() ? Scandir(res) : res
  14.    }))
  15.    return files.reduce((a, f) => a.concat(f), [])
  16. }
  17.  
  18.  
  19. Scandir('./').then(files => {
  20.    if (files.length == 0) return console.log(colors.red('Files empty'))
  21.    let old = new Date()
  22.    files.map(v => {
  23.       let str = fs.readFileSync(v, 'utf-8')
  24.          .replace(new RegExp('[OLD_STRING]', 'g'), '[NEW_STRING')
  25.          .replace(new RegExp('[OLD_STRING]', 'g'), 'NEW_STRING')
  26.          // and more ...
  27.       fs.writeFileSync(v, str, 'utf-8')
  28.    })
  29.    let result = `\n${files.map(v => `${colors.cyan('-')} ${colors.yellow(`${v.split('/')[v.split('/').length - 2]}/${v.split('/')[v.split('/').length - 1]}`)}`).join('\n')}\n\n`
  30.    result += colors.green(`✅ ${files.length} files, completed within ${colors.green((new Date - old) * 1)} ms`)
  31.    console.log(result)
  32. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement