UNPKG

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