| 
 | 1 | +/**  | 
 | 2 | + * @license  | 
 | 3 | + * Copyright Google LLC All Rights Reserved.  | 
 | 4 | + *  | 
 | 5 | + * Use of this source code is governed by an MIT-style license that can be  | 
 | 6 | + * found in the LICENSE file at https://angular.dev/license  | 
 | 7 | + */  | 
 | 8 | + | 
 | 9 | +/**  | 
 | 10 | + * @fileoverview  | 
 | 11 | + * This file defines an abstraction layer for operating-system or file-system operations, such as  | 
 | 12 | + * command execution. This allows for easier testing by enabling the injection of mock or  | 
 | 13 | + * test-specific implementations.  | 
 | 14 | + */  | 
 | 15 | + | 
 | 16 | +import { existsSync as nodeExistsSync } from 'fs';  | 
 | 17 | +import { spawn } from 'node:child_process';  | 
 | 18 | +import { Stats } from 'node:fs';  | 
 | 19 | +import { stat } from 'node:fs/promises';  | 
 | 20 | + | 
 | 21 | +/**  | 
 | 22 | + * An error thrown when a command fails to execute.  | 
 | 23 | + */  | 
 | 24 | +export class CommandError extends Error {  | 
 | 25 | +  constructor(  | 
 | 26 | +    message: string,  | 
 | 27 | +    public readonly stdout: string,  | 
 | 28 | +    public readonly stderr: string,  | 
 | 29 | +    public readonly code: number | null,  | 
 | 30 | +  ) {  | 
 | 31 | +    super(message);  | 
 | 32 | +  }  | 
 | 33 | +}  | 
 | 34 | + | 
 | 35 | +/**  | 
 | 36 | + * An abstraction layer for operating-system or file-system operations.  | 
 | 37 | + */  | 
 | 38 | +export interface Host {  | 
 | 39 | +  /**  | 
 | 40 | +   * Gets the stats of a file or directory.  | 
 | 41 | +   * @param path The path to the file or directory.  | 
 | 42 | +   * @returns A promise that resolves to the stats.  | 
 | 43 | +   */  | 
 | 44 | +  stat(path: string): Promise<Stats>;  | 
 | 45 | + | 
 | 46 | +  /**  | 
 | 47 | +   * Checks if a path exists on the file system.  | 
 | 48 | +   * @param path The path to check.  | 
 | 49 | +   * @returns A boolean indicating whether the path exists.  | 
 | 50 | +   */  | 
 | 51 | +  existsSync(path: string): boolean;  | 
 | 52 | + | 
 | 53 | +  /**  | 
 | 54 | +   * Spawns a child process and returns a promise that resolves with the process's  | 
 | 55 | +   * output or rejects with a structured error.  | 
 | 56 | +   * @param command The command to run.  | 
 | 57 | +   * @param args The arguments to pass to the command.  | 
 | 58 | +   * @param options Options for the child process.  | 
 | 59 | +   * @returns A promise that resolves with the standard output and standard error of the command.  | 
 | 60 | +   */  | 
 | 61 | +  runCommand(  | 
 | 62 | +    command: string,  | 
 | 63 | +    args: readonly string[],  | 
 | 64 | +    options?: {  | 
 | 65 | +      timeout?: number;  | 
 | 66 | +      stdio?: 'pipe' | 'ignore';  | 
 | 67 | +      cwd?: string;  | 
 | 68 | +      env?: Record<string, string>;  | 
 | 69 | +    },  | 
 | 70 | +  ): Promise<{ stdout: string; stderr: string }>;  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +/**  | 
 | 74 | + * A concrete implementation of the `Host` interface that runs on a local workspace.  | 
 | 75 | + */  | 
 | 76 | +export const LocalWorkspaceHost: Host = {  | 
 | 77 | +  stat,  | 
 | 78 | +  existsSync: nodeExistsSync,  | 
 | 79 | +  runCommand: async (  | 
 | 80 | +    command: string,  | 
 | 81 | +    args: readonly string[],  | 
 | 82 | +    options: {  | 
 | 83 | +      timeout?: number;  | 
 | 84 | +      stdio?: 'pipe' | 'ignore';  | 
 | 85 | +      cwd?: string;  | 
 | 86 | +      env?: Record<string, string>;  | 
 | 87 | +    } = {},  | 
 | 88 | +  ): Promise<{ stdout: string; stderr: string }> => {  | 
 | 89 | +    const signal = options.timeout ? AbortSignal.timeout(options.timeout) : undefined;  | 
 | 90 | + | 
 | 91 | +    return new Promise((resolve, reject) => {  | 
 | 92 | +      const childProcess = spawn(command, args, {  | 
 | 93 | +        shell: false,  | 
 | 94 | +        stdio: options.stdio ?? 'pipe',  | 
 | 95 | +        signal,  | 
 | 96 | +        cwd: options.cwd,  | 
 | 97 | +        env: {  | 
 | 98 | +          ...process.env,  | 
 | 99 | +          ...options.env,  | 
 | 100 | +        },  | 
 | 101 | +      });  | 
 | 102 | + | 
 | 103 | +      let stdout = '';  | 
 | 104 | +      childProcess.stdout?.on('data', (data) => (stdout += data.toString()));  | 
 | 105 | + | 
 | 106 | +      let stderr = '';  | 
 | 107 | +      childProcess.stderr?.on('data', (data) => (stderr += data.toString()));  | 
 | 108 | + | 
 | 109 | +      childProcess.on('close', (code) => {  | 
 | 110 | +        if (code === 0) {  | 
 | 111 | +          resolve({ stdout, stderr });  | 
 | 112 | +        } else {  | 
 | 113 | +          const message = `Process exited with code ${code}.`;  | 
 | 114 | +          reject(new CommandError(message, stdout, stderr, code));  | 
 | 115 | +        }  | 
 | 116 | +      });  | 
 | 117 | + | 
 | 118 | +      childProcess.on('error', (err) => {  | 
 | 119 | +        if (err.name === 'AbortError') {  | 
 | 120 | +          const message = `Process timed out.`;  | 
 | 121 | +          reject(new CommandError(message, stdout, stderr, null));  | 
 | 122 | + | 
 | 123 | +          return;  | 
 | 124 | +        }  | 
 | 125 | +        const message = `Process failed with error: ${err.message}`;  | 
 | 126 | +        reject(new CommandError(message, stdout, stderr, null));  | 
 | 127 | +      });  | 
 | 128 | +    });  | 
 | 129 | +  },  | 
 | 130 | +};  | 
0 commit comments