Wondering what’s next for npm?Check out our public roadmap! »

    package-run

    2.0.6 • Public • Published

    package-run

    NPM TypeScript Coverage Status GitHub Stars Twitter Follow

    Node API for running package.json scripts.

    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

    yarn add package-run
    npm install package-run

    API

    Usage

    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

    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;
    }

    Dependenciesdependencies


    Dev DependenciesDavid


    License license

    MIT

    Install

    npm i package-run

    DownloadsWeekly Downloads

    18

    Version

    2.0.6

    License

    MIT

    Unpacked Size

    12.7 kB

    Total Files

    7

    Last publish

    Collaborators

    • avatar