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

@srob/files

v0.0.10

Published

Helpers to programmatically edit text files, save, diff, folder-diff, etc.

Downloads

18

Readme

@srobfr/files

Helpers to programmatically edit text files, save, diff, folder-diff, etc.

Installation

This is a library. You can install it using npm :

npm install --save @srob/files

Usage

File loading, saving and diff-ing :

// test.mjs
import { load } from "@srob/files";

(async () => {
    // Loads the file content if it exists. If the file does not exist, file.content will be null.
    const file = await load("test.md");

    // Change the content (A)
    file.content = `# Sample file

Initial text
`;

    // Writes the new content in the file
    await file.save();

    // Change the content (B)
    file.content = `# Sample file

This file contains some text.

Last edition date : ${new Date()}
`;

    // Compute and prints the diff between (A) and (B).
    console.log(await file.diff());
})();

Running this code with node test.mjs will typically print :

--- /d/w/bench/test.md	2023-10-29 18:19:44.571108700 +0100
+++ /tmp/diff/test.md	2023-10-29 18:19:44.566022491 +0100
@@ -1,3 +1,5 @@
 # Sample file
 
-Initial text
+This file contains some text.
+
+Last edition date : Sun Oct 29 2023 18:19:44 GMT+0100 (Central European Standard Time)

The diff implementation can be changed using the DIFF environment variable :

DIFF="diff" node test.mjs

3c3,5
< Initial text
---
> This file contains some text.
> 
> Last edition date : Sun Oct 29 2023 18:22:47 GMT+0100 (Central European Standard Time)

Different diff commands are tried, using the first available on your system :

  • $DIFF '<original>' '<modified>'
  • meld '<original>' '<modified>'
  • colordiff -u '<original>' '<modified>'
  • diff -u '<original>' '<modified>'

Folder diff

It is possible to get a folder diff when multiple files are loaded :

import { folderDiff, load } from "@srob/files";

(async () => {
    // Load a file in a/b folder
    const file1 = await load("/tmp/a/b/file1.md");
    file1.content = `Last edition date : ${new Date()}
`;
    // Load a file in a/c folder
    const file2 = await load("/tmp/a/c/file2.md");
    file2.content = `Last edition date : ${new Date()}
`;

    // Will print a recursive diff at the common parent folder (a) of all the loaded files
    console.log(await folderDiff());
})();

Running this code with node test.mjs will typically print :

diff -Nru /tmp/a/b/file1.md /tmp/folderDiff/b/file1.md
--- /tmp/a/b/file1.md	1970-01-01 01:00:00.000000000 +0100
+++ /tmp/folderDiff/b/file1.md	2023-10-29 19:00:55.519593897 +0100
@@ -0,0 +1 @@
+Last edition date : Sun Oct 29 2023 19:00:55 GMT+0100 (Central European Standard Time)
diff -Nru /tmp/a/c/file2.md /tmp/folderDiff/c/file2.md
--- /tmp/a/c/file2.md	1970-01-01 01:00:00.000000000 +0100
+++ /tmp/folderDiff/c/file2.md	2023-10-29 19:00:55.519593897 +0100
@@ -0,0 +1 @@
+Last edition date : Sun Oct 29 2023 19:00:55 GMT+0100 (Central European Standard Time)