From faa7032b69f2860d90b3af2e354f6ccf9074ab3f Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Wed, 3 May 2017 11:24:34 +0200 Subject: [PATCH 1/5] Install plugin from CLI --- packages/strapi/bin/strapi-install.js | 89 +++++++++++++++++++++++++++ packages/strapi/bin/strapi.js | 7 +++ 2 files changed, 96 insertions(+) create mode 100755 packages/strapi/bin/strapi-install.js diff --git a/packages/strapi/bin/strapi-install.js b/packages/strapi/bin/strapi-install.js new file mode 100755 index 0000000000..e824e678b6 --- /dev/null +++ b/packages/strapi/bin/strapi-install.js @@ -0,0 +1,89 @@ +#!/usr/bin/env node + +'use strict'; + +/** + * Module dependencies + */ + +// Node.js core. +const exec = require('child_process').exec; +const fs = require('fs'); +const path = require('path'); + +// Logger. +const logger = require('strapi-utils').logger; + +/** + * `$ strapi install` + * + * Install a Strapi plugin. + */ + +module.exports = function (plugin, cliArguments) { + // Define variables. + const pluginPrefix = 'strapi-plugin-'; + const pluginId = `${pluginPrefix}${plugin}`; + const pluginPath = `./plugins/${plugin}`; + + // Check that the plugin is not installed yet. + if (fs.existsSync(pluginPath)) { + logger.error(`It looks like this plugin is already installed. Please check in ${pluginPath}.`); + process.exit(1); + } + + // Progress message. + logger.debug('Installation in progress...'); + + if (cliArguments.dev) { + // Run `npm link` to create a symlink between the node module + // installed globally and the current Strapi application. + exec(`npm link ${pluginId}`, (err) => { + if (err) { + logger.error('It looks like this plugin is not installed globally.'); + process.exit(1); + } + + try { + // Create a symlink between the Strapi application and the node module installed. + fs.symlinkSync(`../node_modules/${pluginId}`, pluginPath, 'dir'); + + // Success. + logger.info('The plugin has been successfully installed.'); + process.exit(0); + } catch (err) { + logger.error('An error occurred during plugin installation.'); + process.exit(1); + } + }); + } else { + // Debug message. + logger.debug('Installing the plugin from npm registry.'); + + // Install the plugin from the npm registry. + exec(`npm install ${pluginId}`, (err) => { + if (err) { + logger.error(`An error occurred during plugin installation. \nPlease make sure this plugin is available on npm: https://www.npmjs.com/package/${pluginId}`); + process.exit(1); + } + + // Debug message. + logger.debug('Plugin successfully installed from npm registry.'); + + try { + // Debug message. + logger.debug(`Moving the \`node_modules/${pluginId}\` folder to the \`./plugins\` folder.`); + + // Move the plugin from the `node_modules` folder to the `./plugins` folder. + fs.renameSync(`./node_modules/${pluginId}`, pluginPath); + + // Success. + logger.info('The plugin has been successfully installed.'); + process.exit(0); + } catch (err) { + logger.error('An error occurred during plugin installation.'); + process.exit(1); + } + }); + } +}; diff --git a/packages/strapi/bin/strapi.js b/packages/strapi/bin/strapi.js index 362a46fd05..36c5b381e2 100755 --- a/packages/strapi/bin/strapi.js +++ b/packages/strapi/bin/strapi.js @@ -126,6 +126,13 @@ program .description('generate a custom generator') .action(require('./strapi-generate')); +// `$ strapi install` +program + .command('install ') + .option('-d, --dev', 'Development mode') + .description('install a Strapi plugin') + .action(require('./strapi-install')); + // `$ strapi migrate:make` program .command('migrate:make') From 7fecec7703c20fe3936f7484cdf8162f1de3c246 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Wed, 3 May 2017 11:35:10 +0200 Subject: [PATCH 2/5] Uninstall plugin from CLI --- packages/strapi/bin/strapi-uninstall.js | 45 +++++++++++++++++++++++++ packages/strapi/bin/strapi.js | 6 ++++ 2 files changed, 51 insertions(+) create mode 100755 packages/strapi/bin/strapi-uninstall.js diff --git a/packages/strapi/bin/strapi-uninstall.js b/packages/strapi/bin/strapi-uninstall.js new file mode 100755 index 0000000000..81753656f1 --- /dev/null +++ b/packages/strapi/bin/strapi-uninstall.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +'use strict'; + +/** + * Module dependencies + */ + +// Node.js core. +const exec = require('child_process').exec; +const fs = require('fs'); +const path = require('path'); +const rimraf = require('rimraf'); + +// Logger. +const logger = require('strapi-utils').logger; + +/** + * `$ strapi uninstall` + * + * Uninstall a Strapi plugin. + */ + +module.exports = function (plugin) { + // Define variables. + const pluginPath = `./plugins/${plugin}`; + + // Check that the plugin is installed. + if (!fs.existsSync(pluginPath)) { + logger.error(`It looks like this plugin is not installed. Please check that \`${pluginPath}\` folder exists.`); + process.exit(1); + } + + // Delete the plugin folder. + rimraf(pluginPath, (err) => { + if (err) { + logger.error('An error occurred during plugin uninstallation.'); + process.exit(1); + } + + // Success. + logger.info('The plugin has been successfully uninstalled.'); + process.exit(0); + }); +}; diff --git a/packages/strapi/bin/strapi.js b/packages/strapi/bin/strapi.js index 36c5b381e2..ba26cc9937 100755 --- a/packages/strapi/bin/strapi.js +++ b/packages/strapi/bin/strapi.js @@ -133,6 +133,12 @@ program .description('install a Strapi plugin') .action(require('./strapi-install')); +// `$ strapi uninstall` +program + .command('uninstall ') + .description('uninstall a Strapi plugin') + .action(require('./strapi-uninstall')); + // `$ strapi migrate:make` program .command('migrate:make') From 67d03d9b433103423023f599a26ed798416c62b6 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Wed, 3 May 2017 11:36:33 +0200 Subject: [PATCH 3/5] Improve installation logs --- packages/strapi/bin/strapi-install.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/strapi/bin/strapi-install.js b/packages/strapi/bin/strapi-install.js index e824e678b6..5e88fc8a27 100755 --- a/packages/strapi/bin/strapi-install.js +++ b/packages/strapi/bin/strapi-install.js @@ -28,7 +28,7 @@ module.exports = function (plugin, cliArguments) { // Check that the plugin is not installed yet. if (fs.existsSync(pluginPath)) { - logger.error(`It looks like this plugin is already installed. Please check in ${pluginPath}.`); + logger.error(`It looks like this plugin is already installed. Please check in \`${pluginPath}\`.`); process.exit(1); } From 580cb6ea9104e22029f120632847c278488d4442 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Wed, 3 May 2017 16:37:22 +0200 Subject: [PATCH 4/5] Check that we're in a valid Strapi project --- packages/strapi-utils/lib/cli.js | 27 +++++++++++++++++++++++++ packages/strapi-utils/lib/index.js | 1 + packages/strapi/bin/strapi-generate.js | 13 ++---------- packages/strapi/bin/strapi-install.js | 8 ++++++-- packages/strapi/bin/strapi-start.js | 7 ++++++- packages/strapi/bin/strapi-uninstall.js | 7 ++++++- 6 files changed, 48 insertions(+), 15 deletions(-) create mode 100755 packages/strapi-utils/lib/cli.js diff --git a/packages/strapi-utils/lib/cli.js b/packages/strapi-utils/lib/cli.js new file mode 100755 index 0000000000..67e1d27d56 --- /dev/null +++ b/packages/strapi-utils/lib/cli.js @@ -0,0 +1,27 @@ +'use strict'; + +/** + * Module dependencies + */ + +// Node.js core. +const path = require('path'); + +/** + * Check that we're in a valid Strapi project. + * + * @returns {boolean} + */ + +exports.isStrapiApp = () => { + const pathToPackageJSON = path.resolve(process.cwd(), 'package.json'); + let validPackageJSON = true; + + try { + require(pathToPackageJSON); + } catch (e) { + validPackageJSON = false; + } + + return validPackageJSON; +}; diff --git a/packages/strapi-utils/lib/index.js b/packages/strapi-utils/lib/index.js index 8b7e16af98..e020a1c534 100755 --- a/packages/strapi-utils/lib/index.js +++ b/packages/strapi-utils/lib/index.js @@ -5,6 +5,7 @@ */ module.exports = { + cli: require('./cli'), commander: require('./commander'), dictionary: require('./dictionary'), finder: require('./finder'), diff --git a/packages/strapi/bin/strapi-generate.js b/packages/strapi/bin/strapi-generate.js index 23b1da94fb..2d89be676a 100755 --- a/packages/strapi/bin/strapi-generate.js +++ b/packages/strapi/bin/strapi-generate.js @@ -13,7 +13,7 @@ const path = require('path'); const generate = require('strapi-generate'); // Logger. -const logger = require('strapi-utils').logger; +const { logger, cli } = require('strapi-utils'); /** * `$ strapi generate` @@ -36,16 +36,7 @@ module.exports = function (id, cliArguments) { // Check that we're in a valid Strapi project. if (scope.generatorType !== 'new' || scope.generatorType !== 'generator' || scope.generatorType !== 'hook') { - const pathToPackageJSON = path.resolve(scope.rootPath, 'package.json'); - let invalidPackageJSON; - - try { - require(pathToPackageJSON); - } catch (e) { - invalidPackageJSON = true; - } - - if (invalidPackageJSON) { + if (!cli.isStrapiApp()) { return logger.error('This command can only be used inside a Strapi project.'); } } diff --git a/packages/strapi/bin/strapi-install.js b/packages/strapi/bin/strapi-install.js index 5e88fc8a27..825505a845 100755 --- a/packages/strapi/bin/strapi-install.js +++ b/packages/strapi/bin/strapi-install.js @@ -9,10 +9,9 @@ // Node.js core. const exec = require('child_process').exec; const fs = require('fs'); -const path = require('path'); // Logger. -const logger = require('strapi-utils').logger; +const { logger, cli } = require('strapi-utils'); /** * `$ strapi install` @@ -26,6 +25,11 @@ module.exports = function (plugin, cliArguments) { const pluginId = `${pluginPrefix}${plugin}`; const pluginPath = `./plugins/${plugin}`; + // Check that we're in a valid Strapi project. + if (!cli.isStrapiApp()) { + return logger.error('This command can only be used inside a Strapi project.'); + } + // Check that the plugin is not installed yet. if (fs.existsSync(pluginPath)) { logger.error(`It looks like this plugin is already installed. Please check in \`${pluginPath}\`.`); diff --git a/packages/strapi/bin/strapi-start.js b/packages/strapi/bin/strapi-start.js index f865d6d01d..7d86d5f13b 100755 --- a/packages/strapi/bin/strapi-start.js +++ b/packages/strapi/bin/strapi-start.js @@ -18,7 +18,7 @@ const forever = require('forever-monitor'); const isLocalStrapiValid = require('../lib/private/isLocalStrapiValid'); // Logger. -const logger = require('strapi-utils').logger; +const { logger, cli } = require('strapi-utils'); /** * `$ strapi start` @@ -28,6 +28,11 @@ const logger = require('strapi-utils').logger; */ module.exports = function () { + // Check that we're in a valid Strapi project. + if (!cli.isStrapiApp()) { + return logger.error('This command can only be used inside a Strapi project.'); + } + try { // Set NODE_ENV if (_.isEmpty(process.env.NODE_ENV)) { diff --git a/packages/strapi/bin/strapi-uninstall.js b/packages/strapi/bin/strapi-uninstall.js index 81753656f1..260882295f 100755 --- a/packages/strapi/bin/strapi-uninstall.js +++ b/packages/strapi/bin/strapi-uninstall.js @@ -13,7 +13,7 @@ const path = require('path'); const rimraf = require('rimraf'); // Logger. -const logger = require('strapi-utils').logger; +const { logger, cli } = require('strapi-utils'); /** * `$ strapi uninstall` @@ -25,6 +25,11 @@ module.exports = function (plugin) { // Define variables. const pluginPath = `./plugins/${plugin}`; + // Check that we're in a valid Strapi project. + if (!cli.isStrapiApp()) { + return logger.error('This command can only be used inside a Strapi project.'); + } + // Check that the plugin is installed. if (!fs.existsSync(pluginPath)) { logger.error(`It looks like this plugin is not installed. Please check that \`${pluginPath}\` folder exists.`); From da2b7459b1949a13a38098a153de2828ebce8661 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Fri, 9 Jun 2017 15:59:24 +0100 Subject: [PATCH 5/5] Refactor code --- packages/strapi-utils/lib/cli.js | 6 +++++- packages/strapi/bin/strapi-install.js | 2 +- packages/strapi/bin/strapi-uninstall.js | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/strapi-utils/lib/cli.js b/packages/strapi-utils/lib/cli.js index 67e1d27d56..0c9ef59c8a 100755 --- a/packages/strapi-utils/lib/cli.js +++ b/packages/strapi-utils/lib/cli.js @@ -13,7 +13,7 @@ const path = require('path'); * @returns {boolean} */ -exports.isStrapiApp = () => { +const isStrapiApp = () => { const pathToPackageJSON = path.resolve(process.cwd(), 'package.json'); let validPackageJSON = true; @@ -25,3 +25,7 @@ exports.isStrapiApp = () => { return validPackageJSON; }; + +module.exports = { + isStrapiApp +}; diff --git a/packages/strapi/bin/strapi-install.js b/packages/strapi/bin/strapi-install.js index 825505a845..a034dc37bb 100755 --- a/packages/strapi/bin/strapi-install.js +++ b/packages/strapi/bin/strapi-install.js @@ -7,7 +7,7 @@ */ // Node.js core. -const exec = require('child_process').exec; +const { exec } = require('child_process'); const fs = require('fs'); // Logger. diff --git a/packages/strapi/bin/strapi-uninstall.js b/packages/strapi/bin/strapi-uninstall.js index 260882295f..88d794ba21 100755 --- a/packages/strapi/bin/strapi-uninstall.js +++ b/packages/strapi/bin/strapi-uninstall.js @@ -7,7 +7,7 @@ */ // Node.js core. -const exec = require('child_process').exec; +const { exec } = require('child_process'); const fs = require('fs'); const path = require('path'); const rimraf = require('rimraf');