@bprotsyk/aso-core 2.0.32 → 2.0.33

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.
@@ -169,35 +169,50 @@ class ServerUtil {
169
169
  const sftp = new ssh2_sftp_client_1.default();
170
170
  try {
171
171
  await sftp.connect(sftpConfig);
172
+ await sftp.stat(remoteDir).catch(async (err) => {
173
+ if (err.code === 2) {
174
+ // Код 2 означає, що директорія не існує
175
+ await sftp.mkdir(remoteDir, true);
176
+ console.log(`Папка створена: ${remoteDir}`);
177
+ }
178
+ else {
179
+ console.error(`Помилка під час перевірки або створення папки: ${remoteDir}`, err);
180
+ throw err;
181
+ }
182
+ });
172
183
  const items = fs_1.default.readdirSync(localDir, { withFileTypes: true });
173
- // Перебираємо кожен елемент (файл чи папку)
174
184
  for (const item of items) {
175
185
  const localPath = path_1.default.join(localDir, item.name);
176
186
  const remotePath = `${remoteDir}/${item.name}`;
177
187
  if (item.isDirectory()) {
178
- // Якщо це директорія, створюємо її на сервері
179
- await sftp.mkdir(remotePath, true);
180
- // Рекурсивно передаємо вміст цієї директорії
188
+ await sftp.stat(remotePath).catch(async (err) => {
189
+ if (err.code === 2) {
190
+ await sftp.mkdir(remotePath, true);
191
+ console.log(`Папка створена: ${remotePath}`);
192
+ }
193
+ else {
194
+ console.error(`Помилка під час перевірки або створення папки: ${remotePath}`, err);
195
+ throw err;
196
+ }
197
+ });
181
198
  await this.uploadDirectoryToSftp(localPath, remotePath, sftpConfig);
182
199
  }
183
200
  else {
184
- // Якщо це файл, передаємо його на сервер
185
201
  await sftp.put(localPath, remotePath);
186
202
  console.log(`Передано файл: ${localPath} -> ${remotePath}`);
187
203
  }
188
204
  }
189
- console.log('Усі файли та папки успішно передані.');
205
+ console.log("Усі файли та папки успішно передані.");
190
206
  }
191
207
  catch (err) {
192
- console.error('Помилка під час передавання файлів через SFTP:', err);
208
+ console.error("Помилка під час передавання файлів через SFTP:", err);
193
209
  }
194
210
  finally {
195
- // Закриваємо підключення до SFTP
196
211
  await sftp.end();
197
212
  }
198
213
  }
199
214
  async generateNginxConfig(domain, rootPath) {
200
- const templatePath = path_1.default.join(__dirname, '..', 'templates', 'nginx-template.conf');
215
+ const templatePath = path_1.default.join(__dirname, "..", "templates", "nginx-template.conf");
201
216
  const template = fs_1.default.readFileSync(templatePath, "utf8");
202
217
  const config = mustache_1.default.render(template, { domain, rootPath });
203
218
  return config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bprotsyk/aso-core",
3
- "version": "2.0.32",
3
+ "version": "2.0.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "scripts": {
@@ -14,17 +14,17 @@ export let PORT = 56777;
14
14
 
15
15
  interface FileObject {
16
16
  name: string;
17
- content: string; // Це буде Base64 закодовані дані
17
+ content: string; // Це буде Base64 закодовані дані
18
18
  }
19
19
 
20
- interface ISFTP{
21
- host:string ,
22
- port: number,
23
- username: string,
24
- password: string;
20
+ interface ISFTP {
21
+ host: string;
22
+ port: number;
23
+ username: string;
24
+ password: string;
25
25
  }
26
26
 
27
- export class ServerUtil {
27
+ export class ServerUtil {
28
28
  DOMAIN_HOME = "/etc/nginx/sites-enabled";
29
29
 
30
30
  ssh?: NodeSSH;
@@ -156,7 +156,7 @@ export class ServerUtil {
156
156
  return true;
157
157
  }
158
158
 
159
- // checking if the ssl certificate exists
159
+ // checking if the ssl certificate exists
160
160
  async domainNginxCertsExist(host: string): Promise<boolean> {
161
161
  try {
162
162
  let result = await this.exec(`ls /etc/letsencrypt/live/${host}`);
@@ -204,58 +204,75 @@ export class ServerUtil {
204
204
 
205
205
  return true;
206
206
  }
207
-
208
207
 
209
-
210
- async uploadDirectoryToSftp(
208
+ async uploadDirectoryToSftp(
211
209
  localDir: string,
212
210
  remoteDir: string,
213
211
  sftpConfig: ISFTP
214
212
  ) {
215
213
  const sftp = new Client();
216
-
214
+
217
215
  try {
218
216
  await sftp.connect(sftpConfig);
219
-
217
+
218
+ await sftp.stat(remoteDir).catch(async (err) => {
219
+ if (err.code === 2) {
220
+ // Код 2 означає, що директорія не існує
221
+ await sftp.mkdir(remoteDir, true);
222
+ console.log(`Папка створена: ${remoteDir}`);
223
+ } else {
224
+ console.error(
225
+ `Помилка під час перевірки або створення папки: ${remoteDir}`,
226
+ err
227
+ );
228
+ throw err;
229
+ }
230
+ });
231
+
220
232
  const items = fs.readdirSync(localDir, { withFileTypes: true });
221
-
222
- // Перебираємо кожен елемент (файл чи папку)
233
+
223
234
  for (const item of items) {
224
235
  const localPath = path.join(localDir, item.name);
225
236
  const remotePath = `${remoteDir}/${item.name}`;
226
-
237
+
227
238
  if (item.isDirectory()) {
228
- // Якщо це директорія, створюємо її на сервері
229
- await sftp.mkdir(remotePath, true);
230
-
231
- // Рекурсивно передаємо вміст цієї директорії
239
+ await sftp.stat(remotePath).catch(async (err) => {
240
+ if (err.code === 2) {
241
+ await sftp.mkdir(remotePath, true);
242
+ console.log(`Папка створена: ${remotePath}`);
243
+ } else {
244
+ console.error(
245
+ `Помилка під час перевірки або створення папки: ${remotePath}`,
246
+ err
247
+ );
248
+ throw err;
249
+ }
250
+ });
251
+
232
252
  await this.uploadDirectoryToSftp(localPath, remotePath, sftpConfig);
233
253
  } else {
234
- // Якщо це файл, передаємо його на сервер
235
254
  await sftp.put(localPath, remotePath);
236
255
  console.log(`Передано файл: ${localPath} -> ${remotePath}`);
237
256
  }
238
257
  }
239
-
240
- console.log('Усі файли та папки успішно передані.');
258
+
259
+ console.log("Усі файли та папки успішно передані.");
241
260
  } catch (err) {
242
- console.error('Помилка під час передавання файлів через SFTP:', err);
261
+ console.error("Помилка під час передавання файлів через SFTP:", err);
243
262
  } finally {
244
- // Закриваємо підключення до SFTP
245
263
  await sftp.end();
246
264
  }
247
265
  }
248
-
249
266
 
250
267
  async generateNginxConfig(domain: string, rootPath: string): Promise<string> {
251
- const templatePath = path.join(__dirname, '..', 'templates', 'nginx-template.conf');
268
+ const templatePath = path.join(
269
+ __dirname,
270
+ "..",
271
+ "templates",
272
+ "nginx-template.conf"
273
+ );
252
274
  const template = fs.readFileSync(templatePath, "utf8");
253
275
  const config = mustache.render(template, { domain, rootPath });
254
276
  return config;
255
277
  }
256
278
  }
257
-
258
-
259
-
260
-
261
-