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

search-in-file

v3.5.2

Published

Includes utility methods and command to search text within file(s).

Downloads

24,353

Readme

Search In File

NPM

Package quality

Includes utility methods and command to search text within file(s).

Installation

npm install --save search-in-file

Available Utility Methods

1. fileSearch(paths, textToSearch, options)

Search a text within files

Parameters:

  • paths: Absolute path files to search.
  • textToSearch: Text to search in files.
  • options: Various options for file search.

Return value: A promise with a list of files in which text is present.

2. getFilesFromDir(dirPath, recursive, omitEmpty)

Get a list of files from a directory

Parameters:

  • dirPath: Path of the directory to get files from.
  • recursive: Get files recursively from directory.
  • omitEmpty: A flag to omit empty files from result.

Return value: A list of files present in the given directory.

3. search(data, textToSearch, options)

Check if the text is present in given data

Parameters:

  • data: Data to search from.
  • textToSearch: Text to search.
  • options: Various options for search.

Return value: true, if text is present in given data. Else false

Available Options

  • recursive: Searches recursively in all sub-directories and files. Default false
  • words: Searches for the exact word. Default false
  • ignoreCase: Case in-sensitive search. Default false
  • isRegex: Searches for the regular expression. Default false
  • ignoreDir: Ignore the specified directories/path while searching. Default []
  • fileMask: Only search in files with given extension. Default null
  • searchResults: Type of search results to get. lineNo to get line with line number that contains given search text. Default filePaths

Usage As command line tool

To use this as a command line tool install this package globally using -g command.

Now, run command

search-in-file <text-to-search>

For example,

search-in-file hello

This will search for text "hello" in all the files from a directory where the command is run.

search-in-file hello world will search for text having either "hello" or "world". To search for hello world, use search-in-file "hello world". I.e. to search for text having more than one word, enclose the text in quotes.

There are the following flags available with this commands:

-p <path>, --path <path> : Path(s) of file/directory to search. Default: Current working directory

-w, --word : Search for exact word?

-i, --ignore-case : Ignore case while searching?

--reg : Consider "text-to-search" as regex?

-r, --recursive : Search recursively in sub-directories of a directory

-e <exclude-dir>, --exclude-dir <exclude-dir> : Directory/file(s) to exclude while searching

-f <file-mask>, --file-mask <file-mask> : Search in files with specific extension. Example: ".txt", ".js"

-s <type>, --search-results <type> : Type of search result. "filePaths"/"lineNo". Default: filePaths

Examples

search-in-file hello -p some-file-path

will search for text having hello in the specified file.

search-in-file hello -p some-dir-path

will search for text having hello in all the files in specified directory.

search-in-file hello -p some-dir-path -r

will search for text having hello in all the files in all subdirectories of the specified directory.

search-in-file hello -p some-dir-path -p some-other-dir-path

will search for text having hello in all the files in both the directories.

search-in-file hello -p some-dir-path -e dir-to-exclude

will search for text having hello in all the files in specified directory except the dir-to-exclude directory.

search-in-file hello -p some-dir-path -w

will search for the exact word hello.

search-in-file hello -p some-dir-path -w -i -f .txt

will search for the exact word hello (ignoring case) in all the with extension ".txt" in the specified directory

search-in-file hello -p some-dir-path -w -s lineNo

will search for the exact word hello and output the files along with the line number where the text is found.