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

codemad

v1.3.2

Published

Mod that code like you're mad!

Downloads

56

Readme

codemad

Mod that code like you're mad!

This package allows you to write simple code mods. It allows you to transform JSON, Typescript and plain text files.

Installation

npm i -D codemad

Usage

This library is meant to be used as a library inside a node utility. The fluent style API and can be chained. The file matching is provided by the glob utility. See the glob documentation for details on what can be passed into the matcher syntax.

The API

// The JSON object is passed into the callback, it expects a serializable object to be written to disk
mod('some file pattern').asJson(json => {
  //...
});

// The text is given to a callback, return a transformed text to overwrite the original file
mod('some file pattern').asText(text => {
  //...
});

// This mod uses the transformer API from Typescript. Provide a visitor function as a callback
mod('some file pattern').asTypescript(node => {
  //...
});

To modify text

import mod from 'codemad';

mod('config.txt').asText(text => {
  return text.replace('hello', 'world');
});

To modify JSON

import mod from 'codemad';

interface PackageJson {
  version: string;
}

mod('package.json').asJson((json: PackageJson) => {
  // JSON can be typed
  json.version = '1.0.0'; // Change the JSON
  return json; // Important: be sure to return a serializable JSON
}, 2); // The API also can take in an optional number of spaces for indentation

To modify Typescript

import mod from 'codemad';

mod('src/test.ts').asTypescript((node, modder) => {
  if (ts.isExpressionStatement(node)) {
    for (let child of node.getChildren()) {
      if (ts.isCallExpression(child)) {
        return modder.prepend(node, 'console.log("hi");');
      }
    }
  }
});

There are several APIs that you can use with the modder object:

  • prepend(node: ts.Node, content: string)
  • append(node: ts.Node, content: string)
  • replace(node: ts.Node, content: string)
  • remove(node: ts.Node, content: string)
  • removeFull(node: ts.Node, content: string)

The difference between remove() and removeFull() is that the removeFull() version will remove whitespace before and after the node. This is a clean way to remove full statements, for example without leaving whitespace or blank lines in the area of the removed node.