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

ts-codemod

v4.0.4

Published

Code-Modifier for Typescript based projects

Downloads

2,108

Readme

ts-codemod

Build Status npm

Code-Modifier for Typescript based projects.

Index

Installation

npm i -g ts-codemod

Command Line Usage

A typical command looks like -

ts-codemod --transformation [transformation name] --params [transformation params] --write [glob pattern]

| Argument | Purpose | Value | | ------------------------------------ | --------------------------------------- | --------- | | --write -w (optional) | Writes back to the file | false | | --transformation -t (required) | Name of the transformation or file path | | | --params -p ( optional) | Additional transformation specific args | |

Example

So lets say I want to update the import statements throughout the application from something like —

import * as components from '../../../component'

to something like —

import * as components from 'component'

Here I have removed the unnecessary ../../../ from the import statement. To achieve this goal I can use the [normalize-import-path] transformation.

  1. Create a .tscodemodrc file
{
  // name of the transformation
  transformation: 'normalize-import-path',

  // transformation params
  params: {
    module: 'component'
  }
}
  1. Run the code mod.
ts-codemod --write src/**/*.ts

Alternatively you can also pass all the arguments without creating a .tscodemodrc file —

ts-codemod --transformation normalize-import-path --params.module=component --write src/**/*.ts

Custom transformation

Writing a custom transformation isn't very easy and one needs to understand how typescript internally converts plain string to an AST.

A good starter could be to checkout the [transformations] directory. Those transformations are written for a varied level of complexity. Also checkout the AST Explorer website to get an understanding of ASTs in general.

A custom transformation (my-custom-transformation.ts) can be implemented via extending the Transformation class.

import * as ts from 'typescript'
import {Transformation} from 'ts-codemod'

// my-custom-transformation.ts
export default class MyCustomTransformation extends Transformation {
  visit(node: ts.Node): ts.VisitResult<ts.Node> {
    // write your implementation here
    return node // will apply no-change
  }
}

It can then be executed as —

ts-codemod -t ./my-custom-transformation.ts src/**.ts

Passing Custom Params: To pass custom params to your transformation can be done as follows —

export type MyParams = {
  moduleName: string
}

// my-custom-transformation.ts
export default class MyCustomTransformation extends Transformation<MyParams> {

  // Called before the transformation is applied on the file
  before () {

  }

  visit(node: ts.Node): ts.VisitResult<ts.Node> {


    // access the params
    console.log(this.params.moduleName)

    ...
  }

  // Called after the transformation is applied on the file
  after () {

  }
}

The additional params are passed via the --params.moduleName cli argument or if you are using a .tscodemodrc file —

{
  params: {
    moduleName: 'abc'
  }
}

Post Transformation

  1. Life can't be that simple right? Running transformations will generally ruin the formatting of your files. A recommended way to solve that problem is by using Prettier.
  2. Even after running prettier its possible to have unnecessary new lines added/removed. This can be solved by ignoring white spaces while staging the changes in git.
git diff --ignore-blank-lines | git apply --cached