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. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.

sha256:c92afed6c986dbf6e1d569c08eeaed85fa755d53fe3f8e0ec3cf5981e9aa5dd7

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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);
});

原始种子者

匿名