fix-test-imports.js 1.3 KB

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