-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
227 lines (201 loc) · 6.37 KB
/
gulpfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import * as fs from "fs";
import * as rimraf from "rimraf";
import * as vinylfs from "vinyl-fs";
import ftp = require("vinyl-ftp");
export interface ISecureOptions {
/**
* Set false for self-signed or expired secure FTP connections.
*/
rejectUnauthorized: boolean;
}
export interface IFtpConfiguration {
/**
* FTP host
* @default localhost
*/
host?: string;
/**
* FTP user
* @default anonymous
*/
user?: string;
/**
* FTP password
* @default anonymous@
*/
password?: string;
/**
* FTP port
* @default 21
*/
port?: number;
/**
* Log function
* @default null
*/
log?: (message?: any, ...optionalParams: any[]) => void;
/**
* Offset server time by this number of minutes
* @default 0
*/
timeOffset?: number;
/**
* Number of parallel transfers
* @default 3
*/
parallel?: number;
/**
* Maximum number of connections, should be greater or equal to "parallel".
* Default is 5, or the parallel setting.
* Don't worry about setting this too high, vinyl-ftp recovers from "Too many connections" errors nicely.
* @default 5
*/
maxConnections?: number;
/**
* Clear caches before (each) stream
* @default false
*/
reload?: boolean;
/**
* Time to keep idle FTP connections (milliseconds)
* @default 100
*/
idleTimeout?: number;
/**
* A debug callback that gets extensive debug information
* @default null
*/
debug?: (...params: any[]) => void;
/**
* Set true for secured FTP connections
* @default false
*/
secure?: boolean;
/**
* Security Options
*/
secureOptions?: ISecureOptions;
}
export type Glob = string | string[];
export type TestFunction = () => any;
function trimTrailingSlashes(input: string) {
while (input.endsWith("/")) {
input = input.substr(0, input.length - 1);
}
return input;
}
export default class FtpDeployment {
private testConnection: ftp;
private prodConnection: ftp;
private targetFolder: string;
private backupDir: string;
private deploymentFiles: NodeJS.ReadWriteStream;
/**
*
* @param testFtpConfiguration
* @param prodFtpConfiguration
* @param deploymentFiles gulp.src(glob, { base: ".", buffer: false })
* @param targetFolder @default "/site/wwwroot"
* @param backupDir @default "./backup"
*/
constructor(testFtpConfiguration: IFtpConfiguration, prodFtpConfiguration: IFtpConfiguration,
deploymentFiles: NodeJS.ReadWriteStream,
targetFolder: string = "/site/wwwroot", backupDir: string = "./backup") {
this.backupDir = backupDir;
this.targetFolder = trimTrailingSlashes(targetFolder);
this.testConnection = ftp.create(testFtpConfiguration);
this.prodConnection = ftp.create(prodFtpConfiguration);
this.deploymentFiles = deploymentFiles;
}
/**
*
* @param tests An array of test functions that should return an error if the test fails.
*/
public async deploy(tests?: TestFunction[]) {
await this.backupDeployTest(this.testConnection, tests);
await this.backupDeployTest(this.prodConnection, tests);
}
private async backupDeployTest(conn: ftp, tests?: TestFunction[]) {
await this.backup(conn);
try {
await this.deploySource(conn, this.deploymentFiles);
if (tests) {
const errors = tests.map((testFunction) => testFunction());
if (errors.length > 0) {
throw errors;
}
}
} catch (e) {
// Rollback
try {
await this.deploySource(conn, vinylfs.src(this.backupDir));
await this.clearBackup();
// tslint:disable-next-line:no-empty
} catch (ee) { }
throw e;
}
await this.clearBackup();
}
private async clearBackup() {
return new Promise((resolve, reject) => {
fs.exists(this.backupDir, (exists) => {
if (!exists) {
resolve();
return;
}
rimraf(this.backupDir, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
});
}
private async backup(conn: ftp) {
await this.clearBackup();
return new Promise(async (resolve, reject) => {
fs.mkdir(this.backupDir, (err) => {
if (err) {
reject(err);
return;
}
conn.src(`${this.targetFolder}/**/*`)
.pipe(vinylfs.dest(this.backupDir))
.on("end", resolve)
.on("error", (...args) => { reject(args); });
});
});
}
private async deploySource(conn: ftp, source: NodeJS.ReadWriteStream) {
return new Promise((resolve, reject) => {
conn.rmdir(this.targetFolder, (e) => {
if (e && (!e.message || e.message !== "The system cannot find the file specified.")) {
reject(e);
return;
}
source
.pipe(conn.dest(this.targetFolder))
.on("end", resolve)
.on("error", (...args) => { reject(args); });
});
});
}
}
const azureFuncSrcGlob = ["./host.json", "./!(node_modules|backup)/!(*.ts)"];
export function getConfiguration(environment: string): IFtpConfiguration {
const port = process.env[`ftp_port_${environment}`];
const secure = process.env[`ftp_secure_${environment}`];
return {
host: process.env[`ftp_host_${environment}`],
user: process.env[`ftp_user_${environment}`],
password: process.env[`ftp_password_${environment}`],
port: port && /^[\d]{1-5}$/.test(port) ? parseInt(port, 10) : 21,
secure: secure && /^(false|0)$/.test(secure) ? false : true
};
}
const deployment = new FtpDeployment(getConfiguration("test"), getConfiguration("prod"),
vinylfs.src(azureFuncSrcGlob, { base: ".", buffer: false }));
// const tests = [() => new Error("fhaksjdhjk")];
deployment.deploy();