UNPKG

3.76 kBJavaScriptView Raw
1/**
2 * Created by Rodey on 2018/3/27.
3 */
4
5'use strict';
6
7const extend = require('extend'),
8 util = require('../utils'),
9 T = require('../tools'),
10 Deploy = require('./deploy'),
11 Backup = require('./backup'),
12 Rollback = require('./rollback');
13
14class DeployManager {
15 constructor(deploies, isWatch) {
16 this.deploies = deploies;
17 this.isWatch = isWatch;
18 this.init();
19 }
20
21 init() {
22 this.deploies && (this.deploies = this.deploies.map(deploy => new Deploy(deploy)));
23 }
24
25 setDeploy(deploies) {
26 if (!util.isArray(deploies)) return this;
27 this.deploies = extend(true, [], deploies);
28 this.init();
29 }
30
31 startDeploy(isSkipBackup) {
32 // 支持多节点部署
33 if (this.deploies.length > 0) {
34 // 过滤设置不执行的
35 const deploies = this.deploies.filter(deploy => deploy.host);
36 const backupAsDeploy = this.deploies.filter(deploy => deploy.backup)[0];
37
38 if (!isSkipBackup && !this._isBackupDone && backupAsDeploy) {
39 return this.startBackup(true);
40 }
41
42 // 递归部署
43 this._recursionDeploy.apply(this, [this.deploies]);
44 }
45 }
46
47 startBackup(isBeforeDeploy) {
48 let isRemove = T.hasArg(['r', 'remove-backup']);
49 if (isRemove) {
50 new Backup(null).removeBackup();
51 } else {
52 if (this.deploies.length === 0) T.log.error('× Not found deploy config');
53 // if (this._isBackupDone) this.startDeploy();
54 const deploy = this.deploies.filter(deploy => deploy.backup)[0];
55 const index = this.deploies.indexOf(deploy);
56
57 const backup = new Backup(deploy);
58 if (T.hasArg('out-path')) {
59 backup.outPath = T.getArg('out-path');
60 } else {
61 backup.outPath = deploy.backup.outPath;
62 }
63
64 _getArg('name');
65 _getArg('mode');
66 _getArg('log');
67
68 function _getArg(name) {
69 if (T.hasArg(name)) {
70 backup.options[name] = T.getArg(name);
71 }
72 }
73
74 // 如果设置部署前进行备份
75 backup.isExecute = true;
76 backup.init();
77 // 是否为清除备份
78
79 backup.start();
80 backup.on('backup_done', () => {
81 if (isBeforeDeploy) {
82 this._isBackupDone = true;
83 // 备份完成后删除备份配置,保证继续部署
84 delete this.deploies[index]['backup'];
85 this.startDeploy();
86 } else {
87 T.log.end();
88 }
89 });
90 }
91 }
92
93 startRollback() {
94 const deploies = this.deploies.filter(deploy => deploy.isRollback);
95 // 支持多节点回滚
96 if (deploies.length > 0) {
97 const rollback = new Rollback(deploies);
98 rollback.start();
99 }
100 }
101
102 _recursionDeploy(deploies, deploy) {
103 deploy = deploy || deploies[0];
104 if (deploy) {
105 deploy.start();
106 deploy.on('deploy_done', message => {
107 _vc.apply(this);
108 });
109 } else {
110 _vc.apply(this);
111 }
112
113 function _vc() {
114 deploies.splice(0, 1);
115 if (deploies.length > 0) {
116 this._recursionDeploy.apply(this, [deploies, deploies[0]]);
117 } else {
118 !this.isWatch && T.log.end();
119 }
120 }
121 }
122}
123
124module.exports = DeployManager;