8
0

switch-to-scoped-imports.js 1006 B

123456789101112131415161718192021222324252627282930313233
  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 srcPath = path.join( process.cwd(), '@(src|tests)' );
  7. for ( const filePath of glob.sync( path.join( srcPath, '**', '*.js' ) ) ) {
  8. let fileContent = fs.readFileSync( filePath, 'utf-8' );
  9. fileContent = fileContent.replace( /from '(ckeditor5-[\w\-]+)\//g, 'from \'@ckeditor/$1/' );
  10. fileContent = fixInnerImports( filePath, fileContent );
  11. fs.writeFileSync( filePath, fileContent );
  12. }
  13. // Inner imports should be relative.
  14. function fixInnerImports( filePath, fileContent ) {
  15. const packageName = filePath.match( /ckeditor5-[\w-]+/ )[ 0 ];
  16. const relativePath = filePath.replace( /^.+ckeditor5-[\w-]+\//, '' );
  17. const depth = relativePath.split( '/' ).length - 1;
  18. fileContent = fileContent.replace( new RegExp( ` '@ckeditor/${ packageName }/`, 'g' ), () => {
  19. const relativePath = '../'.repeat( depth );
  20. return ` '${ relativePath }`;
  21. } );
  22. return fileContent;
  23. }