8
0

fix-test-imports.js 1.5 KB

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