mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-03 19:36:20 +00:00 
			
		
		
		
	Merge pull request #46 from wistityhq/feature/installation
Auto install adapters and template engines on restart
This commit is contained in:
		
						commit
						e9e54b8667
					
				@ -6,6 +6,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Node.js core.
 | 
					// Node.js core.
 | 
				
			||||||
const path = require('path');
 | 
					const path = require('path');
 | 
				
			||||||
 | 
					const spawn = require('child_process').spawn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Public node modules.
 | 
					// Public node modules.
 | 
				
			||||||
const _ = require('lodash');
 | 
					const _ = require('lodash');
 | 
				
			||||||
@ -56,6 +57,52 @@ module.exports = function (strapi) {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      cb();
 | 
					      cb();
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Installation template engines
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    installation: function () {
 | 
				
			||||||
 | 
					      const done = _.after(_.size(strapi.config.views.map), function () {
 | 
				
			||||||
 | 
					        strapi.emit('hook:views:installed');
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      _.forEach(strapi.config.views.map, function (engine) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					          require(path.resolve(strapi.config.appPath, 'node_modules', engine));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          done();
 | 
				
			||||||
 | 
					        } catch (err) {
 | 
				
			||||||
 | 
					          if (strapi.config.environment === 'development') {
 | 
				
			||||||
 | 
					            strapi.log.warn('Installing the `' + engine + '` template engine, please wait...');
 | 
				
			||||||
 | 
					            console.log();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const process = spawn('npm', ['install', engine, '--save']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            process.on('error', function (error) {
 | 
				
			||||||
 | 
					              strapi.log.error('The template engine `' + engine + '` has not been installed.');
 | 
				
			||||||
 | 
					              strapi.log.error(error);
 | 
				
			||||||
 | 
					              process.exit(1);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            process.on('close', function (code) {
 | 
				
			||||||
 | 
					              if (code !== 0) {
 | 
				
			||||||
 | 
					                strapi.log.error('The template engine `' + engine + '` has not been installed.');
 | 
				
			||||||
 | 
					                strapi.log.error('Code: ' + code);
 | 
				
			||||||
 | 
					                process.exit(1);
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              strapi.log.info('`' + engine + '` successfully installed');
 | 
				
			||||||
 | 
					              done();
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            strapi.log.error('The template engine `' + engine + '` is not installed.');
 | 
				
			||||||
 | 
					            strapi.log.error('Execute `$ npm install ' + engine + ' --save` to install it.');
 | 
				
			||||||
 | 
					            process.exit(1);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -285,6 +285,7 @@ module.exports = function (strapi) {
 | 
				
			|||||||
                process.exit(1);
 | 
					                process.exit(1);
 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              strapi.log.info('`' + adapter + '` successfully installed');
 | 
				
			||||||
              done();
 | 
					              done();
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
          } else {
 | 
					          } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -141,7 +141,7 @@ module.exports = function (strapi) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      // Prepare all other hooks.
 | 
					      // Prepare all other hooks.
 | 
				
			||||||
      prepare: function prepareHooks(cb) {
 | 
					      prepare: function prepareHooks(cb) {
 | 
				
			||||||
        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
 | 
					        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
 | 
				
			||||||
          prepareHook(id);
 | 
					          prepareHook(id);
 | 
				
			||||||
          process.nextTick(cb);
 | 
					          process.nextTick(cb);
 | 
				
			||||||
        }, cb);
 | 
					        }, cb);
 | 
				
			||||||
@ -149,7 +149,7 @@ module.exports = function (strapi) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      // Apply the default config for all other hooks.
 | 
					      // Apply the default config for all other hooks.
 | 
				
			||||||
      defaults: function defaultConfigHooks(cb) {
 | 
					      defaults: function defaultConfigHooks(cb) {
 | 
				
			||||||
        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
 | 
					        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
 | 
				
			||||||
          const hook = hooks[id];
 | 
					          const hook = hooks[id];
 | 
				
			||||||
          applyDefaults(hook);
 | 
					          applyDefaults(hook);
 | 
				
			||||||
          process.nextTick(cb);
 | 
					          process.nextTick(cb);
 | 
				
			||||||
@ -158,7 +158,7 @@ module.exports = function (strapi) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      // Load all other hooks.
 | 
					      // Load all other hooks.
 | 
				
			||||||
      load: function loadOtherHooks(cb) {
 | 
					      load: function loadOtherHooks(cb) {
 | 
				
			||||||
        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
 | 
					        async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
 | 
				
			||||||
          loadHook(id, cb);
 | 
					          loadHook(id, cb);
 | 
				
			||||||
        }, cb);
 | 
					        }, cb);
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
@ -171,6 +171,16 @@ module.exports = function (strapi) {
 | 
				
			|||||||
        prepareHook('router');
 | 
					        prepareHook('router');
 | 
				
			||||||
        applyDefaults(hooks.router);
 | 
					        applyDefaults(hooks.router);
 | 
				
			||||||
        loadHook('router', cb);
 | 
					        loadHook('router', cb);
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Load the waterline hook.
 | 
				
			||||||
 | 
					      waterline: function loadWaterlineHook(cb) {
 | 
				
			||||||
 | 
					        if (!hooks.waterline) {
 | 
				
			||||||
 | 
					          return cb();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        prepareHook('waterline');
 | 
				
			||||||
 | 
					        applyDefaults(hooks.waterline);
 | 
				
			||||||
 | 
					        loadHook('waterline', cb);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -42,6 +42,7 @@ module.exports = cb => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  // Callback.
 | 
					  // Callback.
 | 
				
			||||||
  err => {
 | 
					  err => {
 | 
				
			||||||
 | 
					    let count = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Just in case there is an error.
 | 
					    // Just in case there is an error.
 | 
				
			||||||
    if (err) {
 | 
					    if (err) {
 | 
				
			||||||
@ -57,12 +58,21 @@ module.exports = cb => {
 | 
				
			|||||||
    // Run adapters installation
 | 
					    // Run adapters installation
 | 
				
			||||||
    if (cluster.isMaster) {
 | 
					    if (cluster.isMaster) {
 | 
				
			||||||
      self.hooks.waterline.installation();
 | 
					      self.hooks.waterline.installation();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ++count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (_.isPlainObject(self.config.views) && !_.isBoolean(self.config.views)) {
 | 
				
			||||||
 | 
					        self.hooks.views.installation();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ++count;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Install new adapters
 | 
					    const installed = _.after(count, () => {
 | 
				
			||||||
    strapi.after('hook:waterline:installed', () => {
 | 
					      if (_.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers > 0) {
 | 
				
			||||||
      self.log.warn('Application is restarting...');
 | 
					        self.log.warn('Application is restarting...');
 | 
				
			||||||
      console.log();
 | 
					        console.log();
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Teardown Waterline adapters and
 | 
					      // Teardown Waterline adapters and
 | 
				
			||||||
      // reload the Waterline ORM.
 | 
					      // reload the Waterline ORM.
 | 
				
			||||||
@ -99,5 +109,13 @@ module.exports = cb => {
 | 
				
			|||||||
      // Reloading the ORM.
 | 
					      // Reloading the ORM.
 | 
				
			||||||
      self.hooks.waterline.reload();
 | 
					      self.hooks.waterline.reload();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.after('hook:waterline:installed', () => {
 | 
				
			||||||
 | 
					      installed();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.after('hook:views:installed', () => {
 | 
				
			||||||
 | 
					      installed();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user