Skip to content

dworthen/better-install

Repository files navigation

TypeScript NPM Build Status

Better Install

Automatically install TypeScript @types.

Inspired by @yarnpkg/plugin-typescript but works with yarn@1, yarn@2, pnpm and npm.

Install

yarn

yarn add -D better-install

pnpm

pnpm add -D better-install

npm

npm install -D better-install

Configure

package.json

{
  "scripts": {
    "bi": "better-install"
  }
}

⭐ When running better-install as an npm script, it will use the package manager that invokes the script to run all commands, e.g., yarn bi lodash uses yarn to install lodash and @types/lodash.

Add Packages

The format for installing packages is

<PACKAGE_MANAGER> bi [PACKAGES...] [OPTIONS]

yarn

yarn bi lodash yargs-parser minimist-options argville

Install lodash, yargs-parser and minimist-options as a prod dependencies and corresponding @types/ as a devDependencies using yarn.

🔥 better-install installs @types/ packages if the dependency does not contain a types or typings field in package.json or an index.d.ts file in the package root. minimist-options includes a default declaration file, index.d.ts, in the package root but does not specify a types or a typings field within the package.json file. This is enough for better-install to know that minimist-options comes bundled with types and therefore will skip installing @types/minimist-options. better-install checks for types, typings and index.d.ts the same as TypeScript.

Like minimist-options, argville comes bundled with types and therfore better-install skips installing @types/argville.

better-install supports both yarn@1 and yarn@2.

👍 better-install also supports locating and installing @types/ for scoped packages.

pnpm

pnpm bi -- lodash yargs-parser minimist-options argville

npm

npm run bi -- lodash yargs-parser minimist-options argville

Install All Packages

Run better-install without any args to install all packages listed in package.json and corresponding @types/.

# with yarn
yarn bi
# OR pnpm
pnpm bi
# OR npm
npm run bi

Add devDependency

better-install passes all unknown cli flags to the underlying package manager. To install dev dependencies simply pass the dev flag for the appropriate package manager.

# with yarn
yarn bi -D lodash
# OR pnpm
pnpm bi -D lodash
# OR npm
npm run bi --save-dev lodash

Installs lodash and @types/lodash as devDependencies.

CLI Options

Run yarn bi --help (or with pnpm or npm) to view a full list of options.

Option Description Default
--pm [npm|yarn|pnpm] Select the package manager to use to install dependencies. Defaults to npm or, if used as an npm script, the package manager that invokes the script
-f, --filter <package_pattern> Restrict package installation to a subset of packages within a monorepo project. Supports yarn and pnpm workspaces. Expects a single glob pattern or list of glob patterns and supports globbing against package names or package directories. (default=["*"])
--verbose Print debug messages. false
-h, --help Print help message. false

⚠️ better-install passes unknown flags to the underlying package manager. For example, bi lodash -D sends the -D flag to the package manager and therfore installs lodash and @types/lodash as devDependencies (@types are always installed as devDependencies).

As a Global Bin

May also install better-install globally

# with yarn
yarn add better-install -g
# OR pnpm
pnpm add better-install -g
# OR npm
npm add better-install -g

Exposes two bins, better-install and, for convenience, bi.

Install All Packages

bi

Installs all packages listed in <CWD>/package.json.

Select a Package Manager

As a global bin, better-install uses npm by default. Override this using the --pm option.

# install all package.json dependencies and @types with yarn
bi --pm yarn
# add lodash and @types/lodash with pnpm
bi lodash --pm pnpm
# add lodash and @types/lodash as devDependencies with npm
bi lodash -D --pm npm
# Same as
bi lodash -D

Project or User Config

better-install loads configuration from project level .npmrc config files, /path/to/project/.npmrc, and user level .npmrc config files, ~/.npmrc. Instead of always passing --pm to better-install, one can define the pm key once within an .npmrc file.

# /path/to/project/.npmrc
pm=yarn

Now bi commands within /path/to/project will use yarn instead of npm. Can still override this using the --pm flag.

MonoRepo Projects

better-install supports yarn workspaces and pnpm workspaces.

yarn

// package.json
{
  "private": true,
  "workspaces": ["packages/*"]
}

Then use the -f,--filter flag to glob

yarn bi lodash --filter packages/sub-pkg
# Or with globbing
yarn bi lodash -f packages/*

The -f,--filter flag also supports globbing for the package name.

yarn bi lodash -f @pkg-name/sub-pkg
# Or with globbing
yarn bi lodash -f @pkg-name/*

pnpm

# pnpm-workspace.yaml
packages:
  - packages/*
pnpm bi lodash -f @pkg-name/*

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published