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-ast

v0.0.3

Published

Command line interface for cannabis, the TypeScript advanced AST Query library

Downloads

28

Readme

Command line interface for cannabis, the TypeScript/JavaScript advanced AST Query library

Install

npm install -g ts-ast

Syntax

ts-ast --query "//* [@name == 'foo']" --files "src/**/are42/**/*.ts?"

Options

  • --query: the query to search. It could be a query string or a text file with the query. If a files exists it will be used, otherwise it assumes that is a query string. Examples: --query "//* [@name == 'foo']", --query ./queries/myLongQuery.txt.
  • --files: TypeScript files to search. Valid files are .ts, .tsx, .js, .jsx. The value could be a glob pattern, in that case all files matching the pattern will be the input. Examples: --files "src/**/are42/**/*.ts?".
  • --project: TypeScript project in which to search. It could be a folder or a tsconfig.json path. If --files also given it will be used as a filter pattern.
  • --params: query parameters. It could be a json file containing an object or a literal json text. Examples: --params blackListWords.json, --params '{"blackList": ["foo","bar"]}'
  • --output: the output style. Valid values: nodePath, text, name, filePath, kind or a combination of any of them, comma separated.
  • --help: prints help and exits.
  • --outputFile: If given the search result will be written in that file, otherwise will be written in stdout.
  • --outputCompress: if given JSON output will be minified.
  • --outputCount: Just print the result count.

Examples

Give a query as argument and search it across files that match given glob pattern:

ts-ast --query "//* [@name == 'foo']" --files "src/**/are42/**/*.ts?"

Give a query from a file and search it across files of given project

ts-ast --query "./queries/myLongQuery.txt" --project ../git/project1/tsconfig.json

Search given query in all files of current folder's TypeScript project:

ts-ast --query "//* [@name == 'foo']" --project .

Search given query in current folder's project, filtering files using given pattern in --files argument, and using query parameters from given file's text:

ts-ast --query "[//* containsAnyOf(@name, {blackList}]" --project . --files "**/area44/**/*" --params blackListWords.json

Same as before, but passing parameters literally in the argument:

ts-ast --query "[//* containsAnyOf(@name, {blackList}]" --project . --files "**/area44/**/*" --params '{"blackList": ["foo","bar"]}'

Listing only name and kind of matches using --output:

ts-ast --query "// Identifier [..//InterfaceDeclaration]" --project . --output "name,kind"

Since TypeScript is a superset of JavaScript, this also support JavaScript. In this case we use --files to target .js files and --outputCount to just print the matched nodes count.

ts-node bin/cannabis.js --query "//FunctionDeclaration" --files "../../js-project/lib/**/*.js" --outputCount

TODO:

--timings --trace --logs to write query logs.