@oneuptime/common 8.0.5466 → 8.0.5479

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.
@@ -1,26 +1,86 @@
1
1
  import { PromiseRejectErrorFunction } from "../../Types/FunctionTypes";
2
- import { ExecException, exec } from "node:child_process";
2
+ import {
3
+ ExecException,
4
+ ExecOptions,
5
+ exec,
6
+ execFile,
7
+ } from "node:child_process";
3
8
  import logger from "./Logger";
4
9
  import CaptureSpan from "./Telemetry/CaptureSpan";
5
10
 
6
11
  export default class Execute {
7
12
  @CaptureSpan()
8
- public static executeCommand(command: string): Promise<string> {
13
+ public static executeCommand(
14
+ command: string,
15
+ options?: ExecOptions,
16
+ ): Promise<string> {
9
17
  return new Promise(
10
18
  (
11
19
  resolve: (output: string) => void,
12
20
  reject: PromiseRejectErrorFunction,
13
21
  ) => {
14
- exec(`${command}`, (err: ExecException | null, stdout: string) => {
22
+ exec(
23
+ `${command}`,
24
+ {
25
+ ...options,
26
+ },
27
+ (err: ExecException | null, stdout: string, stderr: string) => {
15
28
  if (err) {
16
29
  logger.error(`Error executing command: ${command}`);
17
30
  logger.error(err);
18
31
  logger.error(stdout);
32
+ if (stderr) {
33
+ logger.error(stderr);
34
+ }
19
35
  return reject(err);
20
36
  }
21
37
 
38
+ if (stderr) {
39
+ logger.debug(stderr);
40
+ }
41
+
22
42
  return resolve(stdout);
23
- });
43
+ },
44
+ );
45
+ },
46
+ );
47
+ }
48
+
49
+ @CaptureSpan()
50
+ public static executeCommandFile(data: {
51
+ command: string;
52
+ args: Array<string>;
53
+ cwd: string;
54
+ }): Promise<string> {
55
+ return new Promise(
56
+ (
57
+ resolve: (output: string) => void,
58
+ reject: PromiseRejectErrorFunction,
59
+ ) => {
60
+ execFile(
61
+ data.command,
62
+ data.args,
63
+ {
64
+ cwd: data.cwd,
65
+ },
66
+ (err: ExecException | null, stdout: string, stderr: string) => {
67
+ if (err) {
68
+ logger.error(
69
+ `Error executing command: ${data.command} ${data.args.join(" ")}`,
70
+ );
71
+ logger.error(err);
72
+ logger.error(stdout);
73
+ logger.error(stderr);
74
+ return reject(err);
75
+ }
76
+
77
+ if (stderr) {
78
+ logger.debug(stderr);
79
+ }
80
+
81
+ return resolve(stdout);
82
+ },
83
+ );
24
84
  },
25
85
  );
26
86
  }
@@ -71,6 +71,46 @@ export default class LocalFile {
71
71
  );
72
72
  }
73
73
 
74
+ @CaptureSpan()
75
+ public static async deleteDirectory(path: string): Promise<void> {
76
+ if ((await this.doesDirectoryExist(path)) === false) {
77
+ return;
78
+ }
79
+
80
+ return new Promise(
81
+ (resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
82
+ fs.rm(path, { recursive: true, force: true }, (err: Error | null) => {
83
+ if (err) {
84
+ return reject(err);
85
+ }
86
+ resolve();
87
+ });
88
+ },
89
+ );
90
+ }
91
+
92
+ @CaptureSpan()
93
+ public static async readDirectory(path: string): Promise<Array<fs.Dirent>> {
94
+ return new Promise(
95
+ (
96
+ resolve: (entries: Array<fs.Dirent>) => void,
97
+ reject: PromiseRejectErrorFunction,
98
+ ) => {
99
+ fs.readdir(
100
+ path,
101
+ { withFileTypes: true },
102
+ (err: Error | null, entries: Array<fs.Dirent>) => {
103
+ if (err) {
104
+ return reject(err);
105
+ }
106
+
107
+ resolve(entries);
108
+ },
109
+ );
110
+ },
111
+ );
112
+ }
113
+
74
114
  @CaptureSpan()
75
115
  public static async getListOfDirectories(path: string): Promise<string[]> {
76
116
  return new Promise(