Document and test Windows fixes (#1248)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent 0a7b4f8 · Browse files · 3 files·+41 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−1 removed
- tests:+18 added−1 removed
- other:+23 added−0 removed
Provenance
- Commit
4b0edb3- Tree
87d2579- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0a7b4f8
3 files changed+41 added−1 removed
modified:docs/shell.md+10 added
13 unmodified lines14151617181920212223242526272829303119 unmodified lines13 unmodified lines- Unsafe, potentially allowing [command injection](https://en.wikipedia.org/wiki/Code_injection#Shell_injection) (see the [escaping section](escaping.md#shells)).
In almost all cases, plain JavaScript is a better alternative to shells. The [following page](bash.md) shows how to convert Bash into JavaScript.
## When a shell is not needed
On Windows, a shell is often used to work around limitations of the OS. This is not needed with Execa. In particular, `shell: true` is not required to:- run `.cmd` and `.bat` files, such as `npm.cmd`- [resolve a command](windows.md#file-extensions) by name using `PATHEXT`- run [shebang](windows.md#shebang) scripts- [escape](windows.md#escaping) file paths and arguments
A shell is only needed to use shell-specific syntax, such as pipes `|`, redirections `>`, globbing `*` or environment variable expansion `$VAR`.
## Specific shell
```jsimport {execa} from 'execa';19 unmodified linesmodified:docs/windows.md+13 added
22 unmodified lines23242526272829303132333435363738394041424349 unmodified lines22 unmodified lines```
Although Windows does not natively support shebangs, Execa adds support for them.
## File extensions
On Windows, executables have a file extension, such as `.exe`, `.cmd`, `.bat` or `.com`. When the file is passed without any extension, Execa resolves it using the [`PATHEXT`](https://ss64.com/nt/path.html#pathext) environment variable, just like the OS does when looking up commands.
```jsimport {execa} from 'execa';
// Runs `npm.cmd`await execa`npm run build`;```
This means `.cmd` and `.bat` files can be run directly. Unlike Node.js, Execa does not require a [shell](shell.md) (nor a `cmd.exe /c` prefix) for this. Relative paths (such as `./folder/executable`) and files whose path contains spaces also work without a shell, just like on Unix. This is thanks to [`cross-spawn`](https://github.com/moxystudio/node-cross-spawn?tab=readme-ov-file#why).
## Signals
Only few [signals](termination.md#other-signals) work on Windows with Node.js: [`SIGTERM`](termination.md#sigterm), [`SIGKILL`](termination.md#sigkill), [`SIGINT`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGINT) and [`SIGQUIT`](termination.md#sigquit). Also, sending signals from other processes is [not supported](termination.md#signal-name-and-description). Finally, the [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option [is a noop](termination.md#forceful-termination) on Windows.
49 unmodified linesmodified:test/methods/main-async.js+18 added-1 removed
1234456787 unmodified lines161718192021222324252627282930313233343536373839403 unmodified linesimport process from 'node:process';import test from 'ava';import {execa} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';
setFixtureDirectory();
const isWindows = process.platform === 'win32';7 unmodified lines test('execa() - run cmd command', async t => { const {stdout} = await execa('cmd', ['/c', 'hello.cmd']); t.is(stdout, 'Hello World'); });
// A bare command name without an extension is resolved using `PATHEXT`. test('execa() - resolve command extension using PATHEXT', async t => { const {stdout} = await execa('hello'); t.is(stdout, 'Hello World'); });
test('execa() - run cmd file using a relative path', async t => { const {stdout} = await execa('./hello.cmd', {cwd: FIXTURES_DIRECTORY}); t.is(stdout, 'Hello World'); });
// The fixture's filename contains a space and it starts with a shebang. test('execa() - run file with a space in its path and a shebang', async t => { const {stdout} = await execa('command with space.js', ['foo']); t.is(stdout, 'foo'); });}
test('execa() returns a promise with pid', async t => { const subprocess = execa('noop.js', ['foo']);3 unmodified lines