# package-run

> Node API for running package.json scripts. Supports yarn, npm, and pnpm.

Latest version **3.0.1** (published 2023-01-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install package-run
pnpm add package-run
yarn add package-run
bun add package-run
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2023-01-08 |
| First published | 2020-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 4 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Connor White |
| Maintainers | bconnorwhite |
| Keywords | package, pkg, run, yarn, npm, pnpm, manager, npx, bin, node, api |

## Links

- npm: https://www.npmjs.com/package/package-run
- Repository: https://github.com/bconnorwhite/package-run
- npm.io page: https://npm.io/package/package-run

## Dependencies (4)

- [has-script](https://npm.io/package/has-script.md) ^2.0.0
- [as-typed-array](https://npm.io/package/as-typed-array.md) ^2.0.0
- [which-pm-lockfile](https://npm.io/package/which-pm-lockfile.md) ^2.0.1
- [@bconnorwhite/exec](https://npm.io/package/@bconnorwhite/exec.md) ^6.0.12

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2023-01-08
- 3.0.0 — 2023-01-06
- 2.0.6 — 2021-04-17
- 2.0.5 — 2021-04-17
- 2.0.4 — 2021-04-17
- 2.0.3 — 2020-09-26
- 2.0.2 — 2020-09-21
- 2.0.1 — 2020-09-21
- 2.0.0 — 2020-09-21
- 1.4.1 — 2020-09-06
- 1.4.0 — 2020-09-06
- 1.3.2 — 2020-09-06
- 1.3.1 — 2020-09-06
- 1.3.0 — 2020-09-06
- 1.2.4 — 2020-09-06
- … 15 more at https://npm.io/package/package-run/versions

## README

<!--BEGIN HEADER-->
<div id="top" align="center">
  <h1>package-run</h1>
  <a href="https://npmjs.com/package/package-run">
    <img alt="npm" src="https://img.shields.io/npm/v/package-run.svg">
  </a>
  <a href="https://github.com/bconnorwhite/package-run">
    <img alt="typescript" src="https://img.shields.io/github/languages/top/bconnorwhite/package-run.svg">
  </a>
  <a href="https://coveralls.io/github/bconnorwhite/package-run?branch=master">
    <img alt="Coveralls Status" src="https://img.shields.io/coveralls/github/bconnorwhite/package-run.svg?branch=master">
  </a>
</div>

<br />

<blockquote align="center">Node API for running package.json scripts. Supports yarn, npm, and pnpm.</blockquote>

<br />

_If I should maintain this repo, please ⭐️_
<a href="https://github.com/bconnorwhite/package-run">
  <img align="right" alt="GitHub stars" src="https://img.shields.io/github/stars/bconnorwhite/package-run?label=%E2%AD%90%EF%B8%8F&style=social">
</a>

_DM me on [Twitter](https://twitter.com/bconnorwhite) if you have questions or suggestions._
<a href="https://twitter.com/bconnorwhite">
  <img align="right" alt="Twitter Follow" src="https://img.shields.io/twitter/url?label=%40bconnorwhite&style=social&url=https%3A%2F%2Ftwitter.com%2Fbconnorwhite">
</a>

---
<!--END HEADER-->

Scripts will automatically run with the correct package manager (yarn, npm, or pnpm) if a lockfile is present.

For npm, if a the given script exists in packge.json it will be run using `npm run`.  
Otherwise, `npx --no-install` will be used.

## Installation

```sh
yarn add package-run
```

```sh
npm install package-run
```

```sh
pnpm install package-run
```

## Usage

```ts
import run, { executableToString } from "package-run";

run({
  command: "babel",
  args: [
    "./src",
    {
      "out-dir": "./build",
      "config-file": "./babel.config.json",
      "watch": true
    }
  ]
}, {
  slient: false // If true, will not output from yarn/npm/npx.
});

// Equivalent of:
// yarn run babel ./src --out-dir ./build --config-file ./babel.config.json --watch
// npx --no-install babel ./src --out-dir ./build --config-file ./babel.config.json --watch
// pnpm run babel ./src --out-dir ./build --config-file ./babel.config.json --watch

executableToString({
  command: "babel",
  args: [
    "./src"
    {
      "out-dir": "./build",
      "config-file": "./babel.config.json",
      "watch": true
    }
  ]
});

// "yarn run babel ./src --out-dir ./build --config-file ./babel.config.json --watch"
```

### Types

```ts
function run(executable: Executable, options?: RunOptions): Promise<ExecResult>;

function getString(executable: Executable, options?: RunOptions): Promise<string>;

type Executable = {
  command: string;
  args?: Args;
  cwd?: string;
  env?: NodeJS.ProcessEnv;
  silent?: boolean;
}

type Args = Arg | Arg[];

type Arg = string | Flags;

type Flags = {
  [flag: string]: string | number | boolean  | string[] | undefined;
}

type ExecResult = {
  output: string;
  error: string;
  textOutput: string; // output stripped on ANSI colors
  textError: string; // error stripped on ANSI colors
  jsonOutput: () => JSONObject | JSONArray | undefined; // First JSON object or array in output
  jsonError: () => JSONObject | JSONArray | undefined; // First JSON object or array in error
}

type RunOptions = {
  silent?: boolean | undefined;
}
```

<!--BEGIN FOOTER-->

<br />

<h2 id="dependencies">Dependencies<a href="https://www.npmjs.com/package/package-run?activeTab=dependencies"><img align="right" alt="dependencies" src="https://img.shields.io/librariesio/release/npm/package-run.svg"></a></h2>

- [@bconnorwhite/exec](https://www.npmjs.com/package/@bconnorwhite/exec): Execute commands while keeping flags easily configurable as an object
- [as-typed-array](https://www.npmjs.com/package/as-typed-array): Make any value an array
- [has-script](https://www.npmjs.com/package/has-script): Check if package.json contains a script
- [which-pm-lockfile](https://www.npmjs.com/package/which-pm-lockfile): Check if a project uses yarn, npm, or pnpm


<br />

<h3>Dev Dependencies</h3>

- [@types/mock-fs](https://www.npmjs.com/package/@types/mock-fs): TypeScript definitions for mock-fs
- [autorepo](https://www.npmjs.com/package/autorepo): Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.
- [mock-fs](https://www.npmjs.com/package/mock-fs): A configurable mock file system.  You know, for testing.


<br />

<h2 id="license">License <a href="https://opensource.org/licenses/MIT"><img align="right" alt="license" src="https://img.shields.io/npm/l/package-run.svg"></a></h2>

[MIT](https://opensource.org/licenses/MIT) - _The MIT License_
<!--END FOOTER-->

---
_Source: https://npm.io/package/package-run · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
