forked from SamKirkland/ftp-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.ts
213 lines (172 loc) · 5.97 KB
/
utilities.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
import prettyMilliseconds from "pretty-ms";
import { excludeDefaults } from "./module";
import { ErrorCode, IFtpDeployArguments, IFtpDeployArgumentsWithDefaults } from "./types";
import multimatch from "multimatch";
export interface ILogger {
all(...data: any[]): void;
standard(...data: any[]): void;
verbose(...data: any[]): void;
}
export class Logger implements ILogger {
constructor(level: "minimal" | "standard" | "verbose") {
this.level = level;
}
private level: "minimal" | "standard" | "verbose";
public all(...data: any[]): void {
console.log(...data);
}
public standard(...data: any[]): void {
if (this.level === "minimal") { return; }
console.log(...data);
}
public verbose(...data: any[]): void {
if (this.level !== "verbose") { return; }
console.log(...data);
}
}
export function pluralize(count: number, singular: string, plural: string) {
if (count === 1) {
return singular;
}
return plural;
}
export function formatNumber(number: number): string {
return number.toLocaleString();
}
/**
* retry a request
*
* @example retryRequest(logger, async () => await item());
*/
export async function retryRequest<T>(logger: ILogger, callback: () => Promise<T>): Promise<T> {
try {
return await callback();
}
catch (e: any) {
if (e.code >= 400 && e.code <= 499) {
logger.standard("400 level error from server when performing action - retrying...");
logger.standard(e);
if (e.code === ErrorCode.ConnectionClosed) {
logger.all("Connection closed. This library does not currently handle reconnects");
// await global.reconnect();
// todo reset current working dir
throw e;
}
return await callback();
}
else {
throw e;
}
}
}
interface ITimers {
[key: string]: Timer | undefined;
}
type AvailableTimers = "connecting" | "hash" | "upload" | "total" | "changingDir" | "logging";
export interface ITimings {
start(type: AvailableTimers): void;
stop(type: AvailableTimers): void;
getTime(type: AvailableTimers): number;
getTimeFormatted(type: AvailableTimers): string;
}
export class Timings implements ITimings {
private timers: ITimers = {};
public start(type: AvailableTimers): void {
if (this.timers[type] === undefined) {
this.timers[type] = new Timer();
}
this.timers[type]!.start();
}
public stop(type: AvailableTimers): void {
this.timers[type]!.stop();
}
public getTime(type: AvailableTimers): number {
const timer = this.timers[type];
if (timer === undefined || timer.time === null) {
return 0;
}
return timer.time;
}
public getTimeFormatted(type: AvailableTimers): string {
const timer = this.timers[type];
if (timer === undefined || timer.time === null) {
return "💣 Failed";
}
return prettyMilliseconds(timer.time, { verbose: true });
}
}
/**
* first number is seconds
* second number is nanoseconds
*/
type HRTime = [number, number];
export class Timer {
private totalTime: HRTime | null = null;
private startTime: HRTime | null = null;
private endTime: HRTime | null = null;
start() {
this.startTime = process.hrtime();
}
stop() {
if (this.startTime === null) {
throw new Error("Called .stop() before calling .start()");
}
this.endTime = process.hrtime(this.startTime);
const currentSeconds = this.totalTime === null ? 0 : this.totalTime[0];
const currentNS = this.totalTime === null ? 0 : this.totalTime[1];
this.totalTime = [
currentSeconds + this.endTime[0],
currentNS + this.endTime[1]
];
}
get time() {
if (this.totalTime === null) {
return null;
}
return (this.totalTime[0] * 1000) + (this.totalTime[1] / 1000000);
}
}
export function getDefaultSettings(withoutDefaults: IFtpDeployArguments): IFtpDeployArgumentsWithDefaults {
if (withoutDefaults["local-dir"] !== undefined) {
if (!withoutDefaults["local-dir"].endsWith("/")) {
throw new Error("local-dir should be a folder (must end with /)");
}
}
if (withoutDefaults["server-dir"] !== undefined) {
if (!withoutDefaults["server-dir"].endsWith("/")) {
throw new Error("server-dir should be a folder (must end with /)");
}
}
return {
"server": withoutDefaults.server,
"username": withoutDefaults.username,
"password": withoutDefaults.password,
"port": withoutDefaults.port ?? 21,
"protocol": withoutDefaults.protocol ?? "ftp",
"local-dir": withoutDefaults["local-dir"] ?? "./",
"server-dir": withoutDefaults["server-dir"] ?? "./",
"state-name": withoutDefaults["state-name"] ?? ".ftp-deploy-sync-state.json",
"dry-run": withoutDefaults["dry-run"] ?? false,
"dangerous-clean-slate": withoutDefaults["dangerous-clean-slate"] ?? false,
"exclude": withoutDefaults.exclude ?? excludeDefaults,
"log-level": withoutDefaults["log-level"] ?? "standard",
"security": withoutDefaults.security ?? "loose",
"timeout": withoutDefaults.timeout ?? 30000,
};
}
interface IStats {
path: string;
isDirectory(): boolean;
}
export function applyExcludeFilter(stat: IStats, excludeFilters: Readonly<string[]>): boolean {
// match exclude, return immediatley
if (excludeFilters.length > 0) {
// todo this could be a performance problem...
const pathWithFolderSlash = stat.path + (stat.isDirectory() ? "/" : "");
const excludeMatch = multimatch(pathWithFolderSlash, excludeFilters, { matchBase: true, dot: true });
if (excludeMatch.length > 0) {
return false;
}
}
return true;
}