Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

cross-spawn-extra

bluelovers6kISC3.0.3TypeScript support: included

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

spawn, spawnSync, windows, cross-platform, path-ext, shebang, cmd, execute, create-by-yarn-tool, create-by-tsdx

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
  • Options
  • `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';
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'));
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

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

{ 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' }

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

3.0.2 (2024-03-09)

🐛 Bug Fixes

  • types: fix dts with child_process (26e18d1)

3.0.1 (2024-03-09)

BREAKING CHANGES

  • tsdx
  • tsdx

✨ Features

📦 Code Refactoring

🚨 Tests

🛠 Build System

♻️ Chores

🔖 Miscellaneous