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

frontmatter-file-batcher

v0.0.1-beta.0

Published

Asynchronous bulk edit utility for Front Matter powered Markdown files

Downloads

5,931

Readme

Forked from https://github.com/hilja/file-batcher All issues should be reported there.

File Batcher

A tool to batch process large quantities of files asynchronously. It provides you some convenient tools to edit Front Matter fueled Markdown files (the stuff that most static site generators use). But is not limited to any specific filetype or format.

It could be used for for example to:

  • Search and replace text in multiple files.
  • To rename large quantities of files.
  • Find unused packages by scanning source files.
  • Reformatting chunks of text.
  • Analyzing the readability of text in blog posts.
  • Minifying files attached to blog posts.

Features

  • Is asynchronous.
  • Can glob.
  • Can do bulk edits without you writing any iterators.
  • Provides a sane API to edit complex shapes with (immutability-helper)
  • The remove method doesn’t delete the files, but chugs them into the trash.
  • Exposes read, write, and remove methods to edit individual posts.
  • Is probably fast.

Install

npm i frontmatter-file-batcher

Usage

Imagine having thousands upon thousands of files like this:

---
title: Foo
description: Very Foo
date: "2019-07-12 07:28"
categories:
  - images
  - trains
---
Hello!

We can edit them like so:

import { batch } from "file-batcher";

// A sane limit of 100 concurrent operations.
batch("fixtures/test-content/**", 100, async ({ goods, actions }) => {
  const { author } = goods.data;
  const { update, save } = actions;

  if (!author) return;

  // Update the author’s name with the provided immutability-helper. The update
  // function is prepopulated with the data (`goods`) from the post.
  const newData = update({
    data: { author: { $set: "Slartibartfast" } },
  });

  // At the end you can save your post with the new data.
  await save(newData);

  console.log("Just saved:", goods.path);
});

See examples for more examples.

API

You've got 4 public methods at your disposal: batch, read, remove, and write.

batch(input[, limit, onEach])

Return a promise that resolves to an array with the parsed Front Matter.

input

Type: string|array

If string, it uses uses glob. Or an array of file paths, relative to the current working directory.

limit

Type: string|array Default: Infinite

The concurrency.

onEach(args)

The function that runs on every iteration. It provides you a set of handy tools in the args. See below.

args

Type: object

args.goods

Type: object|any

If the target file is Front Matter, then goods will be an object provided by gray-matter, with a following shape:

{
  content: 'Hello\n',
  data: {
    title: 'foo',
    description: 'bar',
    categories: ['images']
  },
  isEmpty: false,
  excerpt: '',
  path: '/path/to/the/articles/bar.md'
}

If the file is not Front Matter, then goods will be the file’s contents verbatim.

args.actions

Type: object

Actions has all the tools you need to edit/save the files.

args.actions.update(pattern)

Type: function

This is a prepopulated immutability-helper. It's just a tool that provides a syntax for editing complex shapes. Using it is completely optional.

The syntax:

const newData = update({
  data: { author: { $set: "Slartibartfast" } },
});

See more advanced examples in the immutability-helper docs.

args.actions.save(data[, path])

Type: function

Saves the currently iterated file with the data passed to it. See the write method for more info.

data

Type: object

The data to save, in the upper-mentioned shape.

Example:

// Inside the `onEach`.
const capitalize = (string) => string[0].toUpperCase() + string.substring(1);

goods.data.name = capitalize(goods.data.name);

await save(goods);

path?

Type: string

This helper function is prepopulated with with the current file, so, if you’re operating on that file, you don’t need to pass in a path. You can, tho, if you want to save it to a new location.

args.actions.remove(path?)

Type: function Returns: Promise

This one doesn't actually delete anything, but moves it to your computers Trash.

path?

Type: string

It’s prepopulated with the current file, so use this param only if you want to delete another file, that isn’t the one in the iteration.

args.index

Type: number

args.files

Type: array

The original array of files we’re looping over.

delay(milliseconds[, options])

Type: function Return: Promise

See delay.

Throttles the current iteration, good if you’re using somehow constricted API.

Example:

const onEach = ({ throttle }) => {
  // Make only 20 calls per minute.
  await delay(3000)
  // Call your API or whatever.
  const data = fetch('https://example.com/rpm-limited-api')
}

read(file)

Returns: Promise<object>

Reads a file asynchronously and returns its contents. If it’s a markdown file, the Frontmatter will be parsed and it will return the the following shape:

{
  content: '',
  data: {
    description: 'You literally took a shit in the blender',
    title: 'Kitchen nightmares'
  },
  isEmpty: false,
  excerpt: '',
  path: '/path/to/kitchen-nightmares.md'
}

file

Type: string

A path to a file. Relative to the current working directory.

remove(path[, options])

Returns: Promise

Moves a given file into your computers trash, where you can then recover it, if you so like.

path

Type: string

A path to a file. Relative to the current working directory.

options?

Type: object

Options to pass to the underlying library trash.

write(path, data[, options])

An asynchronous function that takes some data and writes it into a file. If the data looks like it's meant to be parsed into a Front Matter, then it will be parsed into Front Matter. E.g. it has a shape: { content: '', data: {} }.

path

Type: string

Where to write the data. Relative to the current working directory.

data

Type: object|any

If it's an object that has data and content in it, then it's parsed into Front Matter. Otherwise it's just written into the file as is.

options.writeFile?

Type: object

Options to pass to Node's fs.readFile.

options.stringify?

Options to pass to gray-matter's stringify method.

Similar packages

If you need something more generic and with more API, then Gulp might be your thing.