deploy.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const assignIn = require('lodash/assignIn');
  3. const clone = require('lodash/clone');
  4. const fs = require('hexo-fs');
  5. const chalk = require('chalk');
  6. const Promise = require('bluebird');
  7. function deployConsole(args) {
  8. let config = this.config.deploy;
  9. const deployers = this.extend.deployer.list();
  10. const self = this;
  11. if (!config) {
  12. let help = '';
  13. help += 'You should configure deployment settings in _config.yml first!\n\n';
  14. help += 'Available deployer plugins:\n';
  15. help += ` ${Object.keys(deployers).join(', ')}\n\n`;
  16. help += `For more help, you can check the online docs: ${chalk.underline('http://hexo.io/')}`;
  17. console.log(help);
  18. return;
  19. }
  20. return new Promise((resolve, reject) => {
  21. const generateArgs = clone(args);
  22. generateArgs.d = false;
  23. generateArgs.deploy = false;
  24. if (args.g || args.generate) {
  25. self.call('generate', args).then(resolve, reject);
  26. } else {
  27. fs.exists(self.public_dir, exist => {
  28. if (exist) return resolve();
  29. self.call('generate', args).then(resolve, reject);
  30. });
  31. }
  32. }).then(() => {
  33. self.emit('deployBefore');
  34. if (!Array.isArray(config)) config = [config];
  35. return config;
  36. }).each(item => {
  37. if (!item.type) return;
  38. const type = item.type;
  39. if (!deployers[type]) {
  40. self.log.error('Deployer not found: %s', chalk.magenta(type));
  41. return;
  42. }
  43. self.log.info('Deploying: %s', chalk.magenta(type));
  44. return deployers[type].call(self, assignIn({}, item, args)).then(() => {
  45. self.log.info('Deploy done: %s', chalk.magenta(type));
  46. });
  47. }).then(() => {
  48. self.emit('deployAfter');
  49. });
  50. }
  51. module.exports = deployConsole;