# cross-spawn-extra

> a async version for cross-spawn and make it return like as sync return

Latest version **3.0.3** (published 2024-03-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install cross-spawn-extra
pnpm add cross-spawn-extra
yarn add cross-spawn-extra
bun add cross-spawn-extra
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2024-03-09 |
| First published | 2018-09-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 123.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | spawn, spawnSync, windows, cross-platform, path-ext, shebang, cmd, execute, create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/cross-spawn-extra
- Repository: https://github.com/bluelovers/cross-spawn-extra
- Homepage: https://github.com/bluelovers/cross-spawn-extra#readme
- Issues: https://github.com/bluelovers/cross-spawn-extra/issues
- npm.io page: https://npm.io/package/cross-spawn-extra

## Dependencies (4)

- [bluebird](https://npm.io/package/bluebird.md) ^3
- [strip-ansi](https://npm.io/package/strip-ansi.md) <7 >=6
- [cross-spawn](https://npm.io/package/cross-spawn.md) ^7.0.3
- [callable-instance2](https://npm.io/package/callable-instance2.md) ^2.0.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 3.0.3 (latest) — 2024-03-09
- 3.0.2 — 2024-03-09
- 3.0.1 — 2024-03-09
- 2.1.0 — 2020-06-10
- 2.0.0 — 2019-12-14
- 1.0.6 — 2019-05-19
- 1.0.5 — 2019-02-26
- 1.0.4 — 2019-01-16
- 1.0.3 — 2018-10-12
- 1.0.1 — 2018-09-25
- 1.0.0 — 2018-09-25

## README

# README

    a async version for cross-spawn and make it return like as sync return

```
npm install cross-spawn-extra cross-spawn
```

## demo

* [Core API](core.d.ts)
* [Options](type.d.ts)
* 
```ts
export interface SpawnOptions
{
	cwd?: string;
	env?: any;
	stdio?: 'inherit' | 'ignore' | 'pipe' | any | Array<'inherit' | 'ignore' | 'pipe' | any>;
	detached?: boolean;
	uid?: number;
	gid?: number;
	shell?: boolean | string;
	windowsVerbatimArguments?: boolean;
	windowsHide?: boolean;

	/**
	 * Strip ANSI escape codes
	 */
	stripAnsi?: boolean,
}

export interface SpawnSyncOptions {
	cwd?: string;
	input?: string | Buffer;
	stdio?: 'inherit' | 'ignore' | 'pipe' | any | Array<'inherit' | 'ignore' | 'pipe' | any>;
	env?: any;
	uid?: number;
	gid?: number;
	timeout?: number;
	killSignal?: string;
	maxBuffer?: number;
	encoding?: string;
	shell?: boolean | string;
	windowsHide?: boolean;
	windowsVerbatimArguments?: boolean;

	/**
	 * Strip ANSI escape codes
	 */
	stripAnsi?: boolean,
}
```

```ts
import crossSpawn = require('cross-spawn-extra');
import crossSpawn from 'cross-spawn-extra';
import { async as crossSpawnAsync, sync as crossSpawnSync } from 'cross-spawn-extra';
```

```ts
import { CrossSpawn } = require('cross-spawn-extra/core');
import CrossSpawn from 'cross-spawn-extra';
import { CrossSpawn } from 'cross-spawn-extra';

//----------

const crossSpawn = new CrossSpawn(require('cross-spawn'));
const crossSpawn = CrossSpawn.use(require('cross-spawn'));
```

```ts
let bin = './bin/log0001';

let cp = crossSpawn('node', [
	bin,
], {
	cwd: __dirname,
	
	/**
	 * Strip ANSI escape codes
	 */
	stripAnsi: true,
})
	.then(function (child)
	{
		return log(child);
	})
	.catch(function (err)
	{
		let child = err.child;

		return log(child);
	})
;

function log(child: SpawnASyncReturns)
{
	let { stdout, stderr, output, _output, status, signal, pid } = child;
	
	// can still via stream, but it already close
	let { stderrStream, stdoutStream } = child;

	console.log({
		pid,
		error: !!child.error,
		status,
		stdout: stdout.toString(),
		stderr: stderr.toString(),
		_output: Buffer.concat(_output).toString(),
	});

	return child;
}
```

> if typescript fail when use `crossSpawn` , try use `crossSpawn.async`

```ts
let bin = './bin/log0001';

let cp = crossSpawn.async('node', [
	bin,
], {
	cwd: __dirname,
})
	.then(function (child)
	{
		return log(child);
	})
	.catch(function (err)
	{
		let child = err.child;

		return log(child);
	})
;
```

`stdout` only show stdout  
`stderr` only show stderr

but `_output` can show real output order

```json5
{ pid: 54268,
  error: false,
  status: 0,
  stdout: 'log 0\nlog 2\nlog 4\ndebug 5\nlog 6\ninfo 7\nlog 8\n',
  stderr: 'error 1\nwarn 3\n',
  _output:
   'log 0\nerror 1\nlog 2\nwarn 3\nlog 4\ndebug 5\nlog 6\ninfo 7\nlog 8\n' }
```

---
_Source: https://npm.io/package/cross-spawn-extra · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
