fix-test-imports.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env node
  2. const fs = require( 'fs' );
  3. const path = require( 'path' );
  4. const glob = require( 'glob' );
  5. const testDir = path.join( process.cwd(), 'tests' );
  6. const testPath = path.join( testDir , '**', '*.js' );
  7. for ( const filePath of glob.sync( testPath ) ) {
  8. const fileContent = fs.readFileSync( filePath, 'utf-8' )
  9. .replace( /\nimport[^']+?'([^']+?)'/gm, fixImport );
  10. fs.writeFileSync( filePath, fileContent , 'utf-8' );
  11. }
  12. function fixImport( wholeImport , path ) {
  13. let fixedImport = fixCkeditorPaths( wholeImport, path );
  14. fixedImport = fixTestPaths( fixedImport, path );
  15. return fixedImport;
  16. }
  17. function fixCkeditorPaths( wholeImport, path ) {
  18. if ( path.indexOf( 'ckeditor5/' ) !== 0 ) {
  19. return wholeImport;
  20. }
  21. const index = wholeImport.indexOf( path );
  22. const pathChunks = path.split( '/' );
  23. return (
  24. wholeImport.slice( 0, index ) +
  25. 'ckeditor5-' + pathChunks[1] + '/src/' + pathChunks.slice( 2 ).join( '/' ) +
  26. wholeImport.slice( path.length + index )
  27. );
  28. }
  29. function fixTestPaths( wholeImport, path ) {
  30. if ( path.indexOf( 'tests/' ) !== 0 ) {
  31. return wholeImport;
  32. }
  33. const index = wholeImport.indexOf( path );
  34. const pathChunks = path.split( '/' );
  35. return (
  36. wholeImport.slice( 0, index ) +
  37. 'ckeditor5-' + pathChunks[1] + '/tests/' + pathChunks.slice( 2 ).join( '/' ) +
  38. wholeImport.slice( path.length + index )
  39. );
  40. }