Browse Source

Changed development tools' copy method to copyTemplateFiles. Added replace functionality for templates.

Szymon Kupś 9 years ago
parent
commit
810d06fe84
2 changed files with 44 additions and 46 deletions
  1. 20 13
      dev/tasks/dev/utils/tools.js
  2. 24 33
      dev/tests/dev/tools.js

+ 20 - 13
dev/tasks/dev/utils/tools.js

@@ -224,29 +224,36 @@ module.exports = {
 	},
 
 	/**
-	 * Copies source files/directories into destination directory.
-	 * If directory path is provided in sources array - all files inside that directory will be copied.
+	 * Copies source files into destination directory and replaces contents of the file using provided `replace` object.
 	 *
-	 * @param { Array } source Source files/directories.
-	 * @param { String} destination Path to destination directory.
+	 *		// Each occurrence of `{{appName}}` inside README.md and CHANGES.md will be changed to `ckeditor5`.
+	 * 		tools.copyTemplateFiles( [ 'README.md', 'CHANGES.md' ], '/new/path', { '{{AppName}}': 'ckeditor5' } );
+	 *
+	 * @param {Array} sources Source files.
+	 * @param {String} destination Path to destination directory.
+	 * @param {Object} [replace] Object with data to fill template. Method will take object's keys and replace their
+	 * occurrences with value stored under that key.
 	 */
-	copy( sources, destination ) {
+	copyTemplateFiles( sources, destination, replace ) {
 		const path = require( 'path' );
 		const fs = require( 'fs-extra' );
+		replace = replace || {};
 		destination = path.resolve( destination );
+		const regexps = [];
+
+		for ( let variableName in replace ) {
+			regexps.push( variableName );
+		}
+		const regexp = new RegExp( regexps.join( '|' ), 'g' );
+		const replaceFunction = ( matched ) => replace[ matched ];
 
 		fs.ensureDirSync( destination );
 
 		sources.forEach( source => {
 			source = path.resolve( source );
-
-			if ( this.isFile( source ) ) {
-				fs.copySync( source, path.join( destination, path.basename( source ) ) );
-			}
-
-			if ( this.isDirectory( source ) ) {
-				fs.copySync( source, destination );
-			}
+			let fileData = fs.readFileSync( source, 'utf8' );
+			fileData = fileData.replace( regexp, replaceFunction );
+			fs.writeFileSync( path.join( destination, path.basename( source ) ), fileData, 'utf8' );
 		} );
 	},
 

+ 24 - 33
dev/tests/dev/tools.js

@@ -366,44 +366,35 @@ describe( 'utils', () => {
 			} );
 		} );
 
-		describe( 'copy', () => {
-			it( 'should be defined', () => expect( tools.copy ).to.be.a( 'function' ) );
-			it( 'should copy files', () => {
-				const fs = require( 'fs-extra' );
+		describe( 'copyTemplateFiles', () => {
+			it( 'should be defined', () => expect( tools.copyTemplateFiles ).to.be.a( 'function' ) );
+			it( 'should copy files and replace provided texts', () => {
 				const path = require( 'path' );
-				let destination = 'destination';
-				let file = 'file.js';
-				sandbox.stub( fs, 'ensureDirSync' );
-				const copyStub = sandbox.stub( fs, 'copySync' );
-				sandbox.stub( tools, 'isFile', () => true );
-				sandbox.stub( tools, 'isDirectory', () => false );
-
-				tools.copy( [ file ], destination );
-
-				file = path.resolve( file );
-				destination = path.join( path.resolve( destination ), path.basename( file ) );
-
-				sinon.assert.calledOnce( copyStub );
-				sinon.assert.calledWithExactly( copyStub.firstCall, file, destination );
-			} );
-
-			it( 'should copy directories', () => {
 				const fs = require( 'fs-extra' );
-				const path = require( 'path' );
-				let destination = 'destination';
-				let dir = 'source';
-				sandbox.stub( fs, 'ensureDirSync' );
-				const copyStub = sandbox.stub( fs, 'copySync' );
-				sandbox.stub( tools, 'isFile', () => false );
-				sandbox.stub( tools, 'isDirectory', () => true );
 
-				tools.copy( [ dir ], destination );
+				const readFileStub = sandbox.stub( fs, 'readFileSync' );
+				readFileStub.onFirstCall().returns( 'file data {{var1}}, {{var2}}' );
+				readFileStub.onSecondCall().returns( '{{var1}}, {{var2}}, {{var2}}{{var1}}' );
 
-				dir = path.resolve( dir );
-				destination = path.resolve( destination );
+				const writeFileStub = sandbox.stub( fs, 'writeFileSync' );
+				const ensureDirStub = sandbox.stub( fs, 'ensureDirSync' );
+				const sources = [ '/path/to/file1.md', '/path/to/file2.md' ];
+				const destination = '/destination/path';
+
+				tools.copyTemplateFiles( sources, destination, {
+					'{{var1}}': 'foo',
+					'{{var2}}': 'bar'
+				} );
 
-				sinon.assert.calledOnce( copyStub );
-				sinon.assert.calledWithExactly( copyStub.firstCall, dir, destination );
+				sinon.assert.calledWithExactly( ensureDirStub, destination );
+				sinon.assert.calledTwice( readFileStub );
+				sinon.assert.calledWithExactly( readFileStub.firstCall, sources[ 0 ], 'utf8' );
+				sinon.assert.calledWithExactly( readFileStub.secondCall, sources[ 1 ], 'utf8' );
+				sinon.assert.calledTwice( writeFileStub );
+				let savePath = path.join( destination, path.basename( sources[ 0 ] ) );
+				sinon.assert.calledWithExactly( writeFileStub.firstCall, savePath, 'file data foo, bar', 'utf8' );
+				savePath = path.join( destination, path.basename( sources[ 1 ] ) );
+				sinon.assert.calledWithExactly( writeFileStub.secondCall, savePath, 'foo, bar, barfoo', 'utf8' );
 			} );
 		} );