瀏覽代碼

Content styles generator will print new plugins.

Kamil Piechaczek 5 年之前
父節點
當前提交
05a6da26b7
共有 3 個文件被更改,包括 103 次插入11 次删除
  1. 1 0
      package.json
  2. 99 11
      scripts/docs/build-content-styles.js
  3. 3 0
      scripts/docs/content-styles-details.json

+ 1 - 0
package.json

@@ -93,6 +93,7 @@
     "@webspellchecker/wproofreader-ckeditor5": "^1.0.5",
     "@wiris/mathtype-ckeditor5": "^7.24.0",
     "babel-standalone": "^6.26.0",
+    "cli-table": "^0.3.1",
     "coveralls": "^3.1.0",
     "css-loader": "^3.5.3",
     "eslint": "^7.1.0",

+ 99 - 11
scripts/docs/build-content-styles.js

@@ -5,6 +5,8 @@
 
 /* eslint-env node */
 
+const cwd = process.cwd();
+
 const path = require( 'path' );
 const fs = require( 'fs' );
 const chalk = require( 'chalk' );
@@ -12,22 +14,32 @@ const glob = require( 'glob' );
 const mkdirp = require( 'mkdirp' );
 const postcss = require( 'postcss' );
 const webpack = require( 'webpack' );
+const Table = require( 'cli-table' );
 const { tools, styles } = require( '@ckeditor/ckeditor5-dev-utils' );
 const { version } = require( '../../package.json' );
 
 const DESTINATION_DIRECTORY = path.join( __dirname, '..', '..', 'build', 'content-styles' );
+const CONTENT_STYLES_GUIDE_PATH = path.join( __dirname, '..', '..', 'docs', 'builds', 'guides', 'integration', 'content-styles.md' );
+const CONTENT_STYLES_DETAILS_PATH = path.join( __dirname, 'content-styles-details.json' );
+
 const DOCUMENTATION_URL = 'https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/content-styles.html';
+
 const VARIABLE_DEFINITION_REGEXP = /(--[\w-]+):\s+(.*);/g;
 const VARIABLE_USAGE_REGEXP = /var\((--[\w-]+)\)/g;
-const CONTENT_STYLES_GUIDE_PATH = path.join( __dirname, '..', '..', 'docs', 'builds', 'guides', 'integration', 'content-styles.md' );
+
+const contentStylesDetails = require( CONTENT_STYLES_DETAILS_PATH );
+
+// An array of objects with plugins used to generate the current version of the content styles.
+let foundModules;
 
 const contentRules = {
 	selector: [],
 	variables: [],
 	atRules: {}
 };
-const packagesPath = path.join( process.cwd(), 'packages' );
-const shouldUpdateGuide = process.argv.includes( '--commit' );
+
+const packagesPath = path.join( cwd, 'packages' );
+const shouldCommitChanges = process.argv.includes( '--commit' );
 
 logProcess( 'Gathering all CKEditor 5 modules...' );
 
@@ -44,7 +56,7 @@ getCkeditor5ModulePaths()
 				return checkWhetherIsCKEditor5Plugin( modulePath )
 					.then( isModule => {
 						if ( isModule ) {
-							ckeditor5Modules.push( path.join( process.cwd(), modulePath ) );
+							ckeditor5Modules.push( path.join( cwd, modulePath ) );
 						}
 					} );
 			} );
@@ -58,7 +70,9 @@ getCkeditor5ModulePaths()
 
 		return mkdirp( DESTINATION_DIRECTORY ).then( () => generateCKEditor5Source( ckeditor5Modules ) );
 	} )
-	.then( () => {
+	.then( ckeditor5Modules => {
+		foundModules = ckeditor5Modules;
+
 		logProcess( 'Building the editor...' );
 		const webpackConfig = getWebpackConfig();
 
@@ -162,12 +176,40 @@ getCkeditor5ModulePaths()
 	.then( () => {
 		console.log( `Content styles have been extracted to ${ path.join( DESTINATION_DIRECTORY, 'content-styles.css' ) }` );
 
-		if ( !shouldUpdateGuide ) {
+		logProcess( 'Looking for new plugins...' );
+
+		const newPlugins = findNewPlugins( foundModules, contentStylesDetails.plugins );
+
+		if ( newPlugins.length ) {
+			console.log( 'Found new plugins.' );
+			displayNewPluginsTable( newPlugins );
+		} else {
+			console.log( 'Previous and current versions of the content styles stylesheet were generated with the same set of plugins.' );
+		}
+
+		if ( !shouldCommitChanges ) {
 			logProcess( 'Done.' );
 
 			return Promise.resolve();
 		}
 
+		if ( newPlugins.length ) {
+			logProcess( 'Updating the content styles details file...' );
+
+			tools.updateJSONFile( CONTENT_STYLES_DETAILS_PATH, json => {
+				const newPluginsObject = {};
+
+				for ( const data of foundModules ) {
+					const modulePath = normalizePath( data.modulePath.replace( cwd + path.sep, '' ) );
+					newPluginsObject[ modulePath ] = data.pluginName;
+				}
+
+				json.plugins = newPluginsObject;
+
+				return json;
+			} );
+		}
+
 		logProcess( 'Updating the content styles guide...' );
 
 		const promises = [
@@ -184,11 +226,12 @@ getCkeditor5ModulePaths()
 			.then( () => {
 				logProcess( 'Saving and committing...' );
 
-				const contentStyleFile = CONTENT_STYLES_GUIDE_PATH.replace( process.cwd() + path.sep, '' );
+				const contentStyleGuide = CONTENT_STYLES_GUIDE_PATH.replace( cwd + path.sep, '' );
+				const contentStyleDetails = CONTENT_STYLES_DETAILS_PATH.replace( cwd + path.sep, '' );
 
 				// Commit the documentation.
-				if ( exec( `git diff --name-only ${ contentStyleFile }` ).trim().length ) {
-					exec( `git add ${ contentStyleFile }` );
+				if ( exec( `git diff --name-only ${ contentStyleGuide } ${ contentStyleDetails }` ).trim().length ) {
+					exec( `git add ${ contentStyleGuide } ${ contentStyleDetails }` );
 					exec( 'git commit -m "Docs (ckeditor5): Updated the content styles stylesheet."' );
 
 					console.log( 'Successfully updated the content styles guide.' );
@@ -227,7 +270,7 @@ function getCkeditor5ModulePaths() {
  * @returns {Promise.<Boolean>}
  */
 function checkWhetherIsCKEditor5Plugin( modulePath ) {
-	return readFile( path.join( process.cwd(), modulePath ) )
+	return readFile( path.join( cwd, modulePath ) )
 		.then( content => {
 			const pluginName = path.basename( modulePath, '.js' );
 
@@ -278,7 +321,8 @@ function generateCKEditor5Source( ckeditor5Modules ) {
 
 	sourceFileContent.push( '];' );
 
-	return writeFile( path.join( DESTINATION_DIRECTORY, 'source.js' ), sourceFileContent.join( '\n' ) );
+	return writeFile( path.join( DESTINATION_DIRECTORY, 'source.js' ), sourceFileContent.join( '\n' ) )
+		.then( () => ckeditor5Modules );
 
 	function capitalize( value ) {
 		return value.charAt( 0 ).toUpperCase() + value.slice( 1 );
@@ -536,6 +580,50 @@ function transformCssRules( rules ) {
 		.join( '\n' );
 }
 
+/**
+ * Returns an object that contains objects with new plugins.
+ *
+ * @param {Array.<Object>} currentPlugins
+ * @param {Array.<Object>} previousPlugins
+ * @returns {{Array.<Object>}}
+ */
+function findNewPlugins( currentPlugins, previousPlugins ) {
+	const newPlugins = [];
+
+	for ( const data of currentPlugins ) {
+		// Use relative paths.
+		const modulePath = normalizePath( data.modulePath.replace( cwd + path.sep, '' ) );
+
+		if ( !previousPlugins[ modulePath ] ) {
+			newPlugins.push( data );
+		}
+	}
+
+	return newPlugins;
+}
+
+/**
+ * Displays a table with new plugins.
+ *
+ * @param {Array.<Object>} newPlugins
+ */
+function displayNewPluginsTable( newPlugins ) {
+	const table = new Table( {
+		head: [ 'Plugin name', 'Module path' ],
+		style: { compact: true }
+	} );
+
+	for ( const data of newPlugins ) {
+		table.push( [ data.pluginName, data.modulePath ] );
+	}
+
+	console.log( table.toString() + '\n' );
+}
+
+function normalizePath( modulePath ) {
+	return modulePath.split( path.sep ).join( path.posix.sep );
+}
+
 function exec( command ) {
 	return tools.shExec( command, { verbosity: 'error' } );
 }

+ 3 - 0
scripts/docs/content-styles-details.json

@@ -0,0 +1,3 @@
+{
+  "plugins": {}
+}