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
*

View File

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