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

files-path

v1.2.1

Published

JS Library to List Files in a Path

Downloads

118

Readme

files-path

JS Library to List Files in a Folder List

NPM version Build Status codecov dependencies Status Known Vulnerabilities

Super simple to use

files-path is designed to be the simplest way possible to list files in a path.

var fp = require('files-path');
var files = fp.sync({
  basePath: 'tests/documents',
  path: 'dir-b/dir-z'
});

console.log(JSON.stringify(files, null, 2));

console output:

[
  {
    "name": "file-dir-b.txt",
    "path": "dir-b",
    "fullName": "tests/documents/dir-b/file-dir-b.txt",
    "fullPath": "tests/documents/dir-b"
  },
  {
    "name": "file-dir-z.txt",
    "path": "dir-b/dir-z",
    "fullName": "tests/documents/dir-b/dir-z/file-dir-z.txt",
    "fullPath": "tests/documents/dir-b/dir-z"
  }
]

Table of contents

back to top


Methods

See: Options

back to top


Options

back to top


Full Samples

With this folder structure:

♦
└┄ ▼ tests
   └┄ ▼ documents
      ├┄ ▼ dir-a
      │  └┄ • file-dir-a.txt
      ├┄ ▼ dir-b
      │  ├┄ ▼ dir-x
      │  │  ├┄ ▼ dir-a
      │  │  │  └┄ • file-dir-a.txt
      │  │  ├┄ • 1file-dir-x.js
      │  │  └┄ • 2file-dir-x.txt
      │  └┄ • file-dir-b.txt
      └┄ • basePath.txt

Basic

var fp = require('files-path');
var files = fp.sync({
  basePath: 'tests/documents',
  path: 'dir-b/dir-x'
});
console.log(JSON.stringify(files, null, 2));
[
  {
    "name": "file-dir-b.txt",
    "path": "dir-b",
    "fullName": "tests\\documents\\dir-b\\file-dir-b.txt",
    "fullPath": "tests\\documents\\dir-b"
  },
  {
    "name": "1file-dir-x.js",
    "path": "dir-b\\dir-x",
    "fullName": "tests\\documents\\dir-b\\dir-x\\1file-dir-x.js",
    "fullPath": "tests\\documents\\dir-b\\dir-x"
  },
  {
    "name": "2file-dir-x.txt",
    "path": "dir-b\\dir-x",
    "fullName": "tests\\documents\\dir-b\\dir-x\\2file-dir-x.txt",
    "fullPath": "tests\\documents\\dir-b\\dir-x"
  }
]

Use basePath + Simple Filters (string, regex and callback)

var fp = require('files-path');
fp.sync({
  basePath: 'tests/documents',
  path: 'dir-b/dir-x',
  useBasePath: true,
  filters: [{
    pattern: '*.js',
    callback: function (file) {
      console.log('filter1: ' + file.name);
    }
  }, {
    pattern: /^[a-zA-Z\.]+$/g,
    callback: function (file) {
      console.log('filter2: ' + file.fullName);
    },
  }]
});
filter2: tests\documents\basePath.txt
filter1: 1file-dir-x.js

Use default options

var fp = require('files-path')({
  basePath: 'tests/documents'
});
var exec1 = fp.sync({
  path: 'dir-b/dir-x',
  filters: ['*.txt']
});
var exec2 = fp.sync({
  path: 'dir-a'
});
console.log(JSON.stringify(exec1, null, 2));
console.log('--');
console.log(JSON.stringify(exec2, null, 2));
[
  {
    "name": "file-dir-b.txt",
    "path": "dir-b",
    "fullName": "tests\\documents\\dir-b\\file-dir-b.txt",
    "fullPath": "tests\\documents\\dir-b"
  },
  {
    "name": "2file-dir-x.txt",
    "path": "dir-b\\dir-x",
    "fullName": "tests\\documents\\dir-b\\dir-x\\2file-dir-x.txt",
    "fullPath": "tests\\documents\\dir-b\\dir-x"
  }
]
--
[
  {
    "name": "file-dir-a.txt",
    "path": "dir-a",
    "fullName": "tests\\documents\\dir-a\\file-dir-a.txt",
    "fullPath": "tests\\documents\\dir-a"
  }
]

Basic Asyncronous with Callback

var fp = require('files-path');
var files = fp.async({
  basePath: 'tests/documents',
  path: 'dir-b/dir-x',
  filters: '*.txt'
}, function(err, files) {
  if (err) console.log(JSON.stringify(err, null, 2));
  console.log(JSON.stringify(files, null, 2));
});
[
  {
    "name": "file-dir-b.txt",
    "path": "dir-b",
    "fullName": "tests\\documents\\dir-b\\file-dir-b.txt",
    "fullPath": "tests\\documents\\dir-b"
  },
  {
    "name": "2file-dir-x.txt",
    "path": "dir-b\\dir-x",
    "fullName": "tests\\documents\\dir-b\\dir-x\\2file-dir-x.txt",
    "fullPath": "tests\\documents\\dir-b\\dir-x"
  }
]

Basic Asyncronous with Promise

var fp = require('files-path');
var files = fp.async({
  basePath: 'tests/documents',
  path: 'dir-b/dir-x',
  filters: '*.txt'
}).then(function(files) {
  console.log(JSON.stringify(files, null, 2));
}).catch(function(err) {
  console.log(JSON.stringify(err, null, 2))
});
[
  {
    "name": "file-dir-b.txt",
    "path": "dir-b",
    "fullName": "tests\\documents\\dir-b\\file-dir-b.txt",
    "fullPath": "tests\\documents\\dir-b"
  },
  {
    "name": "2file-dir-x.txt",
    "path": "dir-b\\dir-x",
    "fullName": "tests\\documents\\dir-b\\dir-x\\2file-dir-x.txt",
    "fullPath": "tests\\documents\\dir-b\\dir-x"
  }
]

back to top


Todo list

  • filter by function
  • make log.js to become an external npm package

License

Apache-2.0 © Flavio L Sousa ([email protected])