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

draxt

v1.3.0

Published

jQuery/NodeList-like module for file system (nodejs)

Downloads

84

Readme

draxt is a utility module for selecting and manipulating filesystem objects in a Node.js environment. It uses glob patterns as its "selector engine". draxt also provides several DOM-like interfaces representing filesystem objects which build on promisified APIs for the fs and fs-extra modules.

Example directory structure:

/app/
 ├── controllers/
 │   └── index.js
 ├── public/
 │   ├── script.js
 │   └── style.css
 └── views/
     └── index.html
const $ = require('draxt');

(async () => {
    // Select `/app` directory content and create a new `draxt` collection.
    const $app = await $('/app/**');
    $app
        // Let's filter js files:
        .filter((node) => node.extension === 'js')
        // Now we have a new `draxt` collection with 2 nodes.
        .forEach(async (node, index, allNodes) => {
            // `node` is instance of `File` class. Because it's a file!
            console.log(node.pathName);
            // → '/app/controllers/index.js' for the first node!

            console.log(node instanceof $.File); // → `true`

            // Let's get contents of the node. `file.read` returns a promise object.
            const content = await node.read('utf8');

            // Let's use some synchronous methods!
            node.appendSync('\na new line!')
                .chmodSync('765')
                // move the file into another directory!
                .appendToSync('/tmp'); // or `.moveToSync('/tmp')`

            console.log(node.pathName);
            // → '/hell/index.js' for the first node in the list!

            // get the parent directory of the node.
            // returns a `Directory` instance with the pathName of '/tmp'!
            const parentNode = node.parentSync(); // or `await node.parent()`

            // is the directory empty?
            console.log(parentNode.isEmptySync()); // → `false`
        });
})();

Key notes:

  • draxt has only 2 dependencies: glob and fs-extra modules.
  • draxt uses glob patterns to select filesystem objects.
  • Each item in a draxt collection is an instance of a File, Directory, or SymbolicLink class, which is a subclass of Node.
  • Every asynchronous method has a synchronous version. E.g., node.siblingsSync() for node.siblings().
  • draxt is a simple constructor function. You can extend/overwrite its methods via its prototype property (or its fn alias) or by using the draxt.extend method.
const draxt = require('draxt');
// Add a method (`images`) for filtering image files.
draxt.fn.images = function() {
    const imgExtensions = ['jpeg', 'jpg', 'png', 'git', ...];
    return this.filter(node => {
       return node.isFile() && imgExtensions.indexOf(node.extension) > -1;
    });
}

Install

Installing via npm:

$ npm i draxt

Via yarn:

$ yarn add draxt

Docs

Test

In the past, mock-fs was used for mocking test file system, but since the package is not compatible with newer versions of node.js, now regular linux cmds like mkdir and echo are used for creating test files and folders. The test fs structure are created in /tmp directory. That being said, for now, tests only work on Linux!

$ npm run test

License

Licensed under MIT.