CodeSampleX

サンプル

cross-spawn 7.0.6: execute child process with cross-spawn and cross-spawn.sync

検証済みサンプル — npm cross-spawn 7.0.6: execute child process with cross-spawn and cross-spawn.sync. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:c92afed6c986dbf6e1d569c08eeaed85fa755d53fe3f8e0ec3cf5981e9aa5dd7

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-26

ケース

HOW
ゴール
execute child process with cross-spawn and cross-spawn.sync
パッケージ
シンボル
  • spawn
  • sync
作成日
2026-08-26T08:56:04Z

コントラクト

  1. spawn.sync executes command and captures stdout and exit status synchronously
  2. spawn.sync returns non-zero status when child process exits with non-zero code
  3. spawn.sync returns ENOENT error in result when command executable does not exist
  4. spawn.sync passes input option to child process stdin
  5. spawn executes command asynchronously and streams stdout data
  6. spawn emits error event with ENOENT when command executable does not exist

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: execute child process with cross-spawn and cross-spawn.sync
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/cross-spawn@7.0.6
Demonstrate these symbols/APIs:
  - spawn
  - sync

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:fb9249f10bd2ae935b0cd742defa5da9606c8686f0dcac64d75b9bbc96f509c8","contract":["spawn.sync executes command and captures stdout and exit status synchronously","spawn.sync returns non-zero status when child process exits with non-zero code","spawn.sync returns ENOENT error in result when command executable does not exist","spawn.sync passes input option to child process stdin","spawn executes command asynchronously and streams stdout data","spawn emits error event with ENOENT when command executable does not exist"],"goal":"execute child process with cross-spawn and cross-spawn.sync","kind":"HOW","packages":["pkg:npm/cross-spawn@7.0.6"],"schemaVersion":1,"symbols":["spawn","sync"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/cross-spawn@7.0.6"],"schemaVersion":1,"subject":"pkg:npm/cross-spawn@7.0.6","symbols":["spawn","sync"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-cross-spawn",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-cross-spawn",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "cross-spawn": "7.0.6"
      }
    },
    "node_modules/cross-spawn": {
      "version": "7.0.6",
      "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
      "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
      "license": "MIT",
      "dependencies": {
        "path-key": "^3.1.0",
        "shebang-command": "^2.0.0",
        "which": "^2.0.1"
      },
      "engines": {
        "node": ">= 8"
      }
    },
    "node_modules/isexe": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
      "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
      "license": "ISC"
    },
    "node_modules/path-key": {
      "version": "3.1.1",
      "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
      "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/shebang-command": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
      "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
      "license": "MIT",
      "dependencies": {
        "shebang-regex": "^3.0.0"
      },
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/shebang-regex": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
      "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/which": {
      "version": "2.0.2",
      "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
      "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
      "license": "ISC",
      "dependencies": {
        "isexe": "^2.0.0"
      },
      "bin": {
        "node-which": "bin/node-which"
      },
      "engines": {
        "node": ">= 8"
      }
    }
  }
}
package.json
{
  "name": "sample-cross-spawn",
  "version": "1.0.0",
  "description": "execute child process with cross-spawn and cross-spawn.sync",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "cross-spawn": "7.0.6"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "execute child process with cross-spawn and cross-spawn.sync",
  "kind": "HOW",
  "packages": [
    "pkg:npm/cross-spawn@7.0.6"
  ],
  "symbols": [
    "spawn",
    "sync"
  ]
}
src/index.js
'use strict';

const spawn = require('cross-spawn');

function runSync(command, args = [], options = {}) {
  return spawn.sync(command, args, options);
}

function runAsync(command, args = [], options = {}) {
  return spawn(command, args, options);
}

function runCommandCapture(command, args = [], options = {}) {
  const result = spawn.sync(command, args, {
    encoding: 'utf8',
    ...options,
  });
  return {
    status: result.status,
    stdout: result.stdout,
    stderr: result.stderr,
    error: result.error,
  };
}

module.exports = {
  runSync,
  runAsync,
  runCommandCapture,
  spawn,
};
test/contract.js
'use strict';

const assert = require('node:assert');
const spawn = require('cross-spawn');
const { runSync, runAsync, runCommandCapture } = require('../src/index.js');

async function testSyncExecution() {
  // 1. spawn.sync executes command and captures stdout and exit status synchronously
  const res = spawn.sync('node', ['-e', 'process.stdout.write("spawn-sync-ok")'], { encoding: 'utf8' });
  assert.strictEqual(res.status, 0, 'status should be 0');
  assert.strictEqual(res.stdout, 'spawn-sync-ok', 'stdout should match');
  assert.ok(!res.error, 'error should not be present on success');

  const helperRes = runCommandCapture('node', ['-e', 'process.stdout.write("helper-ok")']);
  assert.strictEqual(helperRes.status, 0);
  assert.strictEqual(helperRes.stdout, 'helper-ok');
}

async function testNonZeroExit() {
  // 2. spawn.sync returns non-zero status when child process exits with non-zero code
  const res = spawn.sync('node', ['-e', 'process.exit(5)']);
  assert.strictEqual(res.status, 5, 'exit status should be 5');

  const helperRes = runCommandCapture('node', ['-e', 'process.exit(12)']);
  assert.strictEqual(helperRes.status, 12, 'helper status should be 12');
}

async function testMissingCommandSync() {
  // 3. spawn.sync returns ENOENT error in result when command executable does not exist
  const res = spawn.sync('nonexistent_command_xyz_12345');
  assert.ok(res.error, 'error property should exist');
  assert.strictEqual(res.error.code, 'ENOENT', 'error code should be ENOENT');
}

async function testSyncInput() {
  // 4. spawn.sync passes input option to child process stdin
  const res = spawn.sync('node', ['-e', 'process.stdin.pipe(process.stdout)'], {
    input: 'hello from stdin',
    encoding: 'utf8'
  });
  assert.strictEqual(res.status, 0);
  assert.strictEqual(res.stdout, 'hello from stdin');
}

function testAsyncExecution() {
  // 5. spawn executes command asynchronously and streams stdout data
  return new Promise((resolve, reject) => {
    const child = spawn('node', ['-e', 'process.stdout.write("async-stream-data")']);
    let stdout = '';
    child.stdout.on('data', (chunk) => {
      stdout += chunk;
    });
    child.on('error', reject);
    child.on('close', (code) => {
      try {
        assert.strictEqual(code, 0, 'child process should exit with code 0');
        assert.strictEqual(stdout, 'async-stream-data', 'stdout should match');
        resolve();
      } catch (err) {
        reject(err);
      }
    });
  });
}

function testAsyncMissingCommand() {
  // 6. spawn emits error event with ENOENT when command executable does not exist
  return new Promise((resolve, reject) => {
    const child = spawn('nonexistent_command_xyz_12345');
    child.on('error', (err) => {
      try {
        assert.strictEqual(err.code, 'ENOENT', 'async spawn should emit ENOENT error');
        resolve();
      } catch (e) {
        reject(e);
      }
    });
    child.on('close', () => {
      resolve();
    });
  });
}

async function main() {
  await testSyncExecution();
  await testNonZeroExit();
  await testMissingCommandSync();
  await testSyncInput();
  await testAsyncExecution();
  await testAsyncMissingCommand();
  console.log('Contract tests passed successfully.');
}

main().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

オリジンシーダー

匿名