Advertisement
metalni

Auto import shiz

Feb 20th, 2024
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { exec } = require("child_process");
  2. const { readFile, writeFile } = require('node:fs/promises');
  3.  
  4. exec("grep -Rl \"t(LABELS.\" .", (error, stdout, stderr) => {
  5.   if (error) {
  6.     console.log(`error: ${error.message}`);
  7.     return;
  8.   }
  9.   if (stderr) {
  10.     console.log(`stderr: ${stderr}`)
  11.   }
  12.  
  13.   const lines = stdout.split("\n");
  14.   lines.filter(line => line.endsWith('tsx')).forEach(async (line) => {
  15.     const file = await readFile(line, 'utf-8');
  16.     if (!file.includes('useTranslation')) {
  17.       const fileLines = file.split('\n');
  18.       const lineNumberToInsert = fileLines.findIndex(line => line.includes('=> {')) + 1
  19.       fileLines.splice(lineNumberToInsert, 0, '  const { t } = useTranslation()');
  20.       fileLines.splice(0, 0, 'import { useTranslation } from \'react-i18next\'');
  21.  
  22.       await writeFile(line, fileLines.join('\n'))
  23.     }
  24.   })
  25.  
  26. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement