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

code-pluck

v1.0.35

Published

A command line utility to perform SQL-like queries on JavaScript codebases.

Downloads

35

Readme

Code Pluck

A command line utility to perform SQL-like queries on JavaScript codebases.

pluck allows you to search for pieces of code in your project by syntactic type, treating your code like an AST database.

To install

> yarn global add code-pluck

Then, to see all the arrow functions you've written in the project at your current working directory:

> pluck 'select * from $downstream.arrowfunctionexpressions' 

To see them and then cut them all with one command:

> pluck 'select * from $downstream.arrowfunctionexpressions' --post cut

To comment out all of the console.whatever() calls in your project:

pluck 'select * from $downstream.memberexpressions where "callee.object.name" like "console%"' --post commentOutLine

Note the " " around the property lookup in the where expression.

To comment out all of the console.whatever() calls in your project only in files where the path matches test:

pluck 'select * from "/test/.memberexpressions" where "callee.object.name" like "console%"' --post commentOutLine

Note, also, the " " around the target in the from expression.

API

The general form of the api looks like this:

> pluck 'select [propertyName | *] from ["$downstream" | /pattern/].[ast-type-name] where [propertyName[.member, ...]]] like "some value"' --post [post-function-name]

ast-type-name can be any valid babel type, lowercased: see here, or a valid pluck-defined alias.

Types are either plural or singular: plural type names with return all matches for a type per file, singular with stop at the first match.

Pluralisation is simplified to adding "s" to the end of the type, regardless of whether doing so is correct in terms of English grammar.

> pluck 'select * from $downstream.identifier' # -> maximum of one result per file 
> pluck 'select * from $downstream.identifiers' # -> many results per file

There are a number of aliases (WIP) for type names, currently these can be learned by inspecting src/ast-aliases.js.

For example:

> pluck 'select * from $downstream.fatarrows' 
> # same as 
> pluck 'select * from $downstream.arrowfunctionexpressions' 

The * (asterix) token when used with select will return the entire piece of code that is matched as a string.

> pluck 'select * from $downstream.fatarrows'

// ... more results above 
	---
	11: (line 36)
	async (someArg) => {
	  return await fetch(...someArgs)
	}
// ... more results below

Alternatively, select can be used with a property name:

> pluck 'select params from $downstream.fatarrows'

// ... more results above 
########

path:  /my-project/myfile.js
[
  Node {
    type: 'Identifier',
    start: 132,
    end: 141,
    loc: SourceLocation {
      start: [Position],
      end: [Position],
      filename: undefined,
      identifierName: 'props'
    },
    name: 'someArg'
  }
]
// ... more results below

The where expression can look up nested object properties. So were I to refine the last query to have a good chance of only returning the highlighted result, I could try:

> pluck 'select params from $downstream.fatarrows where "params.0.name" like "someArg"' 

Supported SQL Functionality

  • select
    • *
    • propertyName
  • from
    • $downstream.{ast-type-name} (searches all files below cwd)
    • "/{some-pattern}/.{ast-type-name} (searches all paths that match {some pattern})
  • where
    • LIKE function
      • "%STR" | "STR%" | "STR"

Post Operations and Mods

Code Pluck accepts a --post flag which allows a user to apply a function to the query results.

Currently the two supported functions are:

cut

pluck 'select * from $downstream.stringliterals where value like "a%"' --post cut

Will cut all string literal expressions which start with "a".

commentOutLine

pluck 'select * from $downstream.identifiers where value like "console%"' --post commentOutLine

Will comment out any lines that contain the variable name "console".