Giter Site home page Giter Site logo

leadcodedev / recursive-fs Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 115 KB

It is often difficult to retrieve and use files stored locally at your application, recursive-fs allows you to retrieve any file saved in a folder or sub-folder at a defined location.

License: MIT License

TypeScript 100.00%
recursive fs file systeme typescript javascript

recursive-fs's Introduction

Recursive-fs

Easily access your files in any folder or sub-folder.

Objective

It is often difficult to retrieve and use files stored locally at your application, fs-recursive allows you to retrieve any file saved in a folder or sub-folder at a defined location.

How to use

Install the module in your project via YARN

yarn add fs-recursive

Or NPM

npm install fs-recursive

The fs-recursive is very simple to use, you need to use the followed function :

fetch(
  directory: string,
  extentions: Array<string>,
  encode?: Buffer | string | undefined,
  callback?: (file: File) => void
): Promise<Map<string, File>>

fetchExpression (
  path: string,
  filenamePattern: RegExp,
  encode: Encode,
  excludes?: Array<string>,
  callback?: (file: File) => void
): Promise<Map<string, File>>

fetchSortedExpression (
  path: string,
  filenamePattern: RegExp,
  sortedExtensions: Array<string>,
  encode: Encode,
  excludes?: Array<string>,
  callback?: (file: File) => void
): Promise<Array<File>>

An example is always more telling

import { fetch } from 'fs-recursive';

const directory = path.join(process.cwd(), 'folder');
const extensions = ['ts', 'json'];

const files = await fetch(directory, extensions, 'utf-8'); // return Map<string, File>
console.log(files.entries().next().value[1]);

/**
 * Log the followed result
 *
 * File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   ๐Ÿ‘ˆ expredded in bytes
 * }
 */

You can get the same result using RegExp

import { fetchExpression } from 'fs-recursive';

const directory = path.join(process.cwd(), 'folder');
const expression = /.*\.ts$/;

const files = await fetchExpression(directory, expression, 'utf-8'); // return Map<string, File>
console.log(files.entries().next().value[1]);

/**
 * Log the followed result
 *
 * File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   ๐Ÿ‘ˆ expredded in bytes
 * }
 */

Then you can access to other data like this

const files = await fetch(directory, extensions, 'utf-8'); // return Map<string, File>
const file = files.entries().next().value[1];
const stats = await file.getStats();

console.log(stats);
/**
 * Stats {
 *   dev: 310369320,
 *   mode: 33206,
 *   nlink: 1,
 *   uid: 0,
 *   gid: 0,
 *   rdev: 0,
 *   blksize: 4096,
 *   ino: 562949956735823,
 *   size: 72,
 *   blocks: 0,
 *   atimeMs: 1618523067313.032,
 *   mtimeMs: 1618521064609.6714,
 *   ctimeMs: 1618521064609.6714,
 *   birthtimeMs: 1618521033264.188,
 *   atime: 2021-04-15T21:44:27.313Z,
 *   mtime: 2021-04-15T21:11:04.610Z,
 *   ctime: 2021-04-15T21:11:04.610Z,
 *   birthtime: 2021-04-15T21:10:33.264Z
 * }
 */

You can sort results by their extension

import { fetchSortedExpression } from 'fs-recursive';

const extensions = ['ts', 'js'];
const expression = /.*\.(ts|js)$/;
const files = await fetchSortedExpression(
	process.cwd(),
	expression,
	extensions,
	'utf-8'
);

console.log(files);

/**
 * Log the followed result
 *
 * [
 * 	File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   ๐Ÿ‘ˆ expredded in bytes
 *	},
 * 	File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.js',
 *   filename: 'File',
 *   extension: 'js',
 *   size: 1513   ๐Ÿ‘ˆ expredded in bytes
 * 	}
 * ]
 */

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.