From 13013a40b461d82d5a8a40b738f08acc9cdea53b Mon Sep 17 00:00:00 2001 From: Jim LAURIE Date: Tue, 3 Jul 2018 16:43:08 +0200 Subject: [PATCH] Improve find npm global strapi performance --- packages/strapi-generate-new/lib/after.js | 4 ++-- packages/strapi-generate-new/lib/before.js | 2 +- packages/strapi-utils/lib/packageManager.js | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/strapi-generate-new/lib/after.js b/packages/strapi-generate-new/lib/after.js index 8a2e22078d..2f10131b84 100755 --- a/packages/strapi-generate-new/lib/after.js +++ b/packages/strapi-generate-new/lib/after.js @@ -40,7 +40,7 @@ module.exports = (scope, cb) => { const strapiDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') !== -1); const othersDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') === -1); // Add this check to know if we are in development mode so the creation is faster. - const isStrapiInstalledWithNPM = process.argv.indexOf('new') !== -1 && process.argv.indexOf('--dev') !== -1 || packageManager.isStrapiInstalledWithNPM(); + const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM(); const globalRootPath = isStrapiInstalledWithNPM ? execSync('npm root -g') : execSync(packageManager.commands('root -g')); // const globalRootPath = execSync('npm root -g'); @@ -103,7 +103,7 @@ module.exports = (scope, cb) => { pluginsInstallation(); } - + // Install default plugins and link dependencies. function pluginsInstallation() { const strapiBin = path.join(scope.strapiRoot, scope.strapiPackageJSON.bin.strapi); diff --git a/packages/strapi-generate-new/lib/before.js b/packages/strapi-generate-new/lib/before.js index 6dd167ab3d..7e05b493ec 100755 --- a/packages/strapi-generate-new/lib/before.js +++ b/packages/strapi-generate-new/lib/before.js @@ -214,7 +214,7 @@ module.exports = (scope, cb) => { }); }), new Promise(resolve => { - const isStrapiInstalledWithNPM = process.argv.indexOf('new') !== -1 && process.argv.indexOf('--dev') !== -1 || packageManager.isStrapiInstalledWithNPM(); + const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM(); let packageCmd = isStrapiInstalledWithNPM ? `npm install --prefix "${scope.tmpPath}" ${scope.client.connector}@alpha` : packageManager.commands('install --prefix', scope.tmpPath); // let cmd = `npm install --prefix "${scope.tmpPath}" ${scope.client.connector}@alpha`; // Manually create the temp directory for yarn diff --git a/packages/strapi-utils/lib/packageManager.js b/packages/strapi-utils/lib/packageManager.js index 8ae8b5ada6..de0ded255c 100644 --- a/packages/strapi-utils/lib/packageManager.js +++ b/packages/strapi-utils/lib/packageManager.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const shell = require('shelljs'); const { includes } = require('lodash'); @@ -26,11 +27,14 @@ module.exports = { if (process.argv.indexOf('new') !== -1 && process.argv.indexOf('--dev') !== -1) { skipCheck = true; } - + if (!skipCheck) { try { // Retrieve all the packages installed with NPM - const data = watcher('npm -g ls'); + const npmPath = watcher('npm root -g'); + + const data = fs.readdirSync(npmPath.trim()); + // Check if strapi is installed with NPM isNPM = includes(data, 'strapi'); @@ -51,7 +55,7 @@ module.exports = { commands: function (cmdType, path = '') { const isNPM = this.isStrapiInstalledWithNPM(); - + switch(cmdType) { case 'install --prefix': return isNPM ? `npm install --prefix ${path}` : `yarn --cwd ${path} add`; @@ -65,4 +69,4 @@ module.exports = { return ''; } } -}; \ No newline at end of file +};