-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.ts
107 lines (103 loc) · 2.86 KB
/
deploy.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
import {
ClientConfig,
ClientError,
DeployConfig,
FtpFunctionConfig,
} from "../types";
import { resolve } from "path";
import {
deleteDirectory,
uploadDirectory,
getClients,
ItemPool,
createLoggerFromPartialConfig,
} from "../utils";
export async function deploy(
deployConfig: DeployConfig,
clientConfig: ClientConfig,
ftpFunctionConfig: Partial<FtpFunctionConfig>
) {
const {
remoteRoot,
tempRoot,
oldRoot,
localRoot,
concurrency = 16,
} = deployConfig;
const logger = createLoggerFromPartialConfig(ftpFunctionConfig);
const clients = await getClients(ftpFunctionConfig)(
concurrency,
clientConfig
);
const clientPool = new ItemPool(clients);
const client = clients[0];
logger.info(
`Starting to deploy '${remoteRoot}' from '${localRoot}' using ${clients.length} connections.`
);
// Delete existing old deployment
return new Promise<void>(async (resolve) => {
logger.info("Task 1/7: Delete '" + tempRoot + "'.");
logger.info("Task 2/7: Delete '" + oldRoot + "'.");
const deleteTmp = deleteDirectory(ftpFunctionConfig)(clientPool, tempRoot);
const deleteOld = deleteDirectory(ftpFunctionConfig)(clientPool, oldRoot);
await Promise.all([deleteTmp, deleteOld]);
resolve();
})
.then(async () => {
try {
logger.info("Task 3/7: Create '" + tempRoot + "'.");
await client.mkdirAsync(tempRoot);
} catch (err) {
if (err && (err as ClientError).code !== 550) {
logger.error("Error when creating '" + tempRoot + "'.", err);
throw err;
}
}
})
.then(() => {
logger.info(`Task 4/7: Upload '${remoteRoot}' from '${localRoot}'.`);
const outTotalPath = resolve(localRoot);
return uploadDirectory(ftpFunctionConfig)(
clientPool,
tempRoot,
outTotalPath
);
})
.then(async () => {
try {
logger.info(
"Task 5/7: Rename '" + remoteRoot + "' => '" + oldRoot + "'."
);
await client.renameAsync(remoteRoot, oldRoot);
} catch (err) {
if (err && (err as ClientError).code !== 550) {
logger.error(
"Error when renaming '" + remoteRoot + "' => '" + oldRoot + "'."
);
throw err;
}
}
})
.then(async () => {
try {
logger.info(
"Task 6/7: Rename '" + tempRoot + "' => '" + remoteRoot + "'."
);
await client.renameAsync(tempRoot, remoteRoot);
} catch (err) {
logger.error(
"Error when renaming " + tempRoot + "' => '" + remoteRoot + "'."
);
throw err;
}
})
.then(() => {
logger.info("Task 7/7: Delete '" + oldRoot + "'.");
return deleteDirectory(ftpFunctionConfig)(clientPool, oldRoot);
})
.finally(() => {
clients.forEach((client) => {
client.end();
});
});
}