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

read-modify-write

v1.0.5

Published

Node module for reading, modifying and writing files recursively

Downloads

15

Readme

read-modify-write

NodeJS - Read File, Modify Content, Write to File

Copy Files

Table of content

  1. Introduction to read-modify-write
  2. Read File/Directory
  3. Write to File/Directory
  4. Filter files
  5. Modify Content
  6. Prepare for Production
  7. Run tests

1. Introduction to read-modify-write

This Node module provides an API to interact with the file system and perform recursive sequential IO operations (such as file reading/writing and file content modification). It executes all operations in a blocking and synchronous way. It is a very simple yet very flexible toolkit with size of less than 2KB and no dependencies. To use the library all we need is to import read-modify-write module into our code.

const rmw = require('read-modify-write');

2. Read File/Directory

To read file content recursively in a chosen directory we simply need to pass the folder path as the first parameter to the already imported module:

rmw('src/dummy');

3. Write to File/Directory

To copy files from one directory to another we need a path modifier function which is passed as a second parameter:

const move = filePath => filePath.replace('src', 'dist');
rmw('src/dummy', move);

The path modifier function takes one parameter which is the location / path for each of the listed files. The code above copies all files in the 'src/dummy' folder to the 'dist/dummy'.

4. Filter files

To filter files with certain extension we need to supply a function that will be called on each files on the list, and only return the values that pass a specific test (in this case the file extension is present in the string):

const move = filePath => filePath.replace('src', 'dist');
const filterHtml = c => c.endsWith('.html');
rmw('src/dummy', move, filterHtml);

This function, in combination with the previous one can be used to add suffix for certain file types:

const move = filePath => filePath.replace('.js', '.min.js');
const filterJs = c => c.endsWith('.js');
rmw('src/dummy', move, filterJs);

5. Modify files

Consider an html file in 'src' folder with the following content:

<html lang="en"><head><meta charset="utf-8"><title></title></head></html>

To update the file content and save it to a new location we need to pass a forth parameter which is an update function which accepts file content as an argument.

const move = filePath => filePath.replace('src', 'dist');
const filterHtml = c => c.endsWith('.html');
const update = c => c.replace('<title></title>', '<title>Page title</title>');
rmw('src/dummy', move, filterHtml, update);

The code above will result in a new html file created in the 'dist/dummy' folder with the following content:

<html lang="en"><head><meta charset="utf-8"><title></title></head></html>

6. Prepare for production

This toolkit can assist you automate painful or time-consuming tasks in your development workflow. For example when publishing to production it can help you by doing CSS preprocessing, JS transpiling, minification, and much more:

const terser = require('terser');
const cleanCss = require('clean-css');
const uglify = c => terser.minify(c).then(({ code }) => code);
const clean = c => new cleanCss().minify(c).styles;
const move = filePath => filePath.replace('src', 'dist');
const filterJs = c => c.endsWith('.js');
const filterCss = c => c.endsWith('.css');
rmw('src', move);s
rmw('src/dummy', move, filterJs, uglify);
rmw('src/dummy', move, filterCss, clean);

7. Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can run the tests the following command:

$ npm test