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

broccoli-directory

v0.9.1

Published

Broccoli Directory provides familiar map/reduce/filter APIs for working with Broccoli Trees. It allows to chain broccoli tree transformations in the same way that you would chain array operations.

Downloads

4

Readme

broccoli-directory

Broccoli Directory provides familiar map/reduce/filter APIs for working with Broccoli Trees. It allows to chain broccoli tree transformations in the same way that you would chain array operations.

For example, given a directory like this,

my-files/
  some-file.txt
  other-file.txt
  yet-another-file.txt

Using with Broccoli.js

You can get a new directory with whitespace removed from every file by doing the following,

// Broccoli.js
const $d = require('broccoli-directory');

// map will receive content of the file, returned value will be written to the new file
module.exports = $d('my-files')
  .map(text => text.replace(/\s/g,''))

You can chain these operations in same way as you would with arrays.

// Broccoli.js
const $d = require('broccoli-directory');

module.exports = $d('my-files')
  // exclude other-file.txt from build
  .filter((text, relativePath) => relativePath !== 'other-file.txt')
  // remote whitespace from remaining files
  .map(text => text.replace(/\s/g,''));
  // this will produce a directory with
  // some-file.txt
  // yet-another-file.txt

You can also reduce a directory into a single file.

// Broccoli.js
const $d = require('broccoli-directory');

module.exports = $d('my-files')
  .reduce((accumulator, text) => accumulator + text, '', 'result.txt');
  // will build a directoryw result.txt that has content from all files in it 

Using in a node script

You can use this library to process a directory of files in any node.js script.

// index.js
const $d = require('broccoli-directory');

// map will receive content of the file, returned value will be written to the new file
  $d('my-files')
    .map(text => text.replace(/\s/g,''))
    .build('dist') // where should the build result be copied (omit this argument to skip copying)
    .then(builder => {
      // builder.outputPath is path to a tmp directory where the built result is
      return builder.cleanup(); // call builder.cleanup() to remove all temp directories that were created.
    });

API

You must begin by selecting a directory,

const $d = require('broccoli-directory');

$d('src')

$d.find(selector: string)

find operator allows you to select specific files from the current tree.

const $d = require('broccoli-directory');

$d('src')
  .find('**/*.js')
// will create a tree of only javascript files

$d.merge(() => IBroccoliTree)

merge operation can be used to merge multiple directories together. You must return a tree from the merge callback. This tree will be merged into your existing tree.

const $d = require('broccoli-directory');

$d('src')
  .merge(() => $('lib')) // merge lib directory with src directory

$d.use((tree: IBroccoliTree) => IBroccoliTree)

use operation exects a callback. This callback will be invoked and current tree will be passed as the first argument. You can use this operator to apply Broccoli plugins to the tree.

import $d = require('broccoli-directory');
import Filter = require('broccoli-filter');

class Empasize extends Filter {
  processString(contents) {
    return `*${contents}*`;
  }
}

$d('src')
  .use((tree) => new Empasize(tree));
// resulting directory will have * wrapping the content

$d.map((content: string) => string)

map operation allows to transform every file in the directory. map expects a callback which receives a string and returns a string.


$d('src')
  .map(content => `<a>${content}</a>`)
// resulting directory will have <a></a> wrapping the content

$d.filter((content: string, relativePath: string) => boolean)

filter operation allows to remove files from a directory. It expects a callback which will accept the file content and relative path. If the callback returns a falsy value then the file will be excluded from the output.

$d('src')
  .filter((content, relativePath) => !relativePath.startsWith('fixtures'))

$d.reduce((accumulator: string, content: string) => string, intial: string, outputFile: string)

reduce operation allows to concatinate all files in a tree into as single file.

It expects 3 arguments:

  1. callback - function that will be invoked for every file in the tree. The callback will receive as arguments an accumulator that'll contain the result of previous operation and string content of current file.
  2. initial - string that'll contain value that'll be passed as the accumulator to the first invocation of the callback.
  3. ouputFile - string path where the result will be written.
$d('src')
  .reduce((accumulator, content) => `${accumulator}${content}`, '', 'result.txt')

$d.log

log operator will output the state of the tree to console. This is useful when you're debugging a tree build.

$d('src')
  .log()
/**
 * tree = log(tree, {output: 'tree'});
 * // /Users/chietala/workspace/broccoli-stew/tmp/funnel-dest_Q1EeTD.tmp
 * // ├── cat.js
 * // └── dog.js
 */