# code-pluck

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

Latest version **1.0.35** (published 2021-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install code-pluck
pnpm add code-pluck
yarn add code-pluck
bun add code-pluck
```

Provides the command `pluck`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.35 |
| Published | 2021-10-01 |
| First published | 2021-09-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 56.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | stackdynamicsam |

## Links

- npm: https://www.npmjs.com/package/code-pluck
- npm.io page: https://npm.io/package/code-pluck

## Dependencies (9)

- [ejs](https://npm.io/package/ejs.md) ^3.1.6
- [jest](https://npm.io/package/jest.md) ^27.1.1
- [yargs](https://npm.io/package/yargs.md) ^17.1.1
- [nodemon](https://npm.io/package/nodemon.md) ^2.0.13
- [prettier](https://npm.io/package/prettier.md) ^2.4.0
- [codescrive](https://npm.io/package/codescrive.md) ^1.0.11
- [node-fetch](https://npm.io/package/node-fetch.md) 2.6.1
- [node-sql-parser](https://npm.io/package/node-sql-parser.md) ^3.6.10
- [fetch-snippet-templater](https://npm.io/package/fetch-snippet-templater.md) ^2.2.1

## Recent versions

- 1.0.35 (latest) — 2021-10-01
- 1.0.34 — 2021-10-01
- 1.0.33 — 2021-10-01
- 1.0.32 — 2021-09-30
- 1.0.31 — 2021-09-30
- 1.0.3 — 2021-09-30
- 1.0.29 — 2021-09-29
- 1.0.28 — 2021-09-29
- 1.0.27 — 2021-09-21
- 1.0.26 — 2021-09-16
- 1.0.25 — 2021-09-16
- 1.0.24 — 2021-09-16
- 1.0.23 — 2021-09-15
- 1.0.22 — 2021-09-14
- 1.0.21 — 2021-09-14
- … 11 more at https://npm.io/package/code-pluck/versions

## 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
```bash
> yarn global add code-pluck
```

Then, to see all the arrow functions you've written in the project at your current working directory:
```bash
> pluck 'select * from $downstream.arrowfunctionexpressions' 
```

To see them and then cut them all with one command:
```bash
> pluck 'select * from $downstream.arrowfunctionexpressions' --post cut
```

To comment out all of the `console.whatever()` calls in your project:
```bash
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`:
```bash
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:
```bash
> 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](https://github.com/babel/babel/blob/master/packages/babel-types/), 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.

```bash
> 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: 
```bash
> 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:

```bash 
> 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`

```bash
pluck 'select * from $downstream.stringliterals where value like "a%"' --post cut
```
Will cut all string literal expressions which start with `"a"`.

`commentOutLine`

```bash
pluck 'select * from $downstream.identifiers where value like "console%"' --post commentOutLine
```
Will comment out any lines that contain the variable name `"console"`.

---
_Source: https://npm.io/package/code-pluck · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
