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