Use spawn instead of exec to auto-install adapters

This commit is contained in:
Aurélien Georget 2015-11-30 14:08:15 +01:00
parent b117e1d3ee
commit b7f674b86a
2 changed files with 17 additions and 32 deletions

View File

@ -308,21 +308,6 @@ module.exports = function (strapi) {
}); });
}, },
/**
* Subaction for config
*
* @param {Object} data
*
* @return {Function} cb
*/
handleConfig: function (data, cb) {
strapi.log.warn('We need to flush server.');
strapi.log.warn('Install dependencies if we have to.');
cb(null, true);
},
/** /**
* Pull global strapi variable from local server * Pull global strapi variable from local server
* *

View File

@ -7,7 +7,7 @@
// Node.js core. // Node.js core.
const cluster = require('cluster'); const cluster = require('cluster');
const path = require('path'); const path = require('path');
const exec = require('child_process').exec; const spawn = require('child_process').spawn;
// Public node modules. // Public node modules.
const _ = require('lodash'); const _ = require('lodash');
@ -75,15 +75,9 @@ module.exports = function (strapi) {
try { try {
strapi.adapters[name] = require(path.resolve(strapi.config.appPath, 'node_modules', adapter)); strapi.adapters[name] = require(path.resolve(strapi.config.appPath, 'node_modules', adapter));
} catch (err) { } catch (err) {
if (strapi.config.environment === 'development') {
strapi.log.warn('Installing the `' + adapter + '` adapter, please wait...');
spawn('npm', ['install', adapter, '--save']);
} else {
strapi.log.error('The adapter `' + adapter + '` is not installed.'); strapi.log.error('The adapter `' + adapter + '` is not installed.');
strapi.log.error('Execute `$ npm install ' + adapter + ' --save` to install it.');
process.exit(1); process.exit(1);
} }
}
}); });
// Check if the adapter in every connections exists. // Check if the adapter in every connections exists.
@ -266,23 +260,29 @@ module.exports = function (strapi) {
strapi.emit('hook:waterline:installed'); strapi.emit('hook:waterline:installed');
}); });
_.forEach(strapi.config.orm.adapters, function (adapter, name) { _.forEach(strapi.config.orm.adapters, function (adapter) {
try { try {
const isReady = require(path.resolve(strapi.config.appPath, 'node_modules', adapter)); require(path.resolve(strapi.config.appPath, 'node_modules', adapter));
done(); done();
} catch (err) { } catch (err) {
if (strapi.config.environment === 'development') { if (strapi.config.environment === 'development') {
const command = 'npm install ' + adapter + ' --save';
strapi.log.warn('Installing the `' + adapter + '` adapter, please wait...'); strapi.log.warn('Installing the `' + adapter + '` adapter, please wait...');
console.log();
// Run command const process = spawn('npm', ['install', adapter, '--save']);
exec(command, function(error, stdout, stderr) {
if (error) { process.on('error', function (error) {
strapi.log.error('The adapter `' + adapter + '` has not been installed.'); strapi.log.error('The adapter `' + adapter + '` has not been installed.');
strapi.log.error(error); strapi.log.error(error);
process.exit(1); process.exit(1);
});
process.on('close', function (code) {
if (code !== 0) {
strapi.log.error('The adapter `' + adapter + '` has not been installed.');
strapi.log.error('Code: ' + code);
process.exit(1);
} }
done(); done();