execa

人类的进程执行。(Process execution for humans)

Github星跟蹤圖

execa

人类的进程执行。

为什么

该软件包通过以下方式改进了 child_process 方法:

  • Promise 接口。
  • 从输出中删除最后的换行符,这样您就不必执行 stdout.trim() 。
  • 支持跨平台的 shebang 二进制文件。
  • 改进的 Windows 支持。
  • 更高的最大缓冲区。 100 MB,而不是 200 KB。
  • 按名称执行本地安装的二进制文件。
  • 在父进程终止时清除派生的进程。
  • 从 stdout 和 stderr 获取交错输出,类似于在终端上打印的输出。 (仅异步)
  • 可以将文件和参数指定为没有 shell 的单一字符串
  • 更多描述性错误。

安装

$ npm install execa

用法

const execa = require('execa');
(async () => {
    const {stdout} = await execa('echo', ['unicorns']);
    console.log(stdout);
    //=> 'unicorns'
})();

通过管道将子进程 stdout 传输到父进程

const execa = require('execa');
execa('echo', ['unicorns']).stdout.pipe(process.stdout);

错误处理

const execa = require('execa');
(async () => {
    // Catching an error
    try {
        await execa('unknown', ['command']);
    } catch (error) {
        console.log(error);
        /*
        {
            message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
            errno: -2,
            code: 'ENOENT',
            syscall: 'spawn unknown',
            path: 'unknown',
            spawnargs: ['command'],
            originalMessage: 'spawn unknown ENOENT',
            shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
            command: 'unknown command',
            stdout: '',
            stderr: '',
            all: '',
            failed: true,
            timedOut: false,
            isCanceled: false,
            killed: false
        }
        */
    }
})();

取消派生的进程

const execa = require('execa');
(async () => {
    const subprocess = execa('node');
    setTimeout(() => {
        subprocess.cancel();
    }, 1000);
    try {
        await subprocess;
    } catch (error) {
        console.log(subprocess.killed); // true
        console.log(error.isCanceled); // true
    }
})()

用同步方法捕获错误

try {
    execa.sync('unknown', ['command']);
} catch (error) {
    console.log(error);
    /*
    {
        message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
        errno: -2,
        code: 'ENOENT',
        syscall: 'spawnSync unknown',
        path: 'unknown',
        spawnargs: ['command'],
        originalMessage: 'spawnSync unknown ENOENT',
        shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
        command: 'unknown command',
        stdout: '',
        stderr: '',
        all: '',
        failed: true,
        timedOut: false,
        isCanceled: false,
        killed: false
    }
    */
}

杀死进程

使用 SIGTERM,并在 2 秒钟后用 SIGKILL 杀死它。

const subprocess = execa('node');
setTimeout(() => {
    subprocess.kill('SIGTERM', {
        forceKillAfterTimeout: 2000
    });
}, 1000);

API

请参考自述文件。


(The second edition revised by vz on 2020.08.02)

主要指標

概覽
名稱與所有者sindresorhus/execa
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2015-12-05 22:57:03
推送於2025-04-04 19:29:48
最后一次提交
發布數61
最新版本名稱v9.5.2 (發布於 2024-12-07 23:12:04)
第一版名稱v0.1.0 (發布於 2015-12-06 00:03:23)
用户参与
星數7.1k
關注者數41
派生數236
提交數806
已啟用問題?
問題數488
打開的問題數14
拉請求數655
打開的拉請求數0
關閉的拉請求數50
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Build Status Coverage Status

Process execution for humans

Why

This package improves child_process methods with:

Install

$ npm install execa

Usage

const execa = require('execa');

(async () => {
	const {stdout} = await execa('echo', ['unicorns']);
	console.log(stdout);
	//=> 'unicorns'
})();

Pipe the child process stdout to the parent

const execa = require('execa');

execa('echo', ['unicorns']).stdout.pipe(process.stdout);

Handling Errors

const execa = require('execa');

(async () => {
	// Catching an error
	try {
		await execa('unknown', ['command']);
	} catch (error) {
		console.log(error);
		/*
		{
			message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
			errno: -2,
			code: 'ENOENT',
			syscall: 'spawn unknown',
			path: 'unknown',
			spawnargs: ['command'],
			originalMessage: 'spawn unknown ENOENT',
			shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
			command: 'unknown command',
			stdout: '',
			stderr: '',
			all: '',
			failed: true,
			timedOut: false,
			isCanceled: false,
			killed: false
		}
		*/
	}

})();

Cancelling a spawned process

const execa = require('execa');

(async () => {
	const subprocess = execa('node');

	setTimeout(() => {
		subprocess.cancel();
	}, 1000);

	try {
		await subprocess;
	} catch (error) {
		console.log(subprocess.killed); // true
		console.log(error.isCanceled); // true
	}
})()

Catching an error with the sync method

try {
	execa.sync('unknown', ['command']);
} catch (error) {
	console.log(error);
	/*
	{
		message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
		errno: -2,
		code: 'ENOENT',
		syscall: 'spawnSync unknown',
		path: 'unknown',
		spawnargs: ['command'],
		originalMessage: 'spawnSync unknown ENOENT',
		shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
		command: 'unknown command',
		stdout: '',
		stderr: '',
		all: '',
		failed: true,
		timedOut: false,
		isCanceled: false,
		killed: false
	}
	*/
}

Kill a process

Using SIGTERM, and after 2 seconds, kill it with SIGKILL.

const subprocess = execa('node');

setTimeout(() => {
	subprocess.kill('SIGTERM', {
		forceKillAfterTimeout: 2000
	});
}, 1000);

API

execa(file, arguments, options?)

Execute a file. Think of this as a mix of child_process.execFile() and child_process.spawn().

No escaping/quoting is needed.

Unless the shell option is used, no shell interpreter (Bash, cmd.exe, etc.) is used, so shell features such as variables substitution (echo $PATH) are not allowed.

Returns a child_process instance which:

  • is also a Promise resolving or rejecting with a childProcessResult.
  • exposes the following additional methods and properties.

kill(signal?, options?)

Same as the original child_process#kill() except: if signal is SIGTERM (the default value) and the child process is not terminated after 5 seconds, force it by sending SIGKILL.

options.forceKillAfterTimeout

Type: `number