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

@songkick/replaceinfiles

v1.1.6

Published

Utility to replace a map of strings in many files

Downloads

75

Readme

replace-in-files Build Status

Utility to replace a map of strings in many files

Use cases:

  • pipe from hashmark to replace references to hashmarked files - see example
  • replace dev environment paths to production
  • inject values in a config file
  • ???

Usage

Install

npm i replaceinfiles

Create or generate a replace map, save in a file or pipe to stdin

{
  "foo": "bar",
  "hello": "goodbye",
  "world": "earth",
  "%API_URL%": "https://myservice.com/api"
}

Run

Usage: replaceinfiles [options]

Options:

    -h, --help                     output usage information
    -V, --version                  output the version number
    -s, --source <glob>            glob matching files to be updated
    -d, --dest-pattern <path>      pattern to output files
    -o, --output-path <path>       path to output report file default: stdout
    -S, --silent                   do not output report
    -r, --replace-map-path <path>  path to replace map json, default: stdin
    -e, --encoding <string>        used for both read and write, default "utf-8"

Examples

  • Streaming replace map from stdin

    cat replace-map.json | replaceinfiles -s src/*.css -d 'dist/{base}'
  • Getting replace map from file

    replaceinfiles -r replace-map.json -s src/*.css -d 'dist/{base}'
  • Write report to a file

    replaceinfiles -r replace-map.json -s src/*.css -d 'dist/{base}' > report.json
    # or
    replaceinfiles -r replace-map.json -s src/*.css -d 'dist/{base}' -o report.json

Report

replaceinfiles generates a report on stdout or specified path for you to pipe other tools if you need to.

Here is an example:

{
  "options": {
    "source": "test/src/*.txt",
    "destPattern": "test/dist/{base}",
    "outputPath": null,
    "replaceMapPath": null,
    "replaceMap": {
      "hello": "goodbye",
      "world": "earth"
    },
    "encoding": "utf-8"
  },
  "result": [
    {
      "src": "test/src/one.txt",
      "dest": "test/dist/one.txt",
      "changed": true
    },
    {
      "src": "test/src/three.txt",
      "dest": "test/dist/three.txt",
      "changed": false
    },
    {
      "src": "test/src/two.txt",
      "dest": "test/dist/two.txt",
      "changed": true
    }
  ]
}

Options details

-s, --source: A glob matching the files you want to replace from

-d, --dest-pattern: A pattern to define updated files destination. You can use all the path.parse() result values (root, dir, name, base, ext), example: -d './dist/{dir}/{name}.build{ext}'

-r, --replace-map-path: Path to a replace map JSON file ({'stringToReplace': 'replaceWithThat', '..', '...'}). stdin is used as default.

-o, --output-path: A path to write the report, default is stdout

-S, --silent: Do not output report, bypasses -o

-e, --encoding: Used for both read and write, default: utf-8

API

You can also run replaceinfiles from node.

var replaceinfiles = require('replaceinfiles');

var options = {
  source: './test/*.txt',
  destPattern: './test/dist/{base}',
  replaceMap: {
    foo: 'bar'
  }
  // or, specify a path to your replaceMap json file
  // replaceMapPath: './map.json'
};

replaceinfiles(options)
  .then(function(report){
    // ...
  })
  .catch(function(error) {
    // ...
  });

If you do not specify replaceMap or replaceMapPath then stdin will be used.