1.0.0 • Published 1 year ago

u2d v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

U2D

u2d › up to date

ttynon-tty
U2D TTY DemoU2D Non-TTY Demo

A developer experience tool to enforce environment and dependency version policies.

  • Want all of your projects to use the same node and npm semver range?
  • Same react major version?
  • How about detecting duplicate packages in a project's node_modules?
  • Or even making sure certain dependencies are never installed?

Use u2d anywhere in your projects lifecycle to get instant feedback to protect releasing code integrating with unsupported versions. Have several projects and want a single configuration? Host your configuration remotely on your own CDN, or use something like https://jsonbin.io/.

TOC

Install

$ npm i -D u2d

CLI

The u2d CLI was built to be quick and simple, allowing developers to run it anywhere at any step in the development process.

In a terminal

$ npx u2d 

Or as an npm script

{
  "scripts": {
    "prepare": "u2d"
  }
}

CLI Options

flagdescriptiontypedefault
-p \| --cwdworking directorystringprocess.cwd()
-c \| --configconfiguration file or urlstringauto detected
-m \| --managerpackage managernpm \| pnpm \| yarnauto detected
-d \| --depthpackage node_modules depthInfinity \| numberInfinity
-e \| --envpackage node_modules environmentlocal \| dev \| prod \| globallocal
-l \| --loglogging levelerror \| warn \| info \| debuginfo
-a \| --localenable package.json engines/dependencies
-b \| --baildisable error collection
-s \| --silentdisable logging
-o \| --show-configprint config and exit
-h, --helpdisplay help

You can also run npx u2d --help locally

Node

You can directly import u2d in your node projects too. Useful for injecting into scripts or actions, allowing you to manually handle results.

// Supports ESM
import u2d from 'u2d';
// and CJS
const u2d = require('u2d').default;

u2d({ /* options */ }).then(({ skips, infos, errors, warnings }) => {
  if (errors.length) {
    process.exit(1);
  }
});

Node Options

namedescriptiontypedefault
cwdworking directorystringprocess.cwd()
configconfiguration file or urlstring \| objectauto detected
managerpackage managernpm \| pnpm \| yarnauto detected
depthpackage node_modules depthInfinity \| numberInfinity
envpackage node_modules environmentlocal \| dev \| prod \| globallocal
loglogging levelerror \| warn \| info \| debuginfo
localenable package.json engines/dependenciesbooleanfalse
baildisable error collectionbooleanfalse

Checks

A check is a semver range to run against an found version. A check must define a pass range, and optionally a fail range and/or a help description.

Example node check, will pass on 16+, warn on 14-15, and fail on <12.

{
  "engines": {
    "node": {
      "pass": ">=16",
      "fail": "<12",
      "help": "https://letmegooglethat.com/?q=update+node"
    }
  }
}

Checks can also be expressed using shorthand

inputoutputdescription
true{ pass: '*' }always pass, use to override config
false{ pass: '<*' }always fail, use to error if installed
string{ pass: string }provide pass
number{ pass: '^number' }provide pass, with caret
[pass, fail]{ pass, fail }provide pass and fail
[pass, true]{ pass, fail: '*' }provide pass, use to require
[pass, fail, help]{ pass, fail, help }provide pass, fail and help

Engine Checks

These checks are explicit, as it will check the environment of the cwd.

  • node
  • npm
  • pnpm
  • yarn

Package Checks

Currently only supporting npm, u2d will check the installed packages based on the env and specified depth.

Config

By default, u2d doesn't do much without some configuration. You can use your local package.json as a starting point, by enabling the local option. Simply add one of the files below, or define your own and provide using a config option.

  • u2d property in package.json
  • .u2drc JSON file
  • .u2d with .json | .js | .cjs extension
  • u2d.config with .js | .cjs extension

This configuration can live locally, or externally; we suggest both! Hosting externally enables multiple projects to use a single source of configuration. Here's an example hosted at https://jsonbin.io/. Hosting locally allows individual projects to add project specific configuration and/or override external policies.

The result can expose any node option, with the addition of the following

namedescriptiontype
extendsconfiguration filestring
enginesenvironment checks{ [key: string]: Check }
packagesdependency checks{ [key: string]: Check }

Example

module.exports = {
  extends: 'https://api.jsonbin.io/v3/b/628e7498402a5b38020d5cb1', // A basic example
  local: true, // Use `package.json` engines/dependencies
  engines: {
    node: 16, // Warn if not on node@^16
    npm: '7 - 8' // Warn if not npm@>=7 <9
  },
  packages: {
    'left-pad': false, // Error if installed
    'lodash': [4, true], // Error if not on lodash@^4
    'react': true // Allow any version
  }
}

Environment

The env config refers to the type of dependencies to run package checks against. By default, u2d uses local to check your cwd for matching node_modules packages.

export const Environment = {
  GLOBAL: 'global',
  LOCAL: 'local',
  PROD: 'prod',
  DEV: 'dev'
} as const;
envdescription
globalCheck all node_modules packages in the global install prefix
localCheck all node_modules packages in the cwd
prodCheck only non-development node_modules packages in the cwd
devCheck only development node_modules packages in the cwd

Manager

Currently, u2d only supports npm but recognizes the value in handling multiple project management solutions. This limitation only applies to package checks.

export const Manager = {
  NPM: 'npm',
  PNPM: 'pnpm',
  YARN: 'yarn'
} as const;
managerdescription
npmhttps://www.npmjs.com/
pnpmhttps://pnpm.io/
yarnhttps://yarnpkg.com/

Level

By default u2d uses the info log level to communicate what was processed to give developers critical information about the executed checks. The level also applies to capturing results when using the node export.

export enum Level {
  error = 0,
  warn = 1,
  info = 2,
  debug = 3
}
leveldescription
errorCapture only exceptions and/or failed checks
warnCapture non-pass checks, and errors
infoCapture pass checks, warnings and errors
debugCapture everything, including skips

License

u2d is MIT licensed