Browse Source

Merge pull request #433 from ckeditor/t/432

Internal: Made some dependencies optional. Closes #432.
Piotrek Koszuliński 8 years ago
parent
commit
e3a943eafa

+ 24 - 0
gulpfile.js

@@ -19,6 +19,8 @@ gulp.task( 'pre-commit', [ 'lint-staged' ] );
 // Documentation. -------------------------------------------------------------
 
 gulp.task( 'docs', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-docs' );
+
 	return require( '@ckeditor/ckeditor5-dev-docs' )
 		.build( {
 			readmePath: path.join( process.cwd(), 'README.md' ),
@@ -50,20 +52,28 @@ function getTestOptions() {
 // Translations. --------------------------------------------------------------
 
 gulp.task( 'translations:collect', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-env' );
+
 	return require( '@ckeditor/ckeditor5-dev-env' ).collectTranslations();
 } );
 
 gulp.task( 'translations:upload', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-env' );
+
 	return require( '@ckeditor/ckeditor5-dev-env' ).uploadTranslations();
 } );
 
 gulp.task( 'translations:download', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-env' );
+
 	return require( '@ckeditor/ckeditor5-dev-env' ).downloadTranslations();
 } );
 
 // Releasing. -----------------------------------------------------------------
 
 gulp.task( 'changelog:dependencies', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-env' );
+
 	return require( '@ckeditor/ckeditor5-dev-env' )
 		.generateChangelogForDependencies( {
 			cwd: process.cwd(),
@@ -72,9 +82,23 @@ gulp.task( 'changelog:dependencies', () => {
 } );
 
 gulp.task( 'release:dependencies', () => {
+	assertIsInstalled( '@ckeditor/ckeditor5-dev-env' );
+
 	return require( '@ckeditor/ckeditor5-dev-env' )
 		.releaseDependencies( {
 			cwd: process.cwd(),
 			packages: 'packages'
 		} );
 } );
+
+function assertIsInstalled( packageName ) {
+	try {
+		require( packageName + '/package.json' );
+	} catch ( err ) {
+		console.error( `Error: Cannot find package '${ packageName }'.\n` );
+		console.error( `You need to install optional dependencies.` );
+		console.error( `Run: 'npm run install-optional-dependencies'.` );
+
+		process.exit( 1 );
+	}
+}

+ 3 - 3
package.json

@@ -34,8 +34,6 @@
     "@ckeditor/ckeditor5-widget": "^0.1.0"
   },
   "devDependencies": {
-    "@ckeditor/ckeditor5-dev-docs": "^6.1.6",
-    "@ckeditor/ckeditor5-dev-env": "^4.4.0",
     "@ckeditor/ckeditor5-dev-lint": "^2.0.2",
     "@ckeditor/ckeditor5-dev-tests": "^7.3.0",
     "gulp": "^3.9.0",
@@ -56,6 +54,8 @@
     "url": "https://github.com/ckeditor/ckeditor5.git"
   },
   "scripts": {
-    "test": "node --max_old_space_size=4096 ./node_modules/.bin/ckeditor5-dev-tests --reporter=dots"
+    "test": "node --max_old_space_size=4096 ./node_modules/.bin/ckeditor5-dev-tests --reporter=dots",
+    "install-optional-dependencies": "./scripts/install-optional-dependencies.sh",
+    "switch-to-dev-dev": "./scripts/switch-to-dev-dev.sh"
   }
 }

+ 0 - 46
scripts/fix-src-imports.js

@@ -1,46 +0,0 @@
-#!/usr/bin/env node
-
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-const fs = require( 'fs' );
-const path = require( 'path' );
-const glob = require( 'glob' );
-
-const srcDir = path.join( process.cwd(), 'src' );
-const srcPath = path.join( srcDir, '**', '*.js' );
-
-for ( const filePath of glob.sync( srcPath ) ) {
-	const fileDepth = countOcurrences( filePath.replace( srcDir + '/', '' ), path.sep );
-	const fix = ( wholeImport, pathStart ) => fixImport( wholeImport, pathStart, fileDepth );
-
-	const fileContent = fs.readFileSync( filePath, 'utf-8' )
-		.replace( /\nimport[^']+?'((\.\.\/)+[\w-]+)\/[^']+?'/gm, fix );
-
-	fs.writeFileSync( filePath, fileContent, 'utf-8' );
-}
-
-function fixImport( wholeImport, pathStart, fileDepth ) {
-	const indexOfPathStart = wholeImport.indexOf( '../' );
-	const packageShortName = pathStart.split( '/' ).slice( -1 )[ 0 ];
-	const importDepth = countOcurrences( pathStart, '../' );
-
-	if ( importDepth <= fileDepth ) {
-		return wholeImport;
-	}
-
-	return (
-		wholeImport.slice( 0, indexOfPathStart ) +
-		'ckeditor5-' + packageShortName +
-		'/src' +
-		wholeImport.slice( indexOfPathStart + pathStart.length )
-	);
-}
-
-function countOcurrences( str, pattern ) {
-	return str.split( pattern ).length - 1;
-}

+ 0 - 59
scripts/fix-test-imports.js

@@ -1,59 +0,0 @@
-#!/usr/bin/env node
-
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-const fs = require( 'fs' );
-const path = require( 'path' );
-const glob = require( 'glob' );
-
-const testDir = path.join( process.cwd(), 'tests' );
-const testPath = path.join( testDir , '**', '*.js' );
-
-for ( const filePath of glob.sync( testPath ) ) {
-	const fileContent = fs.readFileSync( filePath, 'utf-8' )
-		.replace( /\nimport[^']+?'([^']+?)'/gm, fixImport );
-
-	fs.writeFileSync( filePath, fileContent , 'utf-8' );
-}
-
-function fixImport( wholeImport , path ) {
-	let fixedImport = fixCkeditorPaths( wholeImport, path );
-	fixedImport = fixTestPaths( fixedImport, path );
-
-	return fixedImport;
-}
-
-function fixCkeditorPaths( wholeImport, path ) {
-	if ( path.indexOf( 'ckeditor5/' ) !== 0 ) {
-		return wholeImport;
-	}
-
-	const index = wholeImport.indexOf( path );
-	const pathChunks = path.split( '/' );
-
-	return (
-		wholeImport.slice( 0, index ) +
-		'ckeditor5-' + pathChunks[ 1 ] + '/src/' + pathChunks.slice( 2 ).join( '/' ) +
-		wholeImport.slice( path.length + index )
-	);
-}
-
-function fixTestPaths( wholeImport, path ) {
-	if ( path.indexOf( 'tests/' ) !== 0 ) {
-		return wholeImport;
-	}
-
-	const index = wholeImport.indexOf( path );
-	const pathChunks = path.split( '/' );
-
-	return (
-		wholeImport.slice( 0, index ) +
-		'ckeditor5-' + pathChunks[ 1 ] + '/tests/' + pathChunks.slice( 2 ).join( '/' ) +
-		wholeImport.slice( path.length + index )
-	);
-}

+ 16 - 0
scripts/install-optional-dependencies.sh

@@ -0,0 +1,16 @@
+#!/bin/sh
+
+# @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+# For licensing, see LICENSE.md.
+
+# Installs optional dev dependencies.
+# They are required by the following tasks:
+#
+# * gulp docs
+# * gulp changelog:dependencies
+# * gulp release:dependencies
+# * gulp translations:*
+
+set -e
+
+npm i @ckeditor/ckeditor5-dev-docs @ckeditor/ckeditor5-dev-env

+ 0 - 21
scripts/remove-js-extensions-from-imports.js

@@ -1,21 +0,0 @@
-#!/usr/bin/env node
-
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-const fs = require( 'fs' );
-const path = require( 'path' );
-const glob = require( 'glob' );
-
-const srcPath = path.join( process.cwd(), '@(src|tests)', '**', '*.js' );
-
-for ( const filePath of glob.sync( srcPath ) ) {
-	const fileContent = fs.readFileSync( filePath, 'utf-8' )
-		.replace( /\nimport([^;]+)\.js';/g, '\nimport$1\';' );
-
-	fs.writeFileSync( filePath, fileContent , 'utf-8' );
-}

+ 3 - 0
scripts/switch-to-dev-dev.sh

@@ -3,6 +3,9 @@
 # @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
 # For licensing, see LICENSE.md.
 
+# Symlinks packages from https://github.com/ckeditor/ckeditor5-dev in ckeditor5's node_modules.
+# This allows easily switching to dev versions of ckeditor5-dev-* packages.
+
 set -e
 
 # If it doesn't exist the following lines won't work.

+ 0 - 33
scripts/switch-to-scoped-imports.js

@@ -1,33 +0,0 @@
-#!/usr/bin/env node
-
-'use strict';
-
-const fs = require( 'fs' );
-const path = require( 'path' );
-const glob = require( 'glob' );
-
-const srcPath = path.join( process.cwd(), '@(src|tests)' );
-
-for ( const filePath of glob.sync( path.join( srcPath, '**', '*.js' ) ) ) {
-	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;
-}