totalist

3.0.1 · active · verified Sun Apr 19

totalist is a highly optimized, minimalist (under 250 bytes gzipped) utility for recursively listing all files within a specified directory in Node.js. It enables developers to efficiently traverse file systems and apply a custom callback function to each file found, providing the relative path, absolute path, and the `fs.Stats` object directly. The package offers both asynchronous (default, since Node.js 8.x) and synchronous (since Node.js 6.x) modes to accommodate various application contexts and performance requirements. The current stable version is 3.0.1, which includes native ESM support via `exports` maps. Its primary differentiators are its minuscule footprint, direct integration with `fs.Stats` within the callback, and the avoidance of generating intermediate large file lists, making it suitable for performance-critical scenarios.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `totalist/sync` to recursively scan a directory and categorize files based on their extensions. It sets up a temporary directory with various files and then logs the categorized absolute paths and file sizes.

import { totalist } from 'totalist/sync';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import { existsSync, mkdirSync, writeFileSync } from 'fs';

// Create a dummy directory for demonstration
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const testDir = resolve(__dirname, 'temp_files');
if (!existsSync(testDir)) {
  mkdirSync(testDir, { recursive: true });
}
writeFileSync(resolve(testDir, 'file1.js'), 'console.log("file1");');
writeFileSync(resolve(testDir, 'style.css'), 'body { color: red; }');
writeFileSync(resolve(testDir, 'another.js'), 'console.log("file2");');
mkdirSync(resolve(testDir, 'subdir'), { recursive: true });
writeFileSync(resolve(testDir, 'subdir', 'nested.txt'), 'nested content');

const scripts = new Set();
const styles = new Set();
const others = new Set();

try {
  totalist(testDir, (name, abs, stats) => {
    if (name.endsWith('.js')) {
      scripts.add(abs);
    } else if (name.endsWith('.css')) {
      styles.add(abs);
    } else {
      others.add(abs);
    }
    console.log(`Processing: ${name} (size: ${stats.size} bytes)`);
  });

  console.log('\n--- Summary ---');
  console.log('JavaScript files:', [...scripts]);
  console.log('CSS files:', [...styles]);
  console.log('Other files:', [...others]);
} catch (error) {
  console.error('Error listing files:', error);
}

// Clean up dummy directory (optional, but good for examples)
// import { rmSync } from 'fs';
// rmSync(testDir, { recursive: true, force: true });

view raw JSON →