stub-rename.js 982 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const fs = require('fs');
  2. const child = require('child_process');
  3. const results = child
  4. .execSync("find . -type f -name '*.spec.jsx' -exec grep -lH 'TestStubs' {} \\;")
  5. .toString();
  6. const files = results.split('\n');
  7. function lowercasefirst(string) {
  8. return string.charAt(0).toLowerCase() + string.slice(1);
  9. }
  10. for (const file of files) {
  11. if (!file) {
  12. continue;
  13. }
  14. let content = fs.readFileSync(file, 'utf8');
  15. const imports = new Set();
  16. let matches;
  17. while ((matches = /TestStubs\.([^\(]+)/g.exec(content))) {
  18. imports.add(matches[1]);
  19. content = content.replaceAll(new RegExp(`TestStubs.(${matches[1]})`, 'g'), '$1');
  20. matches = /TestStubs\.([^\(]+)/g.exec(content);
  21. }
  22. const contentByLine = content.split('\n');
  23. for (const importName of imports) {
  24. contentByLine.unshift(
  25. `import {${importName}} from 'fixtures/js-stubs/${lowercasefirst(importName)}';`
  26. );
  27. fs.writeFileSync(file, contentByLine.join('\n'), 'utf8');
  28. }
  29. }