npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@uber-web-ui/baseui-codemods

v2.0.5

Published

You can use this CLI tool to run various scripts (codemods) that will automate laborious migrations between major versions of the [baseui](https://github.com/uber/baseweb) library.

Downloads

21

Readme

Codemods for Base Web (baseui)

You can use this CLI tool to run various scripts (codemods) that will automate laborious migrations between major versions of the baseui library.

What is a "codemod"?

A codemod is just a script that makes some changes to a directory of source code. It can move files and folders around, make changes to code, and even issue git commands. The idea is to automate some sort of simple repeatable task that is required for a code migration.

👍 Save yourself some time with a codemod!

Usage

Installation

Most folks probably want to run baseui-codemods using npx. If you want to run multiple codemods however, it may be convenient to install globally.

$ npm i -g @uber-web-ui/baseui-codemods

Just make sure to install the latest version next time you run a codemod.

Running a codemod

Simply specify a path to your source code's directory (--dir) and which codemod you would like to run (--mod).

$ npx @uber-web-ui/baseui-codemods --dir=<PATH_TO_CODE> --mod=<CODEMOD_NAME>

Here is an example with some dummy options:

$ npx @uber-web-ui/baseui-codemods --dir=src --mod=v8Types

Note, there are no defaults for the --dir and --mod flags. You have to specify the directory and codemod you would like to run.

For a list of available codemods, run npx baseui-codemods -h.

Contributing

Set up

$ yarn install

Overview

We use dubstep/core to organize arrays of steps into a codemod. It looks something like this:

// A new `colorUpdate` codemod
const colorUpdate = [
  step('replace "red" with "blue"', redToBlue),
  step('replace "green" with "pink"', greenToPink),
];
const stepper = new Stepper(colorUpdate);
stepper.run();

A user can then run this script on their source code.

In practice, src/cli.js aggregates all of the available codemods into a MODS config object and makes these runnable from the command line.

This is how users will actually run our codemods.

Basic workflow

Create a new file in src that exports at least one async function. These functions will be the steps for your codemod.

// src/colorUpdate.js

async function redToBlue(options) {
  // do some stuff with options.dir
}

async function greenToPink(options) {
  // do some stuff with options.dir
}

export {redToBlue, greenToPink};

Inside of each function you can run some codemod type logic using dubstep/core and/or babel. Reference the other codemods for examples of various operations you can run. Generally it is good practice to organize the codemod into discrete steps (meaning multiple async functions) and, if possible, keep things idempotent.

There are a few ways to test your codemod while in development. Consider both methods requirements for adding a new codemod.

The first method is to unit test your logic. We use inline snapshots to verify that codemod logic runs as expected. Reference other test files to get the general pattern we expect in these tests.

The second method is to run your codemod on actual source code.

Add your codemod to the MODS object in src/cli.js.

// src/cli.js

import {redToBlue, greenToPink} from './colorUpdate.js';

const MODS = {
  // ...
  colorUpdate: [
    step('replace "red" with "blue"', redToBlue),
    step('replace "green" with "pink"', greenToPink),
  ],
};

// more CLI logic below

This makes the colorUpdate codemod available on the command line.

Run yarn build to compile everything into dist and now you can run dist/cli.js as you would any Node script.

$ node dist/cli.js --dir=~/code/some-project/src --mod=colorUpdate

You could also use yarn link and open a shell in the project directory. This is a bit closer to what an end user will experience.

$ baseui-codemods --dir=src --mod=colorUpdate

After running your codemod, you can use a diff tool to verify that the codebase has changed as expected. It is crucial to run every codemod against a number of projects to ensure it functions correctly. There are always edge-cases!

References

For some more info on developing CLI tools in node, check out this article. This project follows a lot of the recommendations in the article.

A very useful tool for iterating on AST related logic is https://astexplorer.net/. Set the compiler to Babylon7 and enable Transform, which will open two new panes. The bottom left is a babel plugin you can fill in and the bottom right is the output of that plugin on the source in the top left pane. The top right pane is for exploring the source code's AST, which will prove very useful if doing any sort of AST traversal in your codemod.

For more information on AST traversal, you will want to become familiar with how Babel plugins work, specifically Traversal via the Visitor pattern.

Versioning

| UPDATE_TYPE | Note | | --- | --- | | major | Breaking changes to CLI or an existing codemod. | | minor | New codemod or enhanced CLI functionality. | | patch | Non breaking fix for existing codemod or CLI functionality. |