Skip to content

Translate package manager install commands

License

Notifications You must be signed in to change notification settings

RostiMelk/packmorph

Repository files navigation

packmorph

Translate package manager commands between npm, pnpm, Yarn, and Bun.

npm version License: MIT

import { packmorph } from "packmorph";

// Install commands (default)
const result = packmorph("npm install -D typescript");
if (result.ok) {
  console.log(result.npm); // npm install -D typescript
  console.log(result.pnpm); // pnpm add -D typescript
  console.log(result.yarn); // yarn add --dev typescript
  console.log(result.bun); // bun add --dev typescript
}

Install

npm install packmorph

What it does

Converts package manager commands between npm, pnpm, Yarn, and Bun. Supports install, exec, run, and create commands.

Command Types

Install (default)

packmorph("npm install react");
packmorph("pnpm add -D vitest");
packmorph("yarn global add eslint");
packmorph("bun add --exact lodash");

Exec (opt-in)

packmorph("npx prettier .", { parseExec: true });
// → npm: npx prettier .
// → pnpm: pnpm dlx prettier .
// → yarn: yarn dlx prettier .
// → bun: bunx prettier .

Run (opt-in)

packmorph("npm run dev", { parseRun: true });
// → npm: npm run dev
// → pnpm: pnpm run dev
// → yarn: yarn run dev
// → bun: bun run dev

Create (opt-in)

packmorph("npm create vite@latest my-app", { parseCreate: true });
// → npm: npm create vite@latest -- my-app
// → pnpm: pnpm create vite@latest my-app
// → yarn: yarn create vite@latest my-app
// → bun: bun create vite@latest my-app

Note: npm create requires -- before additional args. Packmorph handles this automatically.

Multi-line (opt-in)

const result = packmorph(
  `# Install dependencies
npm install react
npm install -D typescript
cd my-project
npx prettier --write .`,
  { parseMultiLine: true, parseExec: true },
);

if (result.ok) {
  console.log(result.npm);
  // # Install dependencies
  // npm install react
  // npm install -D typescript
  // cd my-project
  // npx prettier --write .

  console.log(result.pnpm);
  // # Install dependencies
  // pnpm add react
  // pnpm add -D typescript
  // cd my-project
  // pnpm dlx prettier --write .
}

Note: Multi-line mode automatically preserves comments (#), empty lines, and non-package-manager commands (like cd, mkdir, echo). Only package manager commands are transformed - everything else passes through unchanged.

Important: Multi-line parsing requires all package manager commands to use the same package manager. Mixing different package managers will return an error:

const result = packmorph(
  `npm install react
pnpm add typescript`,
  { parseMultiLine: true },
);
// → { ok: false, reason: "mixed-package-managers" }

This ensures consistent command conversion across all package managers.

Options

interface PackmorphOptions {
  parseInstall?: boolean; // default: true
  parseExec?: boolean; // default: false
  parseRun?: boolean; // default: false
  parseCreate?: boolean; // default: false
  parseMultiLine?: boolean; // default: false
}

Enable multiple command types:

const result = packmorph(command, {
  parseInstall: true,
  parseExec: true,
  parseRun: true,
  parseCreate: true,
  parseMultiLine: true,
});

Supported Commands

Install

  • npm install / npm i
  • pnpm add / pnpm install
  • yarn add / yarn install / yarn global add
  • bun add / bun install

Exec

  • npx <package>
  • pnpm dlx <package>
  • yarn dlx <package>
  • bunx <package>

Run

  • npm run <script>
  • pnpm run <script>
  • yarn run <script>
  • bun run <script>

Create

  • npm create <template>
  • pnpm create <template>
  • yarn create <template>
  • bun create <template>

Flags

Install commands support: -D (dev), -g (global), -E (exact), -O (optional), -P (peer), --frozen-lockfile.

Flags are automatically normalized:

  • yarn add --devnpm install -D
  • bun add -dnpm install -D
  • yarn add --exactnpm install -E

API

packmorph(command: string, options?: PackmorphOptions): PackmorphResult

Success:

{
  ok: true,
  type: "install" | "exec" | "run" | "create",
  npm: string,
  pnpm: string,
  yarn: string,
  bun: string,
  meta: { /* command-specific metadata */ }
}

Error:

{
  ok: false,
  reason: "not-install-command" | "parse-error" | "not-supported-command" | "disabled-command-type" | "mixed-package-managers"
}

Multi-line Success:

When parseMultiLine: true, the result has the same structure as single-line mode:

{
  ok: true,
  type: "install" | "exec" | "run" | "create",
  npm: string,
  pnpm: string,
  yarn: string,
  bun: string,
  meta: { /* command-specific metadata */ }
}

The npm, pnpm, yarn, and bun properties contain the full transformed multi-line text with non-package-manager lines preserved.

License

MIT

About

Translate package manager install commands

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •