Giter VIP home page Giter VIP logo

riceburn's Introduction

Build Status

riceburn

Mod that code!

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

Installation

npm i -D riceburn

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 'riceburn';

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

To modify JSON

import mod from 'riceburn';

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 'riceburn';

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.

riceburn's People

Contributors

kenotron avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.