diff --git a/packages/strapi-generate-admin/lib/after.js b/packages/strapi-generate-admin/lib/after.js index a04be51979..0a71643ba6 100755 --- a/packages/strapi-generate-admin/lib/after.js +++ b/packages/strapi-generate-admin/lib/after.js @@ -25,7 +25,7 @@ module.exports = (scope, cb) => { } // Install back-end admin `node_modules`. - const cmd = packageManager.isStrapiInstalledWithNPM ? 'npm install --production --ignore-scripts' : 'yarn install --production --ignore-scripts'; + const cmd = packageManager.isStrapiInstalledWithNPM() ? 'npm install --production --ignore-scripts' : 'yarn install --production --ignore-scripts'; exec(cmd, { cwd: path.resolve(scope.rootPath, 'admin') }, (err) => { diff --git a/packages/strapi-generate-new/json/package.json.js b/packages/strapi-generate-new/json/package.json.js index ba14205b2f..67054456e3 100755 --- a/packages/strapi-generate-new/json/package.json.js +++ b/packages/strapi-generate-new/json/package.json.js @@ -7,6 +7,7 @@ // Public node modules. const _ = require('lodash'); const uuid = require('uuid/v4'); +const { packageManager } = require('strapi-utils'); /** * Expose main package JSON of the application @@ -15,6 +16,8 @@ const uuid = require('uuid/v4'); module.exports = scope => { const cliPkg = scope.strapiPackageJSON || {}; + // Store the package manager info into the package.json + const pckManager = packageManager.isStrapiInstalledWithNPM() ? 'npm' : 'yarn'; // Let us install additional dependencies on a specific version. // Ex: it allows us to install the right version of knex. @@ -68,6 +71,7 @@ module.exports = scope => { 'url': scope.website || '' }], 'strapi': { + 'packageManager': pckManager, 'uuid': uuid() }, 'engines': { diff --git a/packages/strapi-generate-new/lib/after.js b/packages/strapi-generate-new/lib/after.js index ca56adcd0c..8a2e22078d 100755 --- a/packages/strapi-generate-new/lib/after.js +++ b/packages/strapi-generate-new/lib/after.js @@ -39,8 +39,9 @@ module.exports = (scope, cb) => { const dependencies = _.get(packageJSON, 'dependencies'); const strapiDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') !== -1); const othersDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') === -1); - const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM; - const globalRootPath = execSync(packageManager.commands('root -g')); + // 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 globalRootPath = isStrapiInstalledWithNPM ? execSync('npm root -g') : execSync(packageManager.commands('root -g')); // const globalRootPath = execSync('npm root -g'); // Verify if the dependencies are available into the global diff --git a/packages/strapi-generate-new/lib/before.js b/packages/strapi-generate-new/lib/before.js index 385c07ce5f..6dd167ab3d 100755 --- a/packages/strapi-generate-new/lib/before.js +++ b/packages/strapi-generate-new/lib/before.js @@ -214,8 +214,8 @@ module.exports = (scope, cb) => { }); }), new Promise(resolve => { - const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM; - let packageCmd = packageManager.commands('install --prefix', scope.tmpPath); + const isStrapiInstalledWithNPM = process.argv.indexOf('new') !== -1 && process.argv.indexOf('--dev') !== -1 || 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 if (!isStrapiInstalledWithNPM) { diff --git a/packages/strapi-utils/lib/packageManager.js b/packages/strapi-utils/lib/packageManager.js index 8f67b3f7a0..af321c0012 100644 --- a/packages/strapi-utils/lib/packageManager.js +++ b/packages/strapi-utils/lib/packageManager.js @@ -1,8 +1,8 @@ const shell = require('shelljs'); const { includes } = require('lodash'); -let isStrapiInstalledWithNPM; -let skipCheck = false; +// let isStrapiInstalledWithNPM = true; +// let skipCheck = false; const watcher = (cmd) => { const data = shell.exec(cmd, { @@ -10,44 +10,55 @@ const watcher = (cmd) => { }); if (includes(data.stderr, 'command not found') && data.code !== 0) { - console.log('ERRRRR'); throw new Error('Command not found'); } return data.stdout.toString(); }; -// Check if we are in development mode (working with the monorepo) -// So we don't run `npm -g ls` which takes time -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'); - // Check if strapi is installed with NPM - isStrapiInstalledWithNPM = includes(data, 'strapi'); - } catch(err) { - // If NPM is not installed strapi is installed with Yarn - isStrapiInstalledWithNPM = false; - } -} - module.exports = { - isStrapiInstalledWithNPM, + isStrapiInstalledWithNPM: () => { + let isNPM = true; + let skipCheck = false; + + // Check if we are in development mode (working with the monorepo) + // So we don't run `npm -g ls` which takes time + 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'); + // Check if strapi is installed with NPM + isNPM = includes(data, 'strapi'); + try { + const yarnData = watcher('yarn global -ls'); + isNPM = includes(yarnData, 'strapi'); + } catch(err) { + isNPM = true; + } + } catch(err) { + // If NPM is not installed strapi is installed with Yarn + isNPM = false; + } + } + + return isNPM; + }, commands: (cmdType, path = '') => { + const isNPM = module.isStrapiInstalledWithNPM(); switch(cmdType) { case 'install --prefix': - return isStrapiInstalledWithNPM ? `npm install --prefix ${path}` : `yarn --cwd ${path} add`; + return isNPM ? `npm install --prefix ${path}` : `yarn --cwd ${path} add`; case 'root -g': - return isStrapiInstalledWithNPM ? 'npm root -g' : 'yarn global dir'; + return isNPM ? 'npm root -g' : 'yarn global dir'; case 'install global': - return isStrapiInstalledWithNPM ? 'npm install' : 'yarn install'; + return isNPM ? 'npm install' : 'yarn install'; case 'install package': - return isStrapiInstalledWithNPM ? 'npm install' : 'yarn add'; + return isNPM ? 'npm install' : 'yarn add'; default: return ''; } diff --git a/packages/strapi/bin/strapi-install.js b/packages/strapi/bin/strapi-install.js index f4ccc32512..deebf61440 100755 --- a/packages/strapi/bin/strapi-install.js +++ b/packages/strapi/bin/strapi-install.js @@ -49,7 +49,6 @@ module.exports = function (plugin, cliArguments) { process.exit(0); } catch (e) { logger.error('An error occurred during plugin installation.'); - console.log('ERROR PLUGINS INSTALL', e); process.exit(1); } } else { @@ -57,7 +56,7 @@ module.exports = function (plugin, cliArguments) { logger.debug('Installing the plugin from npm registry.'); // Install the plugin from the npm registry. - const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM; + const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM(); if (!isStrapiInstalledWithNPM) { // Create the directory yarn doesn't do it it