Skip to content

Commit

Permalink
Review requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslaw-zan committed Nov 9, 2022
1 parent 31d2ede commit 13e1392
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
12 changes: 7 additions & 5 deletions packages/ckeditor5-package-generator/lib/utils/copy-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,24 @@ module.exports = function copyFiles( logger, options ) {
/**
* Copies all files into the package directory. If any file has any template placeholders, they are filled.
*
* @param {String} templateFile The relative path to the "templates/" directory of the file to copy.
* @param {String} templatePath The relative path to the "templates/" directory of the file to copy.
* @param {String} packagePath The destination directory where the new package is created.
* @param {Object} data The data to fill in the template file.
*/
function copyTemplate( templateFile, packagePath, data ) {
const rawFile = fs.readFileSync( path.join( TEMPLATE_PATH, templateFile ), 'utf-8' );
function copyTemplate( templatePath, packagePath, data ) {
const rawFile = fs.readFileSync( path.join( TEMPLATE_PATH, templatePath ), 'utf-8' );
const filledFile = template( rawFile )( data );

const destinationPath = path.join( packagePath, templateFile )
const processedTemplatePath = templatePath
// Remove sub-directory inside templates to merge results into one directory.
.replace( /(?:common|js|ts)(?:\\|\/)/, '' )
.replace( /^(?:common|js|ts)(?:\\|\/)/, '' )
// We use the ".txt" file extension to circumvent syntax errors in templates and npm not publishing the ".gitignore" file.
.replace( /\.txt$/, '' )
// Replace placeholder filenames with the class name.
.replace( /_PLACEHOLDER_/, data.formattedNames.plugin.lowerCaseMerged );

const destinationPath = path.join( packagePath, processedTemplatePath );

// Make sure that the destination directory exists.
mkdirp.sync( path.dirname( destinationPath ) );
fs.writeFileSync( destinationPath, filledFile );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function toPascalCase( parts ) {
*/
function toCamelCase( parts ) {
const pascalCase = toPascalCase( parts );

return pascalCase.charAt( 0 ).toLowerCase() + pascalCase.slice( 1 );
}

Expand Down
42 changes: 21 additions & 21 deletions packages/ckeditor5-package-generator/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,25 @@ describe( 'lib/index', () => {
expect( index ).to.be.a( 'function' );
} );

it( 'creates logger with verbose option set to false', async () => {
it( 'passes the verbose option when creating the logger ("true" check)', async () => {
await index( packageName, options );

expect( stubs.validatePackageName.callCount ).to.equal( 1 );
expect( stubs.validatePackageName.getCall( 0 ).args[ 0 ].constructor.name ).to.equal( 'Logger' );
expect( stubs.validatePackageName.getCall( 0 ).args[ 0 ] ).to.deep.equal( { verbose: true, info: stubs.logger.info } );
const logger = stubs.validatePackageName.getCall( 0 ).args[ 0 ];
expect( logger.constructor.name ).to.equal( 'Logger' );
expect( logger ).to.deep.equal( { verbose: true, info: stubs.logger.info } );
} );

it( 'creates logger with verbose option set to true', async () => {
options.verbose = true;
it( 'passes the verbose option when creating the logger ("false" check)', async () => {
options.verbose = false;

await index( packageName, options );

expect( stubs.validatePackageName.callCount ).to.equal( 1 );
expect( stubs.validatePackageName.getCall( 0 ).args[ 0 ].constructor.name ).to.equal( 'Logger' );
expect( stubs.validatePackageName.getCall( 0 ).args[ 0 ] ).to.deep.equal( { verbose: true, info: stubs.logger.info } );
const logger = stubs.validatePackageName.getCall( 0 ).args[ 0 ];
expect( logger.constructor.name ).to.equal( 'Logger' );
expect( logger ).to.deep.equal( { verbose: false, info: stubs.logger.info } );
} );

it( 'passes correct arguments to the validatePackageName()', async () => {
it( 'validates the package name', async () => {
await index( packageName, options );

expect( stubs.validatePackageName.callCount ).to.equal( 1 );
Expand All @@ -143,7 +143,7 @@ describe( 'lib/index', () => {
expect( stubs.validatePackageName.getCall( 0 ).args[ 1 ] ).to.equal( '@scope/ckeditor5-feature' );
} );

it( 'passes correct arguments to the validatePluginName()', async () => {
it( 'validates the plugin name', async () => {
await index( packageName, options );

expect( stubs.validatePluginName.callCount ).to.equal( 1 );
Expand All @@ -153,7 +153,7 @@ describe( 'lib/index', () => {
expect( stubs.validatePluginName.getCall( 0 ).args[ 1 ] ).to.equal( 'FooBar' );
} );

it( 'passes correct arguments to the getPackageNameFormats()', async () => {
it( 'gets the package name formats', async () => {
await index( packageName, options );

expect( stubs.getPackageNameFormats.callCount ).to.equal( 1 );
Expand All @@ -163,7 +163,7 @@ describe( 'lib/index', () => {
expect( stubs.getPackageNameFormats.getCall( 0 ).args[ 1 ] ).to.equal( 'FooBar' );
} );

it( 'passes correct arguments to the createDirectory()', async () => {
it( 'creates the directory', async () => {
await index( packageName, options );

expect( stubs.createDirectory.callCount ).to.equal( 1 );
Expand All @@ -173,7 +173,7 @@ describe( 'lib/index', () => {
expect( stubs.createDirectory.getCall( 0 ).args[ 1 ] ).to.equal( '@scope/ckeditor5-feature' );
} );

it( 'passes correct arguments to the choosePackageManager()', async () => {
it( 'chooses the package manager', async () => {
await index( packageName, options );

expect( stubs.choosePackageManager.callCount ).to.equal( 1 );
Expand All @@ -183,7 +183,7 @@ describe( 'lib/index', () => {
expect( stubs.choosePackageManager.getCall( 0 ).args[ 1 ] ).to.equal( true );
} );

it( 'passes correct arguments to the chooseProgrammingLanguage()', async () => {
it( 'chooses the programming language', async () => {
await index( packageName, options );

expect( stubs.chooseProgrammingLanguage.callCount ).to.equal( 1 );
Expand All @@ -193,7 +193,7 @@ describe( 'lib/index', () => {
expect( stubs.chooseProgrammingLanguage.getCall( 0 ).args[ 1 ] ).to.equal( 'js' );
} );

it( 'passes correct arguments to the getDependenciesVersions()', async () => {
it( 'gets the versions of the dependencies', async () => {
await index( packageName, options );

expect( stubs.getDependenciesVersions.callCount ).to.equal( 1 );
Expand All @@ -203,7 +203,7 @@ describe( 'lib/index', () => {
expect( stubs.getDependenciesVersions.getCall( 0 ).args[ 1 ] ).to.equal( false );
} );

it( 'passes correct arguments to the copyFiles()', async () => {
it( 'copies the files', async () => {
await index( packageName, options );

expect( stubs.copyFiles.callCount ).to.equal( 1 );
Expand Down Expand Up @@ -237,7 +237,7 @@ describe( 'lib/index', () => {
} );
} );

it( 'passes correct arguments to the installDependencies()', async () => {
it( 'installs the dependencies', async () => {
await index( packageName, options );

expect( stubs.installDependencies.callCount ).to.equal( 1 );
Expand All @@ -249,7 +249,7 @@ describe( 'lib/index', () => {
expect( stubs.installDependencies.getCall( 0 ).args[ 3 ] ).to.equal( false );
} );

it( 'passes correct arguments to the initializeGitRepository()', async () => {
it( 'initializes the git repository', async () => {
await index( packageName, options );

expect( stubs.initializeGitRepository.callCount ).to.equal( 1 );
Expand All @@ -259,7 +259,7 @@ describe( 'lib/index', () => {
expect( stubs.initializeGitRepository.getCall( 0 ).args[ 1 ].constructor.name ).to.equal( 'Logger' );
} );

it( 'passes correct arguments to the installGitHooks()', async () => {
it( 'installs the git hooks', async () => {
await index( packageName, options );

expect( stubs.installGitHooks.callCount ).to.equal( 1 );
Expand All @@ -270,7 +270,7 @@ describe( 'lib/index', () => {
expect( stubs.installGitHooks.getCall( 0 ).args[ 2 ] ).to.equal( true );
} );

it( 'logs info when the script finishes', async () => {
it( 'logs info before the script finishes', async () => {
await index( packageName, options );

expect( stubs.logger.info.callCount ).to.equal( 1 );
Expand Down
48 changes: 48 additions & 0 deletions packages/ckeditor5-package-generator/tests/utils/copy-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,54 @@ describe( 'lib/utils/copy-files', () => {
].join( '\n' ) );
} );

it( 'works correctly with path containing directory called "common"', () => {
options.directoryPath = 'directory/common/foo';

copyFiles( stubs.logger, options );

expect( stubs.fs.writeFileSync.callCount ).to.equal( 4 );
expect( stubs.fs.writeFileSync.getCall( 0 ).args[ 0 ] ).to.equal( 'directory/common/foo/LICENSE.md' );
expect( stubs.fs.writeFileSync.getCall( 1 ).args[ 0 ] ).to.equal( 'directory/common/foo/lang/contexts.json' );
expect( stubs.fs.writeFileSync.getCall( 2 ).args[ 0 ] ).to.equal( 'directory/common/foo/package.json' );
expect( stubs.fs.writeFileSync.getCall( 3 ).args[ 0 ] ).to.equal( 'directory/common/foo/src/index.js' );
} );

it( 'works correctly with path containing directory called "js"', () => {
options.directoryPath = 'directory/js/foo';

copyFiles( stubs.logger, options );

expect( stubs.fs.writeFileSync.callCount ).to.equal( 4 );
expect( stubs.fs.writeFileSync.getCall( 0 ).args[ 0 ] ).to.equal( 'directory/js/foo/LICENSE.md' );
expect( stubs.fs.writeFileSync.getCall( 1 ).args[ 0 ] ).to.equal( 'directory/js/foo/lang/contexts.json' );
expect( stubs.fs.writeFileSync.getCall( 2 ).args[ 0 ] ).to.equal( 'directory/js/foo/package.json' );
expect( stubs.fs.writeFileSync.getCall( 3 ).args[ 0 ] ).to.equal( 'directory/js/foo/src/index.js' );
} );

it( 'works correctly with path containing directory called "ts"', () => {
options.directoryPath = 'directory/ts/foo';

copyFiles( stubs.logger, options );

expect( stubs.fs.writeFileSync.callCount ).to.equal( 4 );
expect( stubs.fs.writeFileSync.getCall( 0 ).args[ 0 ] ).to.equal( 'directory/ts/foo/LICENSE.md' );
expect( stubs.fs.writeFileSync.getCall( 1 ).args[ 0 ] ).to.equal( 'directory/ts/foo/lang/contexts.json' );
expect( stubs.fs.writeFileSync.getCall( 2 ).args[ 0 ] ).to.equal( 'directory/ts/foo/package.json' );
expect( stubs.fs.writeFileSync.getCall( 3 ).args[ 0 ] ).to.equal( 'directory/ts/foo/src/index.js' );
} );

it( 'works correctly with path containing directory called "Projects" (it ends with "ts")', () => {
options.directoryPath = 'directory/Projects/foo';

copyFiles( stubs.logger, options );

expect( stubs.fs.writeFileSync.callCount ).to.equal( 4 );
expect( stubs.fs.writeFileSync.getCall( 0 ).args[ 0 ] ).to.equal( 'directory/Projects/foo/LICENSE.md' );
expect( stubs.fs.writeFileSync.getCall( 1 ).args[ 0 ] ).to.equal( 'directory/Projects/foo/lang/contexts.json' );
expect( stubs.fs.writeFileSync.getCall( 2 ).args[ 0 ] ).to.equal( 'directory/Projects/foo/package.json' );
expect( stubs.fs.writeFileSync.getCall( 3 ).args[ 0 ] ).to.equal( 'directory/Projects/foo/src/index.js' );
} );

it( 'works with npm instead of yarn', () => {
options.packageManager = 'npm';

Expand Down

0 comments on commit 13e1392

Please sign in to comment.