mirror of
				https://github.com/threeal/cmake-action.git
				synced 2025-11-04 05:43:42 +00:00 
			
		
		
		
	feat: log executed command
Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
This commit is contained in:
		
							parent
							
								
									c7ba931617
								
							
						
					
					
						commit
						7ad4029dc8
					
				
							
								
								
									
										11
									
								
								dist/action.mjs
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										11
									
								
								dist/action.mjs
									
									
									
										generated
									
									
										vendored
									
									
								
							@ -50,6 +50,16 @@ function logError(err) {
 | 
				
			|||||||
    const message = err instanceof Error ? err.message : String(err);
 | 
					    const message = err instanceof Error ? err.message : String(err);
 | 
				
			||||||
    process.stdout.write(`::error::${message}${os.EOL}`);
 | 
					    process.stdout.write(`::error::${message}${os.EOL}`);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Logs a command along with its arguments in GitHub Actions.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param command - The command to log.
 | 
				
			||||||
 | 
					 * @param args - The arguments of the command.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function logCommand(command, ...args) {
 | 
				
			||||||
 | 
					    const message = [command, ...args].join(" ");
 | 
				
			||||||
 | 
					    process.stdout.write(`[command]${message}${os.EOL}`);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Executes a command with the given arguments.
 | 
					 * Executes a command with the given arguments.
 | 
				
			||||||
@ -62,6 +72,7 @@ function logError(err) {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
async function exec(command, args) {
 | 
					async function exec(command, args) {
 | 
				
			||||||
    return new Promise((resolve, reject) => {
 | 
					    return new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
					        logCommand(command, ...args);
 | 
				
			||||||
        const proc = spawn(command, args, {
 | 
					        const proc = spawn(command, args, {
 | 
				
			||||||
            stdio: ["ignore", "inherit", "inherit"],
 | 
					            stdio: ["ignore", "inherit", "inherit"],
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
				
			|||||||
@ -1,13 +1,30 @@
 | 
				
			|||||||
import { exec } from "./exec.js";
 | 
					import { jest } from "@jest/globals";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe("execute commands", () => {
 | 
					describe("execute commands", () => {
 | 
				
			||||||
 | 
					  const logCommand = jest.fn<(command: string, ...args: string[]) => void>();
 | 
				
			||||||
 | 
					  jest.unstable_mockModule("gha-utils", () => ({ logCommand }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  beforeEach(() => {
 | 
				
			||||||
 | 
					    logCommand.mockClear();
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it("should successfully execute a command", async () => {
 | 
					  it("should successfully execute a command", async () => {
 | 
				
			||||||
 | 
					    const { exec } = await import("./exec.js");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await exec("node", ["--version"]);
 | 
					    await exec("node", ["--version"]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(logCommand).toHaveBeenCalledTimes(1);
 | 
				
			||||||
 | 
					    expect(logCommand).toHaveBeenCalledWith("node", "--version");
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it("should fail to execute a command", async () => {
 | 
					  it("should fail to execute a command", async () => {
 | 
				
			||||||
 | 
					    const { exec } = await import("./exec.js");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await expect(exec("node", ["--invalid"])).rejects.toThrow(
 | 
					    await expect(exec("node", ["--invalid"])).rejects.toThrow(
 | 
				
			||||||
      "Command exited with status code 9",
 | 
					      "Command exited with status code 9",
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(logCommand).toHaveBeenCalledTimes(1);
 | 
				
			||||||
 | 
					    expect(logCommand).toHaveBeenCalledWith("node", "--invalid");
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import { logCommand } from "gha-utils";
 | 
				
			||||||
import { spawn } from "node:child_process";
 | 
					import { spawn } from "node:child_process";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@ -11,6 +12,7 @@ import { spawn } from "node:child_process";
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
export async function exec(command: string, args: string[]): Promise<void> {
 | 
					export async function exec(command: string, args: string[]): Promise<void> {
 | 
				
			||||||
  return new Promise<void>((resolve, reject) => {
 | 
					  return new Promise<void>((resolve, reject) => {
 | 
				
			||||||
 | 
					    logCommand(command, ...args);
 | 
				
			||||||
    const proc = spawn(command, args, {
 | 
					    const proc = spawn(command, args, {
 | 
				
			||||||
      stdio: ["ignore", "inherit", "inherit"],
 | 
					      stdio: ["ignore", "inherit", "inherit"],
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user