execa

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

Github stars Tracking Chart

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)

Main metrics

Overview
Name With Ownersindresorhus/execa
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2015-12-05 22:57:03
Pushed At2025-04-04 19:29:48
Last Commit At
Release Count61
Last Release Namev9.5.2 (Posted on 2024-12-07 23:12:04)
First Release Namev0.1.0 (Posted on 2015-12-06 00:03:23)
用户参与
Stargazers Count7.1k
Watchers Count41
Fork Count236
Commits Count806
Has Issues Enabled
Issues Count488
Issue Open Count14
Pull Requests Count655
Pull Requests Open Count0
Pull Requests Close Count50
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

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