How to use cross-spawn - 10 common examples

To help you get started, we’ve selected a few cross-spawn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kentcdodds / kcd-scripts / src / scripts / __tests__ / travis-after-success.js View on Github external
})
    const originalLog = console.log
    const originalExit = process.exit
    process.exit = jest.fn()
    console.log = jest.fn()

    // tests
    if (version) {
      utils.pkg.version = version
    }
    utils.hasFile = () => hasCoverageDir
    process.env.SKIP_CODECOV = isOptedOutOfCoverage
    require('../travis-after-success')

    expect(console.log.mock.calls).toMatchSnapshot()
    const commands = crossSpawnSyncMock.mock.calls.map(
      call => `${call[0]} ${call[1].join(' ')}`,
    )
    expect(commands).toMatchSnapshot()

    // afterEach
    process.exit = originalExit
    console.log = originalLog
    Object.keys(originalEnvs).forEach(envKey => {
      process.env[envKey] = env[envKey]
    })
    jest.resetModules()
  },
  {
github kentcdodds / kcd-scripts / src / __tests__ / index.js View on Github external
// tests
      process.argv = ['node', '../', ...args]
      crossSpawnSyncMock.mockClear()
      if (signal) {
        crossSpawnSyncMock.mockReturnValueOnce({result: 1, signal})
      }
      require('../')
      if (snapshotLog) {
        expect(console.log.mock.calls).toMatchSnapshot()
      } else if (signal) {
        expect(process.exit).toHaveBeenCalledTimes(1)
        expect(process.exit).toHaveBeenCalledWith(1)
        expect(console.log.mock.calls).toMatchSnapshot()
      } else {
        expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
        const [firstCall] = crossSpawnSyncMock.mock.calls
        const [script, calledArgs] = firstCall
        expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
      }
    } catch (error) {
      if (throws) {
        expect(error.message).toMatchSnapshot()
      } else {
        throw error
      }
    } finally {
      // afterEach
      process.exit = originalExit
      process.argv = originalArgv
      console.log = originalLog
      jest.resetModules()
    }
github kentcdodds / kcd-scripts / src / __tests__ / index.js View on Github external
({snapshotLog = false, throws = false, signal = false, args = []}) => {
    // beforeEach
    const {sync: crossSpawnSyncMock} = require('cross-spawn')
    const originalExit = process.exit
    const originalArgv = process.argv
    const originalLog = console.log
    process.exit = jest.fn()
    console.log = jest.fn()
    try {
      // tests
      process.argv = ['node', '../', ...args]
      crossSpawnSyncMock.mockClear()
      if (signal) {
        crossSpawnSyncMock.mockReturnValueOnce({result: 1, signal})
      }
      require('../')
      if (snapshotLog) {
        expect(console.log.mock.calls).toMatchSnapshot()
      } else if (signal) {
        expect(process.exit).toHaveBeenCalledTimes(1)
        expect(process.exit).toHaveBeenCalledWith(1)
        expect(console.log.mock.calls).toMatchSnapshot()
      } else {
        expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
        const [firstCall] = crossSpawnSyncMock.mock.calls
        const [script, calledArgs] = firstCall
        expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
      }
github kentcdodds / kcd-scripts / src / __tests__ / index.js View on Github external
({snapshotLog = false, throws = false, signal = false, args = []}) => {
    // beforeEach
    const {sync: crossSpawnSyncMock} = require('cross-spawn')
    const originalExit = process.exit
    const originalArgv = process.argv
    const originalLog = console.log
    process.exit = jest.fn()
    console.log = jest.fn()
    try {
      // tests
      process.argv = ['node', '../', ...args]
      crossSpawnSyncMock.mockClear()
      if (signal) {
        crossSpawnSyncMock.mockReturnValueOnce({result: 1, signal})
      }
      require('../')
      if (snapshotLog) {
        expect(console.log.mock.calls).toMatchSnapshot()
      } else if (signal) {
        expect(process.exit).toHaveBeenCalledTimes(1)
        expect(process.exit).toHaveBeenCalledWith(1)
        expect(console.log.mock.calls).toMatchSnapshot()
      } else {
        expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
        const [firstCall] = crossSpawnSyncMock.mock.calls
        const [script, calledArgs] = firstCall
        expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
      }
    } catch (error) {
      if (throws) {
github kentcdodds / kcd-scripts / src / scripts / __tests__ / lint.js View on Github external
const originalExit = process.exit
    Object.assign(utils, {
      hasPkgProp,
      hasFile,
      resolveBin: (modName, {executable = modName} = {}) => executable,
    })
    process.exit = jest.fn()
    const teardown = setup()

    process.argv = ['node', '../lint', ...args]

    try {
      // tests
      require('../lint')
      expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
      const [firstCall] = crossSpawnSyncMock.mock.calls
      const [script, calledArgs] = firstCall
      expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
    } catch (error) {
      throw error
    } finally {
      teardown()
      // afterEach
      process.exit = originalExit
      process.argv = originalArgv
      jest.resetModules()
    }
  },
  {
github kentcdodds / kcd-scripts / src / scripts / __tests__ / precommit.js View on Github external
const originalExit = process.exit
    Object.assign(utils, {
      hasPkgProp,
      hasFile,
      resolveBin: (modName, {executable = modName} = {}) => executable,
    })
    process.exit = jest.fn()

    process.argv = ['node', '../pre-commit', ...args]
    utils.isOptedIn = optIn => optIn === 'pre-commit'

    try {
      // tests
      require('../pre-commit')
      expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2)
      const [firstCall, secondCall] = crossSpawnSyncMock.mock.calls
      const [scriptOne, calledArgsOne] = firstCall
      expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot()
      const [scriptTwo, calledArgsTwo] = secondCall
      expect([scriptTwo, ...calledArgsTwo].join(' ')).toMatchSnapshot()
    } catch (error) {
      throw error
    } finally {
      // afterEach
      process.exit = originalExit
      process.argv = originalArgv
      jest.resetModules()
    }
  },
  {
github kentcdodds / kcd-scripts / src / scripts / __tests__ / validate.js View on Github external
({setup = () => () => {}}) => {
    // beforeEach
    const {sync: crossSpawnSyncMock} = require('cross-spawn')
    const originalExit = process.exit
    process.exit = jest.fn()
    process.env['SCRIPTS_PRE-COMMIT'] = 'false'
    const teardown = setup()

    try {
      // tests
      require('../validate')
      expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
      const [firstCall] = crossSpawnSyncMock.mock.calls
      const [script, calledArgs] = firstCall
      expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
    } catch (error) {
      throw error
    } finally {
      teardown()
    }

    // afterEach
    process.exit = originalExit
    jest.resetModules()
  },
  {
github kentcdodds / kcd-scripts / src / scripts / __tests__ / format.js View on Github external
({args}) => {
    // beforeEach
    const {sync: crossSpawnSyncMock} = require('cross-spawn')
    const originalExit = process.exit
    const originalArgv = process.argv
    const utils = require('../../utils')
    utils.resolveBin = (modName, {executable = modName} = {}) => executable
    process.exit = jest.fn()

    // tests
    process.argv = ['node', '../format', ...args]
    require('../format')
    expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
    const [firstCall] = crossSpawnSyncMock.mock.calls
    const [script, calledArgs] = firstCall
    expect([script, ...calledArgs].join(' ')).toMatchSnapshot()

    // afterEach
    process.exit = originalExit
    process.argv = originalArgv
    jest.resetModules()
  },
  {
github codejamninja / reactant / packages / context / src / state.ts View on Github external
// eslint-disable-next-line import/no-dynamic-require,global-require
        require.resolve('find-process', {
          paths: [
            path.resolve(pkgDir.sync(__dirname) || __dirname, 'node_modules'),
            path.resolve(rootPath, 'node_modules')
          ]
        })
      ) || path.resolve(rootPath, 'node_modules');
    const bin = path.resolve(
      pkgPath,
      // eslint-disable-next-line import/no-dynamic-require,global-require
      require(path.resolve(pkgPath, 'package.json')).bin['find-process']
    );
    return !/No process found/.test(
      (
        crossSpawn.sync('node', [bin, pid.toString()], {
          stdio: 'pipe'
          // eslint-disable-next-line no-undef
        })?.stdout || ''
      ).toString()
    );
  }
github tricinel / frontwerk / src / scripts / prettier.js View on Github external
const path = require('path');
const spawn = require('cross-spawn');

const { warning, info } = require('../utils/logger');

const [executor, script, ...args] = process.argv; // eslint-disable-line no-unused-vars
const scriptPath = require.resolve(path.join(__dirname, 'format'));

// Show deprecation warning
warning('This command will be deprecated in a future version of frontwerk!');
info(
  'Please use frontwerk format instead.',
  'You can pass the same options and it will work in the same way.'
);

const result = spawn.sync(executor, [scriptPath, ...args], {
  stdio: 'inherit'
});

process.exit(result.status);