瀏覽代碼

Updated the script so internal imports are converted to relative paths.

Piotrek Koszuliński 9 年之前
父節點
當前提交
3f9baf8daa
共有 2 個文件被更改,包括 21 次插入4 次删除
  1. 2 2
      scripts/fix-test-imports.js
  2. 19 2
      scripts/switch-to-scoped-imports.js

+ 2 - 2
scripts/fix-test-imports.js

@@ -38,7 +38,7 @@ function fixCkeditorPaths( wholeImport, path ) {
 
 	return (
 		wholeImport.slice( 0, index ) +
-		'ckeditor5-' + pathChunks[1] + '/src/' + pathChunks.slice( 2 ).join( '/' ) +
+		'ckeditor5-' + pathChunks[ 1 ] + '/src/' + pathChunks.slice( 2 ).join( '/' ) +
 		wholeImport.slice( path.length + index )
 	);
 }
@@ -53,7 +53,7 @@ function fixTestPaths( wholeImport, path ) {
 
 	return (
 		wholeImport.slice( 0, index ) +
-		'ckeditor5-' + pathChunks[1] + '/tests/' + pathChunks.slice( 2 ).join( '/' ) +
+		'ckeditor5-' + pathChunks[ 1 ] + '/tests/' + pathChunks.slice( 2 ).join( '/' ) +
 		wholeImport.slice( path.length + index )
 	);
 }

+ 19 - 2
scripts/switch-to-scoped-imports.js

@@ -9,8 +9,25 @@ const glob = require( 'glob' );
 const srcPath = path.join( process.cwd(), '@(src|tests)' );
 
 for ( const filePath of glob.sync( path.join( srcPath, '**', '*.js' ) ) ) {
-	const fileContent = fs.readFileSync( filePath, 'utf-8' )
-		.replace( /from '(ckeditor5-[\w\-]+)\//g, 'from \'@ckeditor/$1/' );
+	let fileContent = fs.readFileSync( filePath, 'utf-8' );
+
+	fileContent = fileContent.replace( /from '(ckeditor5-[\w\-]+)\//g, 'from \'@ckeditor/$1/' );
+	fileContent = fixInnerImports( filePath, fileContent );
 
 	fs.writeFileSync( filePath, fileContent );
 }
+
+// Inner imports should be relative.
+function fixInnerImports( filePath, fileContent ) {
+	const packageName = filePath.match( /ckeditor5-[\w-]+/ )[ 0 ];
+	const relativePath = filePath.replace( /^.+ckeditor5-[\w-]+\//, '' );
+	const depth = relativePath.split( '/' ).length - 1;
+
+	fileContent = fileContent.replace( new RegExp( ` '@ckeditor/${ packageName }/`, 'g' ), () => {
+		const relativePath = '../'.repeat( depth );
+
+		return ` '${ relativePath }`;
+	} );
+
+	return fileContent;
+}