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

@static-pages/io

v1.1.0

Published

Helpers for reading and writing data.

Downloads

32

Readme

Static Pages / IO

This package provides utilities for reading and writing documents from an abstract filesystem.

Use read() to create an async iterable collection of documents, and write() to handle rendering and storing.

This project is structured as a toolkit split to many packages, published under the @static-pages namespace on NPM. In most cases you should not use this io package directly, but the @static-pages/starter is a good point to begin with.

Usage

import staticPages from '@static-pages/core';
import { read, write } from '@static-pages/io/node';

staticPages({
    from: read({
        cwd: 'pages',
        pattern: '**/*.md',
    }),
    controller(data) {
        data.now = new Date().toJSON();
        return data;
    },
    to: write({
        render({ title, content, now }) {
            return `<html><body><h1>${title}</h1><p>${content}</p><p>generated: ${now}</p></body></html>`;
        },
    })
})
.catch(error => {
    console.error('Error:', error);
    console.error(error.stack);
});

Documentation

For detailed information, visit the project page.

read(options: ReadOptions<T>): AsyncIterable<T>

interface ReadOptions<T> {
    // Filesystem implementation that handles reads and writes.
    fs: Filesystem;
    // Current working directory.
    cwd?: string;
    // File patterns to include.
    pattern?: string | string[];
    // File patterns to exclude.
    ignore?: string | string[];
    // Callback to parse raw contents into an object.
    // default: see About the default `parse` function
    parse?(content: Uint8Array | string, filename: string): T | Promise<T>;
    // Handler function called on error.
    // default: (err) => { throw err; }
    onError?(error: unknown): void | Promise<void>;
}

write(options: WriteOptions<T>): void

interface WriteOptions<T> {
    // Filesystem implementation that handles reads and writes.
    fs: Filesystem;
    // Current working directory.
    cwd?: string;
    // Callback that retrieves the filename (URL) of a page.
    // default: (d) => d.url + '.html'
    name?(data: T): string | Promise<string>;
    // Callback that renders the document into a page.
    // default: (d) => d.content
    render?(data: T): Uint8Array | string | Promise<Uint8Array | string>;
    // Handler function called on error.
    // default: (err) => { throw err; }
    onError?(error: unknown): void | Promise<void>;
}

Filesystem interface

You can provide a Filesystem implementation for both read() and write() helpers. This interface is a minimal subset of the NodeJS FS API.

interface Filesystem {
	stat(
		path: string | URL,
		callback: (err: Error | null, stats: { isFile(): boolean; isDirectory(): boolean; }) => void
	): void;

	readdir(
		path: string | URL,
		options: {
			encoding: 'utf8';
			withFileTypes: false;
			recursive: boolean;
		},
		callback: (err: Error | null, files: string[]) => void,
	): void;

	mkdir(
		path: string | URL,
		options: {
			recursive: true;
		},
		callback: (err: Error | null, path?: string) => void
	): void;

	readFile(
		path: string | URL,
		callback: (err: Error | null, data: Uint8Array) => void
	): void;

	writeFile(
		path: string | URL,
		data: string | Uint8Array,
		callback: (err: Error | null) => void
	): void;
}

About the default parse function

When using the default parser, a file type will be guessed by the file extension. These could be json, yaml, yml, md or markdown.

  • json will be parsed with JSON.parse
  • yaml and yml will be parsed with the yaml package
  • md and markdown will be parsed with the gray-matter package

When the document is missing an url property this function will assign the filename without extension to it.

Importing @static-pages/io/node

The @static-pages/io/node export provides the same functions as the @static-pages/io with the added benefit of setting the default value of the fs property to the node:fs package.

This way it is easier to use these utility functions from a node script and also easier to bundle them for browsers.

Missing a feature?

Create an issue describing your needs! If it fits the scope of the project I will implement it.