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

picklr

v0.11.2

Published

A utility to update a single line in multiple files

Downloads

47

Readme

Picklr

npm version Verify Coverage Status

"Pick line & replace"

A simple, zero dependency, recursive, text replacer.

Use to update a specific text across multiple file types in a project. Finds matching lines in files, based on text or regular expression, and runs a text replacement on just those lines matched*.
* If you have special cases, you can supply a filter function to exclude those special cases.

You might be reading this because you're in a pickle where you have to change a line or text pattern across a big project. No worries, picklr allows you to test replacement behavior prior to any updates with actions echo and audit. It's good to see what would happen prior to any file being updated across a project, and also what files will get omitted from the change set.

API

Picklr exports a single function that starts synchronous file processing when invoked.

function (startDir, options)

Options

A brief description of the options that control file selection and processing:

  • action - {String} echo|audit|update The action taken on each file. Defaults to echo. Use echo and audit actions to test the results of picklr with options prior to running update.
    • echo - Just output what files could be affected by a change. Tests includeExts and excludeDirsRe options.
    • audit - Must use with targetText and replacementText. Outputs lines found and the change plus any files that would be omitted. Tests includeExts, excludeDirsRe, targetText and replacementText options.
    • update - Does the actual update on the files in place. Use only if you have version control.
  • includeExts - {Array} File extensions used to include files in processing. Must include the dot.
  • excludeDirsRe - {RegExp} A regular expression used to exclude directories from processing. A match is an exclusion.
  • targetText - {String|RegExp} Used to identify the line to process. First argument to string.replace.
  • replacementText - {String|Function} Used to replace the targetText. Second argument to string.replace.
  • replacementFilter - {Function} A function to deny a specific replacement otherwise matched by targetText. Return true to allow the replacement to proceed as normal, false to deny it. If not supplied, defaults to all matched replacements allowed.
    signature: function (filePath, lineText): Boolean
  • logger - {Function} A log function to receive output. Defaults to console.log.

Example Usage

// Example use for a copyright banner text update:
import picklr from 'picklr';

// Output what files WOULD be affected by includes/excludes:
picklr('.', {
  action: 'echo',
  includeExts: ['.js', '.jsx', '.scss'],
  excludeDirsRe: /\/\.|node_modules|dist|tmp|reports/i
});

// Output what WOULD be processed using targetText/replacementText:
picklr('.', {
  action: 'audit',
  targetText: 'Copyright (c) 2015',
  replacementText: 'Copyright (c) 2015, 2016',
  includeExts: ['.js', '.jsx', '.scss'],
  excludeDirsRe: /\/\.|node_modules|dist|tmp|reports/i
});

// Got it. Now, actually perform the pick line and replacement.
picklr('.', {
  action: 'update',
  targetText: 'Copyright (c) 2015',
  replacementText: 'Copyright (c) 2015, 2016',
  includeExts: ['.js', '.jsx', '.scss'],
  excludeDirsRe: /\/\.|node_modules|dist|tmp|reports/i
});

Non-module Usage

// import the es module in a non-module environment, echo options example:
import('picklr').then(({ picklr }) => {
  picklr('.', {
    action: 'echo',
    includeExts: ['.js', '.jsx', '.scss'],
    excludeDirsRe: /\/\.|node_modules|dist|tmp|reports/i
  });
});

Omitted

To see only what files would be omitted, set your picklr script to use action 'audit', then grep the output for 'Omitted'.

// myPicklrScript.js

import picklr from 'picklr';
picklr('.', {
  action: 'audit',
  targetText: 'Copyright (c) 2015',
  replacementText: 'Copyright (c) 2015, 2016',
  includeExts: ['.js', '.jsx', '.scss'],
  excludeDirsRe: /\/\.|node_modules|dist|tmp|reports/i
});
node myPicklrScript.js | grep 'Omitted'

License

MIT