@bprotsyk/aso-core 2.0.41 → 2.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,8 +11,10 @@ interface ISFTP {
11
11
  }
12
12
  export declare class ServerUtil {
13
13
  DOMAIN_HOME: string;
14
- ssh?: NodeSSH;
15
- constructor(ssh?: NodeSSH);
14
+ ssh: NodeSSH;
15
+ sftpConfig: ISFTP;
16
+ constructor(sftpConfig: ISFTP);
17
+ connectSSH(): Promise<void>;
16
18
  exec(command: string, options?: any): Promise<string>;
17
19
  generateSSHKey(): Promise<string>;
18
20
  createDirectories(): Promise<void>;
@@ -33,6 +35,7 @@ export declare class ServerUtil {
33
35
  ensureDirectoryExistence(dirPath: string): void;
34
36
  generateNginxConfig(domain: string, rootPath: string): Promise<string>;
35
37
  extractUploadedZip(name: string, file: any, extractToPath: string): Promise<void>;
36
- ensureDirectoryExistsViaSSH(sshConfig: ISFTP, remoteDir: string): Promise<void>;
38
+ ensureDirectoryExistsViaSSH(remoteDir: string): Promise<void>;
39
+ disconnectSSH(): Promise<void>;
37
40
  }
38
41
  export {};
@@ -20,8 +20,18 @@ exports.PORT = 56777;
20
20
  class ServerUtil {
21
21
  DOMAIN_HOME = "/etc/nginx/sites-enabled";
22
22
  ssh;
23
- constructor(ssh) {
24
- this.ssh = ssh;
23
+ sftpConfig;
24
+ constructor(sftpConfig) {
25
+ this.ssh = new node_ssh_1.NodeSSH();
26
+ this.sftpConfig = sftpConfig;
27
+ }
28
+ async connectSSH() {
29
+ await this.ssh.connect({
30
+ host: this.sftpConfig.host,
31
+ port: this.sftpConfig.port,
32
+ username: this.sftpConfig.username,
33
+ password: this.sftpConfig.password,
34
+ });
25
35
  }
26
36
  // Выконує команду або по SSH або локально, повертаючи текст незалежно від результату (помилка чи успіх)
27
37
  async exec(command, options) {
@@ -151,7 +161,7 @@ class ServerUtil {
151
161
  }
152
162
  // install certbot
153
163
  async setupCertbot() {
154
- await this.exec(`apt-get install -y certbot python3-certbot-nginx;`);
164
+ await this.ssh.execCommand(`apt-get install -y certbot python3-certbot-nginx;`);
155
165
  return true;
156
166
  }
157
167
  // creating a SSL certificate
@@ -254,13 +264,11 @@ class ServerUtil {
254
264
  console.error('Error extracting file:', err);
255
265
  }
256
266
  }
257
- async ensureDirectoryExistsViaSSH(sshConfig, remoteDir) {
258
- const ssh = new node_ssh_1.NodeSSH();
267
+ async ensureDirectoryExistsViaSSH(remoteDir) {
259
268
  try {
260
- await ssh.connect(sshConfig);
261
269
  // Перевірка, чи існує директорія, і створення її, якщо вона не існує
262
270
  const command = `if [ ! -d "${remoteDir}" ]; then mkdir -p "${remoteDir}"; fi`;
263
- await ssh.execCommand(command);
271
+ await this.ssh.execCommand(command);
264
272
  console.log(`Перевірено або створено папку: ${remoteDir}`);
265
273
  }
266
274
  catch (err) {
@@ -268,8 +276,11 @@ class ServerUtil {
268
276
  throw err;
269
277
  }
270
278
  finally {
271
- ssh.dispose();
279
+ this.ssh.dispose();
272
280
  }
273
281
  }
282
+ async disconnectSSH() {
283
+ this.ssh.dispose();
284
+ }
274
285
  }
275
286
  exports.ServerUtil = ServerUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bprotsyk/aso-core",
3
- "version": "2.0.41",
3
+ "version": "2.0.43",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "scripts": {
@@ -29,11 +29,22 @@ interface ISFTP {
29
29
  export class ServerUtil {
30
30
  DOMAIN_HOME = "/etc/nginx/sites-enabled";
31
31
 
32
- ssh?: NodeSSH;
32
+ ssh: NodeSSH;
33
+ sftpConfig: ISFTP;
33
34
 
34
- constructor(ssh?: NodeSSH) {
35
- this.ssh = ssh;
36
- }
35
+ constructor(sftpConfig: ISFTP) {
36
+ this.ssh = new NodeSSH();
37
+ this.sftpConfig = sftpConfig;
38
+ }
39
+
40
+ async connectSSH(): Promise<void> {
41
+ await this.ssh.connect({
42
+ host: this.sftpConfig.host,
43
+ port: this.sftpConfig.port,
44
+ username: this.sftpConfig.username,
45
+ password: this.sftpConfig.password,
46
+ });
47
+ }
37
48
 
38
49
  // Выконує команду або по SSH або локально, повертаючи текст незалежно від результату (помилка чи успіх)
39
50
  async exec(command: string, options?: any): Promise<string> {
@@ -183,7 +194,7 @@ export class ServerUtil {
183
194
 
184
195
  // install certbot
185
196
  async setupCertbot(): Promise<boolean> {
186
- await this.exec(`apt-get install -y certbot python3-certbot-nginx;`);
197
+ await this.ssh.execCommand(`apt-get install -y certbot python3-certbot-nginx;`);
187
198
 
188
199
  return true;
189
200
  }
@@ -318,15 +329,12 @@ export class ServerUtil {
318
329
  }
319
330
  }
320
331
 
321
- async ensureDirectoryExistsViaSSH(sshConfig:ISFTP, remoteDir:string) {
322
- const ssh = new NodeSSH();
332
+ async ensureDirectoryExistsViaSSH(remoteDir:string) {
323
333
 
324
334
  try {
325
- await ssh.connect(sshConfig);
326
-
327
- // Перевірка, чи існує директорія, і створення її, якщо вона не існує
335
+ // Перевірка, чи існує директорія, і створення її, якщо вона не існує
328
336
  const command = `if [ ! -d "${remoteDir}" ]; then mkdir -p "${remoteDir}"; fi`;
329
- await ssh.execCommand(command);
337
+ await this.ssh.execCommand(command);
330
338
  console.log(`Перевірено або створено папку: ${remoteDir}`);
331
339
  } catch (err) {
332
340
  console.error(
@@ -335,9 +343,14 @@ export class ServerUtil {
335
343
  );
336
344
  throw err;
337
345
  } finally {
338
- ssh.dispose();
346
+ this.ssh.dispose();
339
347
  }
340
348
  }
349
+ async disconnectSSH(): Promise<void> {
350
+ this.ssh.dispose();
351
+ }
352
+
353
+
341
354
  }
342
355
 
343
356