@nka212bg/backend-utils 0.1.3 → 0.1.4

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.
Files changed (3) hide show
  1. package/misc.js +3 -2
  2. package/os.js +49 -1
  3. package/package.json +2 -1
package/misc.js CHANGED
@@ -238,7 +238,8 @@ exports.deleteRecursive = (path, pathTemp) => {
238
238
  if (pathTemp) this.deleteRecursive(pathTemp);
239
239
  };
240
240
 
241
- exports.copyRecursive = (source, destination) => {
241
+ exports.copyAny = (source, destination) => {
242
+ if (lstatSync(source).isFile()) return copyFileSync(source, destination);
242
243
  if (!existsSync(destination)) mkdirSync(destination, { recursive: true });
243
244
 
244
245
  readdirSync(source).forEach((item) => {
@@ -246,7 +247,7 @@ exports.copyRecursive = (source, destination) => {
246
247
  const destinationPath = `${destination}/${item}`;
247
248
 
248
249
  if (lstatSync(sourcePath).isDirectory()) {
249
- this.copyRecursive(sourcePath, destinationPath);
250
+ this.copyAny(sourcePath, destinationPath);
250
251
  } else {
251
252
  copyFileSync(sourcePath, destinationPath);
252
253
  }
package/os.js CHANGED
@@ -1,12 +1,60 @@
1
+ const { execSync } = require("child_process");
1
2
  const si = require("systeminformation");
2
3
  const osUtils = require("os-utils");
3
4
  const os = require("os");
4
- const { statfsSync } = require("fs");
5
+ const { statfsSync, readdirSync, existsSync, rmSync, lstatSync } = require("fs");
5
6
  const { shortCount, secToTime } = require("./misc");
7
+ const AdmZip = require("adm-zip");
6
8
 
7
9
  let cpuUsage, temperature;
8
10
  const serverStart = Date.now();
9
11
 
12
+ exports.restartApp = ({ service, password }) => {
13
+ try {
14
+ if (password) password = `echo ${password} | sudo -p ${password} `;
15
+ const execRes = execSync(`${password || ""} systemctl restart ${service}`);
16
+ return String(execRes);
17
+ } catch (error) {
18
+ return String(error);
19
+ }
20
+ };
21
+
22
+ exports.rebootServer = ({ password } = {}) => {
23
+ try {
24
+ if (password) password = `echo ${password} | sudo -p ${password} `;
25
+ const execRes = execSync(`${password || ""} reboot`);
26
+ return String(execRes);
27
+ } catch (error) {
28
+ return String(error);
29
+ }
30
+ };
31
+
32
+ exports.backupSystem = async ({ basePath, archiveName, overrideExisting, items } = {}) => {
33
+ try {
34
+ if (!items) throw "No items";
35
+ if (typeof items == "string") items = [items];
36
+
37
+ const startTime = Date.now();
38
+
39
+ basePath = basePath || `${__basedir}/backups`;
40
+ archiveName = archiveName || new Date().toISOString().split("T")[0];
41
+ const fullPath = `${basePath}/${archiveName}`;
42
+
43
+ const availableBackups = readdirSync(basePath);
44
+ if (availableBackups.length > 2) rmSync(`${basePath}/${availableBackups[0]}`, { recursive: true, force: true });
45
+
46
+ if (!overrideExisting && existsSync(`${fullPath}.zip`)) return console.log("backupSystem info, file awready exists --> ", `${fullPath}.zip`);
47
+
48
+ const zip = new AdmZip();
49
+ items.forEach((e) => (lstatSync(e).isFile() ? zip.addLocalFile(e) : zip.addLocalFolder(e)));
50
+ zip.writeZip(`${fullPath}.zip`);
51
+
52
+ console.info(`System backup --> time: ${parseInt((Date.now() - startTime) / 1000)}`);
53
+ } catch (error) {
54
+ console.error("backupSystem error --> ", error);
55
+ }
56
+ };
57
+
10
58
  exports.osStatus = () => {
11
59
  const now = Date.now();
12
60
 
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",
8
8
  "systeminformation": "^5.21.24",
9
+ "adm-zip": "^0.5.16",
9
10
  "nodemailer": "^6.9.14",
10
11
  "os-utils": "^0.0.14",
11
12
  "stripe": "^14.23.0",