Explorar o código

fix(cli): handle unhandled errors in execAsync

Nicolas Meienberger hai 1 ano
pai
achega
163d2f9374

+ 1 - 4
packages/cli/src/executors/app/app.executors.ts

@@ -2,8 +2,6 @@
 /* eslint-disable no-restricted-syntax */
 import fs from 'fs';
 import path from 'path';
-import { exec } from 'child_process';
-import { promisify } from 'util';
 import pg from 'pg';
 import { getEnv } from '@/utils/environment/environment';
 import { pathExists } from '@/utils/fs-helpers';
@@ -11,8 +9,7 @@ import { compose } from '@/utils/docker-helpers';
 import { copyDataDir, generateEnvFile } from './app.helpers';
 import { fileLogger } from '@/utils/logger/file-logger';
 import { TerminalSpinner } from '@/utils/logger/terminal-spinner';
-
-const execAsync = promisify(exec);
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 const getDbClient = async () => {
   const { postgresDatabase, postgresUsername, postgresPassword, postgresPort } = getEnv();

+ 1 - 4
packages/cli/src/executors/app/app.helpers.ts

@@ -2,13 +2,10 @@ import crypto from 'crypto';
 import fs from 'fs';
 import path from 'path';
 import { appInfoSchema, envMapToString, envStringToMap } from '@runtipi/shared';
-import { exec } from 'child_process';
-import { promisify } from 'util';
 import { getEnv } from '@/utils/environment/environment';
 import { generateVapidKeys, getAppEnvMap } from './env.helpers';
 import { pathExists } from '@/utils/fs-helpers';
-
-const execAsync = promisify(exec);
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 /**
  *  This function generates a random string of the provided length by using the SHA-256 hash algorithm.

+ 1 - 4
packages/cli/src/executors/repo/repo.executors.ts

@@ -1,12 +1,9 @@
 import { getEnv } from 'src/utils/environment/environment';
 import path from 'path';
-import { promisify } from 'util';
-import { exec } from 'child_process';
 import { pathExists } from '@/utils/fs-helpers';
 import { getRepoHash } from './repo.helpers';
 import { fileLogger } from '@/utils/logger/file-logger';
-
-const execAsync = promisify(exec);
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 export class RepoExecutors {
   private readonly logger;

+ 2 - 4
packages/cli/src/executors/system/system.executors.ts

@@ -7,10 +7,9 @@ import semver from 'semver';
 import axios from 'axios';
 import boxen from 'boxen';
 import path from 'path';
-import { exec, spawn } from 'child_process';
+import { spawn } from 'child_process';
 import si from 'systeminformation';
 import { Stream } from 'stream';
-import { promisify } from 'util';
 import dotenv from 'dotenv';
 import { SystemEvent } from '@runtipi/shared';
 import chalk from 'chalk';
@@ -23,8 +22,7 @@ import { getEnv } from '@/utils/environment/environment';
 import { fileLogger } from '@/utils/logger/file-logger';
 import { runPostgresMigrations } from '@/utils/migrations/run-migration';
 import { getUserIds } from '@/utils/environment/user';
-
-const execAsync = promisify(exec);
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 export class SystemExecutors {
   private readonly rootFolder: string;

+ 1 - 4
packages/cli/src/executors/system/system.helpers.ts

@@ -3,12 +3,11 @@ import fs from 'fs';
 import path from 'path';
 import os from 'os';
 import { envMapToString, envStringToMap, settingsSchema } from '@runtipi/shared';
-import { exec } from 'child_process';
-import { promisify } from 'util';
 import chalk from 'chalk';
 import { pathExists } from '@/utils/fs-helpers';
 import { getRepoHash } from '../repo/repo.helpers';
 import { fileLogger } from '@/utils/logger/file-logger';
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 type EnvKeys =
   | 'APPS_REPO_ID'
@@ -38,8 +37,6 @@ type EnvKeys =
   // eslint-disable-next-line @typescript-eslint/ban-types
   | (string & {});
 
-const execAsync = promisify(exec);
-
 const DEFAULT_REPO_URL = 'https://github.com/meienberger/runtipi-appstore';
 
 /**

+ 1 - 4
packages/cli/src/services/watcher/watcher.ts

@@ -1,13 +1,10 @@
 import { eventSchema } from '@runtipi/shared';
 import { Worker } from 'bullmq';
-import { exec } from 'child_process';
-import { promisify } from 'util';
 import { AppExecutors, RepoExecutors, SystemExecutors } from '@/executors';
 import { getEnv } from '@/utils/environment/environment';
 import { getUserIds } from '@/utils/environment/user';
 import { fileLogger } from '@/utils/logger/file-logger';
-
-const execAsync = promisify(exec);
+import { execAsync } from '@/utils/exec-async/execAsync';
 
 const runCommand = async (jobData: unknown) => {
   const { gid, uid } = getUserIds();

+ 1 - 4
packages/cli/src/utils/docker-helpers/docker-helpers.ts

@@ -1,11 +1,8 @@
 import path from 'path';
-import { promisify } from 'util';
-import { exec } from 'child_process';
 import { getEnv } from '../environment/environment';
 import { pathExists } from '../fs-helpers/fs-helpers';
 import { fileLogger } from '../logger/file-logger';
-
-const execAsync = promisify(exec);
+import { execAsync } from '../exec-async/execAsync';
 
 const composeUp = async (args: string[]) => {
   const { stdout, stderr } = await execAsync(`docker compose ${args.join(' ')}`);

+ 18 - 0
packages/cli/src/utils/exec-async/execAsync.tsx

@@ -0,0 +1,18 @@
+import { exec } from 'child_process';
+import { promisify } from 'util';
+
+type ExecAsyncParams = [command: string];
+
+type ExecResult = { stdout: string; stderr: string };
+
+export const execAsync = async (...args: ExecAsyncParams): Promise<ExecResult> => {
+  try {
+    return await promisify(exec)(...args);
+  } catch (error) {
+    if (error instanceof Error) {
+      return { stderr: error.message, stdout: '' };
+    }
+
+    return { stderr: String(error), stdout: '' };
+  }
+};

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 12 - 560
pnpm-lock.yaml


Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio