diff --git a/README.md b/README.md index 7962d6ed..01db8d92 100644 --- a/README.md +++ b/README.md @@ -69,13 +69,14 @@ The tool will create a new directory called `@organization/ckeditor5-package` wi To use a local version of the `@ckeditor/ckeditor5-package-tools` package, use the `--dev` option when executing the command. ```bash -node /path/to/repository/packages/create-ckeditor5-plugin [--dev] +node /path/to/repository/packages/create-ckeditor5-plugin [--dev] [--verbose] [--use-npm] ``` #### Options -* `--verbose` - (alias: `-v`) whether to prints additional logs. +* `--verbose` - (alias: `-v`) whether to prints additional logs about the current executed task. * `--dev` - whether to execute in the development mode. It means that the `@ckeditor/ckeditor5-package-tools` will not be installed from npm, but from the local file system. +* `--use-npm` - whether to use `npm` instead of `yarn` when installing dependencies in a newly created package. #### Developing the package diff --git a/packages/create-ckeditor5-plugin/README.md b/packages/create-ckeditor5-plugin/README.md index 22758971..f0b506b0 100644 --- a/packages/create-ckeditor5-plugin/README.md +++ b/packages/create-ckeditor5-plugin/README.md @@ -7,13 +7,14 @@ Create CKEditor 5 Plugin * [Creating a package](#creating-a-package) * [Developing the package](#developing-the-package) + * [Modifiers](#modifiers) ## Creating a package To create a new plugin, call the following command: ```bash -npx create-ckeditor5-plugin +npx create-ckeditor5-plugin [--verbose] [--use-npm] ``` The `` argument is obligatory and must follow these rules: @@ -23,6 +24,11 @@ The `` argument is obligatory and must follow these rules: As a result of executing the command, a new directory with a package will be created. The directory's name will be equal to the specified package name without the `@organization` part, and it will contain an example plugin and development environment. +### Modifiers + +* `--verbose` - (alias: `-v`) whether to prints additional logs about the current executed task. +* `--use-npm` - whether to use `npm` instead of `yarn` when installing dependencies in a newly created package. + ## Developing the package Once your package is generated, you can change your working directory to that of the package, and use the available scripts: diff --git a/packages/create-ckeditor5-plugin/lib/index.js b/packages/create-ckeditor5-plugin/lib/index.js index 7934720c..7975ab82 100755 --- a/packages/create-ckeditor5-plugin/lib/index.js +++ b/packages/create-ckeditor5-plugin/lib/index.js @@ -44,8 +44,6 @@ new Command( packageJson.name ) .option( '--use-npm', 'whether use npm to install packages', false ) .allowUnknownOption() .action( ( packageName, options ) => init( packageName, options ) ) - // .on( '--help', () => { - // } ) .parse( process.argv ); /** @@ -68,6 +66,7 @@ async function init( packageName, options ) { // // TODO: Implement the `--info` flag for reporting issues. // Use: https://www.npmjs.com/package/envinfo. + const log = getLogger( options.verbose ); // (1.) console.log( '📍 Verifying the specified package name.' ); @@ -86,7 +85,7 @@ async function init( packageName, options ) { const directoryName = packageName.split( '/' )[ 1 ]; const directoryPath = path.resolve( directoryName ); - console.log( '📍 Checking whether the specified directory can be created.' ); + console.log( `📍 Checking whether the "${ chalk.cyan( directoryName ) }" directory can be created.` ); if ( fs.existsSync( directoryPath ) ) { console.log( chalk.red( 'Error:' ), 'Cannot create a directory as the location is already taken.' ); @@ -99,7 +98,9 @@ async function init( packageName, options ) { console.log( `📍 Creating the directory "${ chalk.cyan( directoryPath ) }".` ); mkdirp.sync( directoryPath ); - const packageVersions = getDependenciesVersions( options.dev ); + const packageVersions = getDependenciesVersions( { + devMode: options.dev + } ); const dllConfiguration = getDllConfiguration( packageName ); @@ -113,7 +114,7 @@ async function init( packageName, options ) { console.log( '📍 Copying files...' ); for ( const templatePath of templatesToCopy ) { - console.log( `* Copying "${ chalk.gray( templatePath ) }"...` ); + log( `* Copying "${ chalk.gray( templatePath ) }"...` ); let data; if ( TEMPLATES_TO_FILL.includes( templatePath ) ) { @@ -134,7 +135,10 @@ async function init( packageName, options ) { // (4.) console.log( '📍 Install dependencies...' ); - installPackages( directoryPath ); + installPackages( directoryPath, { + useNpm: options.useNpm, + verbose: options.verbose + } ); // (5.) console.log( '📍 Initializing Git repository...' ); @@ -169,24 +173,38 @@ function copyTemplate( templateFile, packagePath, data ) { /** * @param {String} directoryPath + * @param {Object} options + * @param {Boolean} options.useNpm + * @param {Boolean} options.verbose */ -function installPackages( directoryPath /* Support for NPM */ ) { - const yarnArguments = [ - '--cwd', - directoryPath - ]; - - // if ( verbose ) { - // yarnArguments.push( '--verbose' ); - // } - - spawnSync( 'yarnpkg', yarnArguments, { +function installPackages( directoryPath, options ) { + const spawnOptions = { encoding: 'utf8', shell: true, cwd: directoryPath, - stdio: 'inherit', stderr: 'inherit' - } ); + }; + + if ( options.verbose ) { + spawnOptions.stdio = 'inherit'; + } + + if ( options.useNpm ) { + const npmArguments = [ + 'install', + '--prefix', + directoryPath + ]; + + spawnSync( 'npm', npmArguments, spawnOptions ); + } else { + const yarnArguments = [ + '--cwd', + directoryPath + ]; + + spawnSync( 'yarnpkg', yarnArguments, spawnOptions ); + } } /** @@ -250,6 +268,18 @@ function getIndexFileName( packageName ) { return packageName.replace( /^ckeditor5-/, '' ) + '.js'; } +/** + * @param {Boolean} verbose + * @return {Function} + */ +function getLogger( verbose ) { + return message => { + if ( verbose ) { + console.log( message ); + } + }; +} + /** * @typedef {Object} CreateCKeditor5PluginOptions * diff --git a/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js b/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js index 412b6a8d..bb314df3 100644 --- a/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js +++ b/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js @@ -9,23 +9,24 @@ const path = require( 'path' ); const getPackageVersion = require( './get-package-version' ); /** - * Returns an object containing string values: + * Returns an object containing version for packages listed below: * - * { - * ckeditor5: (version), - * devUtils: (version), - * packageTools: (version|path) - * } + * * ckeditor5 + * * @ckeditor/ckeditor5-dev-utils (as `devUtils`) + * * eslint-config-ckeditor5 (as `eslintConfigCkeditor5`) + * * stylelint-config-ckeditor5 (as `eslintConfigCkeditor5`) + * * @ckeditor/ckeditor5-package-tools (as `stylelintConfigCkeditor5`) * - * Last value is dependent on the devMode parameter: + * The value for the `packageTools` package depends on the `options.devMode` modifier: * - * True: Path to where locally cloned package should be. - * False: Latest NPM version. + * * `true` - an absolute path to locally cloned package. + * * `false` - the latest version published on npm. * - * @param {Boolean} devMode whether or not to use locally cloned packageTools. + * @param {Boolean} devMode Whether current process is executed in the developer mode. + * @param {Boolean} useNpm Whether to use `npm` when installing packages. * @returns {Object} */ -module.exports = function getDependenciesVersions( devMode ) { +module.exports = function getDependenciesVersions( { devMode } ) { return { ckeditor5: getPackageVersion( 'ckeditor5' ), devUtils: getPackageVersion( '@ckeditor/ckeditor5-dev-utils' ), diff --git a/packages/create-ckeditor5-plugin/package.json b/packages/create-ckeditor5-plugin/package.json index cd4f8ce4..3de9db0b 100644 --- a/packages/create-ckeditor5-plugin/package.json +++ b/packages/create-ckeditor5-plugin/package.json @@ -9,7 +9,7 @@ "engines": { "node": ">= 12" }, - "devDependencies": { + "dependencies": { "chalk": "^4.1.2", "commander": "^8.1.0", "glob": "^7.1.7", diff --git a/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js b/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js index 96f7a8ae..8f9791a8 100644 --- a/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js +++ b/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js @@ -42,32 +42,32 @@ describe( 'lib/utils/get-dependencies-versions', () => { } ); it( 'returns an object with a version of the "ckeditor5" package', () => { - const returnedValue = getDependenciesVersions( false ); + const returnedValue = getDependenciesVersions( { devMode: false } ); expect( returnedValue.ckeditor5 ).to.equal( '30.0.0' ); } ); it( 'returns an object with a version of the "@ckeditor/ckeditor5-dev-utils" package', () => { - const returnedValue = getDependenciesVersions( false ); + const returnedValue = getDependenciesVersions( { devMode: false } ); expect( returnedValue.devUtils ).to.equal( '25.0.0' ); } ); it( 'returns an object with a version of the "eslint-config-ckeditor5', () => { - const returnedValue = getDependenciesVersions( false ); + const returnedValue = getDependenciesVersions( { devMode: false } ); expect( returnedValue.eslintConfigCkeditor5 ).to.equal( '5.0.0' ); } ); it( 'returns an object with a version of the "stylelint-config-ckeditor5" package', () => { - const returnedValue = getDependenciesVersions( false ); + const returnedValue = getDependenciesVersions( { devMode: false } ); expect( returnedValue.stylelintConfigCkeditor5 ).to.equal( '3.0.0' ); } ); it( 'returns an object with a version of the "@ckeditor/ckeditor5-package-tools" package if "devMode" is disabled', () => { - const returnedValue = getDependenciesVersions( false ); + const returnedValue = getDependenciesVersions( { devMode: false } ); expect( returnedValue.packageTools ).to.equal( '^1.0.0' ); } ); it( 'it returns an absolute path to the "@ckeditor/ckeditor5-package-tools" package if "devMode" is enabled', () => { - const returnedValue = getDependenciesVersions( true ); + const returnedValue = getDependenciesVersions( { devMode: true, useNpm: true } ); const PROJECT_ROOT_DIRECTORY = path.join( __dirname, '..', '..', '..' ); let packageTools = 'file:' + path.resolve( PROJECT_ROOT_DIRECTORY, 'ckeditor5-package-tools' );