GitHub - carlos-menezes/target-run: Platform-aware script runner for Node.js projects

1 min read Original article ↗

NPM Version NPM Downloads NPM License

Platform-aware script runner for Node.js projects.

Set a script body to target-run, then define platform/arch variants:

{
  "scripts": {
    "test": "target-run",
    "test:darwin:arm64": "jest --config jest.apple-silicon.config.ts",
    "test:linux:x64": "jest --config jest.linux.config.ts",
    "test:default": "jest"
  }
}

Candidates are resolved in order:

  1. <script>:<platform>:<arch>
  2. <script>:<platform>
  3. <script>:<arch>
  4. <script>:default

Platform values come from os.platform() and arch from os.arch().

Example

Given this package.json on a Linux x64 machine:

{
  "scripts": {
    "build": "target-run",
    "build:linux:x64": "node dist/index-linux-x64.js",
    "build:darwin:arm64": "node dist/index-darwin-arm64.js",
    "build:default": "node dist/index.js"
  }
}
$ pnpm build
# linux/x64   → node dist/index-linux-x64.js
# darwin/arm64 → node dist/index-darwin-arm64.js
# win32/x64   → node dist/index.js  (fallback)

CLI options

Flag Description
--dry-run Print the resolved key without executing
--verbose Print platform, arch, resolved key and runner
--optional Exit 0 silently when no match is found
--required Exit 1 when no match is found
--script <name> Override the base script name1
--cwd <path> Set the working directory for package.json lookup
  1. Useful when invoking target-run outside of an npm lifecycle, e.g. target-run --script build in a shell script or CI step where npm_lifecycle_event is not set.