From 0a6fd5f2a02d48028acee56c2a9fc5337a5399e6 Mon Sep 17 00:00:00 2001 From: Przemyslaw Zan Date: Tue, 28 Sep 2021 16:30:56 +0200 Subject: [PATCH 1/5] Implemented option to install dependencies using npm. --- packages/create-ckeditor5-plugin/lib/index.js | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/create-ckeditor5-plugin/lib/index.js b/packages/create-ckeditor5-plugin/lib/index.js index 17bd4dcd..c18e45b0 100755 --- a/packages/create-ckeditor5-plugin/lib/index.js +++ b/packages/create-ckeditor5-plugin/lib/index.js @@ -134,7 +134,7 @@ async function init( packageName, options ) { // (4.) console.log( '📍 Install dependencies...' ); - installPackages( directoryPath ); + installPackages( directoryPath, options.useNpm ); // (5.) console.log( '📍 Initializing Git repository...' ); @@ -170,23 +170,31 @@ function copyTemplate( templateFile, packagePath, data ) { /** * @param {String} directoryPath */ -function installPackages( directoryPath /* Support for NPM */ ) { - const yarnArguments = [ - '--cwd', - directoryPath - ]; - - // if ( verbose ) { - // yarnArguments.push( '--verbose' ); - // } - - spawnSync( 'yarnpkg', yarnArguments, { +function installPackages( directoryPath, useNpm ) { + const spawnOptions = { encoding: 'utf8', shell: true, cwd: directoryPath, stdio: 'inherit', stderr: 'inherit' - } ); + }; + + if ( useNpm ) { + const npmArguments = [ + 'install', + '--prefix', + directoryPath + ]; + + spawnSync( 'npm', npmArguments, spawnOptions ); + } else { + const yarnArguments = [ + '--cwd', + directoryPath + ]; + + spawnSync( 'yarnpkg', yarnArguments, spawnOptions ); + } } /** From 19aecd70647e424f68ccfaaeda0a6639c7735454 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Wed, 29 Sep 2021 11:43:11 +0200 Subject: [PATCH 2/5] Unified a way on how package managers install packages. --- packages/create-ckeditor5-plugin/lib/index.js | 41 +++++++++++++++---- .../lib/utils/get-dependencies-versions.js | 35 ++++++++++------ .../tests/utils/get-dependencies-versions.js | 24 +++++++---- 3 files changed, 72 insertions(+), 28 deletions(-) diff --git a/packages/create-ckeditor5-plugin/lib/index.js b/packages/create-ckeditor5-plugin/lib/index.js index c18e45b0..30f85821 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,10 @@ 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, + useNpm: options.useNpm + } ); const dllConfiguration = getDllConfiguration( packageName ); @@ -113,7 +115,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 +136,10 @@ async function init( packageName, options ) { // (4.) console.log( '📍 Install dependencies...' ); - installPackages( directoryPath, options.useNpm ); + installPackages( directoryPath, { + useNpm: options.useNpm, + verbose: options.verbose + } ); // (5.) console.log( '📍 Initializing Git repository...' ); @@ -169,17 +174,23 @@ function copyTemplate( templateFile, packagePath, data ) { /** * @param {String} directoryPath + * @param {Object} options + * @param {Boolean} options.useNpm + * @param {Boolean} options.verbose */ -function installPackages( directoryPath, useNpm ) { +function installPackages( directoryPath, options ) { const spawnOptions = { encoding: 'utf8', shell: true, cwd: directoryPath, - stdio: 'inherit', stderr: 'inherit' }; - if ( useNpm ) { + if ( options.verbose ) { + spawnOptions.stdio = 'inherit'; + } + + if ( options.useNpm ) { const npmArguments = [ 'install', '--prefix', @@ -258,6 +269,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 a7c3a444..a2403036 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,34 @@ 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. + * Also, the `options.useNpm` modifier changes the protocol of the returned value: + * + * * `true` - use the `file:` protocol. + * * `false` - use the `link:` protocol. + * + * @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, useNpm } ) { + // `npm` does not understand the `link:` protocol. When installing packages from a file system, + // it creates symlinks instead of copying the package files, while `yarn` does the opposite. + // However, `yarn` supports the `link:` protocol, so let's use it. + const protocol = useNpm ? 'file:' : 'link:'; + return { ckeditor5: getPackageVersion( 'ckeditor5' ), devUtils: getPackageVersion( '@ckeditor/ckeditor5-dev-utils' ), @@ -33,7 +44,7 @@ module.exports = function getDependenciesVersions( devMode ) { stylelintConfigCkeditor5: getPackageVersion( 'stylelint-config-ckeditor5' ), packageTools: devMode ? // Windows accepts unix-like paths in `package.json`, so let's unify it to avoid errors with paths. - 'file:' + path.resolve( __dirname, '..', '..', '..', 'ckeditor5-package-tools' ).split( path.sep ).join( path.posix.sep ) : + protocol + path.resolve( __dirname, '..', '..', '..', 'ckeditor5-package-tools' ).split( path.sep ).join( path.posix.sep ) : '^' + getPackageVersion( '@ckeditor/ckeditor5-package-tools' ) }; }; 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 11c8de26..f6aee0c1 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,42 @@ 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 ); + it( 'it returns an absolute path to the "@ckeditor/ckeditor5-package-tools" package if "devMode" is enabled (yarn)', () => { + const returnedValue = getDependenciesVersions( { devMode: true } ); + + const PROJECT_ROOT_DIRECTORY = path.join( __dirname, '..', '..', '..' ); + let packageTools = 'link:' + path.resolve( PROJECT_ROOT_DIRECTORY, 'ckeditor5-package-tools' ); + packageTools = packageTools.split( path.sep ).join( path.posix.sep ); + + expect( returnedValue.packageTools ).to.equal( packageTools ); + } ); + + it( 'it returns an absolute path to the "@ckeditor/ckeditor5-package-tools" package if "devMode" is enabled (npm)', () => { + 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' ); From 11b2a7f84fc22a145c4335c88189edfb5d33d861 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Wed, 29 Sep 2021 11:59:18 +0200 Subject: [PATCH 3/5] Added CLI modifiers in READMEs. --- README.md | 7 +++++-- packages/create-ckeditor5-plugin/README.md | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7962d6ed..6672c18c 100644 --- a/README.md +++ b/README.md @@ -69,13 +69,16 @@ 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. + +⚠️ Installing packages using npm from local files (the combination of `--dev` and `--use-npm` options) ends with missing packages in the newly created packages, as npm does not download dependencies of linked packages. Hence, we suggest using `yarn` when contributing to this project. #### 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: From 36d6d7bef7ca6e9eb8c395ff677b9eb7d32c19b5 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Wed, 29 Sep 2021 15:25:40 +0200 Subject: [PATCH 4/5] The "getDependenciesVersions()" util always return a path with the "file:" protocol. --- README.md | 2 -- packages/create-ckeditor5-plugin/lib/index.js | 3 +-- .../lib/utils/get-dependencies-versions.js | 14 ++------------ .../tests/utils/get-dependencies-versions.js | 12 +----------- 4 files changed, 4 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 6672c18c..01db8d92 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,6 @@ node /path/to/repository/packages/create-ckeditor5-plugin [--dev] * `--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. -⚠️ Installing packages using npm from local files (the combination of `--dev` and `--use-npm` options) ends with missing packages in the newly created packages, as npm does not download dependencies of linked packages. Hence, we suggest using `yarn` when contributing to this project. - #### Developing the package Available scripts and their modifiers are described in the [`README.md` file of the `create-ckeditor5-plugin` package](/packages/create-ckeditor5-plugin). diff --git a/packages/create-ckeditor5-plugin/lib/index.js b/packages/create-ckeditor5-plugin/lib/index.js index 87e65b54..7975ab82 100755 --- a/packages/create-ckeditor5-plugin/lib/index.js +++ b/packages/create-ckeditor5-plugin/lib/index.js @@ -99,8 +99,7 @@ async function init( packageName, options ) { mkdirp.sync( directoryPath ); const packageVersions = getDependenciesVersions( { - devMode: options.dev, - useNpm: options.useNpm + devMode: options.dev } ); const dllConfiguration = getDllConfiguration( packageName ); 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 5d8f588d..bb314df3 100644 --- a/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js +++ b/packages/create-ckeditor5-plugin/lib/utils/get-dependencies-versions.js @@ -22,21 +22,11 @@ const getPackageVersion = require( './get-package-version' ); * * `true` - an absolute path to locally cloned package. * * `false` - the latest version published on npm. * - * Also, the `options.useNpm` modifier changes the protocol of the returned value: - * - * * `true` - use the `file:` protocol. - * * `false` - use the `link:` protocol. - * * @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, useNpm } ) { - // `npm` does not understand the `link:` protocol. When installing packages from a file system, - // it creates symlinks instead of copying the package files, while `yarn` does the opposite. - // However, `yarn` supports the `link:` protocol, so let's use it. - const protocol = useNpm ? 'file:' : 'link:'; - +module.exports = function getDependenciesVersions( { devMode } ) { return { ckeditor5: getPackageVersion( 'ckeditor5' ), devUtils: getPackageVersion( '@ckeditor/ckeditor5-dev-utils' ), @@ -44,7 +34,7 @@ module.exports = function getDependenciesVersions( { devMode, useNpm } ) { stylelintConfigCkeditor5: getPackageVersion( 'stylelint-config-ckeditor5' ), packageTools: devMode ? // Windows accepts unix-like paths in `package.json`, so let's unify it to avoid errors with paths. - protocol + path.resolve( __dirname, '..', '..', '..', 'ckeditor5-package-tools' ).split( path.sep ).join( path.posix.sep ) : + 'file:' + path.resolve( __dirname, '..', '..', '..', 'ckeditor5-package-tools' ).split( path.sep ).join( path.posix.sep ) : '^' + getPackageVersion( '@ckeditor/ckeditor5-package-tools' ) }; }; 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 9c3b1396..8f9791a8 100644 --- a/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js +++ b/packages/create-ckeditor5-plugin/tests/utils/get-dependencies-versions.js @@ -66,17 +66,7 @@ describe( 'lib/utils/get-dependencies-versions', () => { 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 (yarn)', () => { - const returnedValue = getDependenciesVersions( { devMode: true } ); - - const PROJECT_ROOT_DIRECTORY = path.join( __dirname, '..', '..', '..' ); - let packageTools = 'link:' + path.resolve( PROJECT_ROOT_DIRECTORY, 'ckeditor5-package-tools' ); - packageTools = packageTools.split( path.sep ).join( path.posix.sep ); - - expect( returnedValue.packageTools ).to.equal( packageTools ); - } ); - - it( 'it returns an absolute path to the "@ckeditor/ckeditor5-package-tools" package if "devMode" is enabled (npm)', () => { + it( 'it returns an absolute path to the "@ckeditor/ckeditor5-package-tools" package if "devMode" is enabled', () => { const returnedValue = getDependenciesVersions( { devMode: true, useNpm: true } ); const PROJECT_ROOT_DIRECTORY = path.join( __dirname, '..', '..', '..' ); From 6f4fbc3c09d19fdca43ba1b22d8d48f5b522a071 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Wed, 29 Sep 2021 15:25:56 +0200 Subject: [PATCH 5/5] Fixed invalid key in the package.json. --- packages/create-ckeditor5-plugin/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",