Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dir-requirer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dir-requirer

require指定目录下的所有文件,返回与目录结构相同的对象

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

dir-requirer

Build Status Coverage Status

用途

dir-requirer是一个对nodejs自身的require方法的封装,使用它可以直接require一个目录,它会帮助你require指定目录下的 所有文件,并且返回一个与目录结构相同的对象,目录中被require的文件名即对象的方法名。

比如在当前目录下的dir文件夹的结构如下:

					  |-- inner2 -- d.json
		 |-- inner -- |
		 |            |-- e.js
		 |-- a.js
 dir --  |-- b.js
         |-- c.json
var dr = require('dir-requirer')(__dirname);
var myDir = dr('./dir');
myDir.inner.e(); //调用e.js中exports出的方法/对象,相当于myDir.inner.e = require('./dir/inner/e');
cJson = myDir.c;//也可以调用json文件

安装

通过NPM即可安装使用:

$ npm install dir-requirer

调用:

var dr = require('dir-requirer')(__dirname);

使用

比如在当前目录下的dir文件夹的结构如下:

					  |-- inner2 -- d.json
		 |-- inner -- |
		 |            |-- e.js
		 |-- a.js
 dir --  |-- b.js
         |-- c.json

dr(path,options)

参数:

  • path(String) - 指定要导入的路径
  • options(Object) - 可选的配置选项

options可选属性:

  • ext(Array) - 只导入指定扩展名的文件,如['.json'],默认为['.js','.json','.node']
  • dirBlackList(Array) - 不导入指定目录名下的所有文件,如['inner2'],默认为[]
  • fileBlackList(Array) - 不导入指定文件,如['e.js'],默认为[]

例子:

var dr = require('dir-requirer')(__dirname);
var myDir = dr('./dir')
//myDir对象将会拥有与'./dir'目录相同的结构,可以直接调用 myDir.a (即a.js文件exports出的对象/方法) , 
//myDir.c , myDir.inner.e , myDir.inner.inner2.d 
var myDir = dr('./dir',{ext:['.json']});
//将只会require目录下的所有json文件,如 myDir.inner.inner2.d , myDir.c 
var myDir = dr('./dir',{dirBlackList:['inner2']});
//将不会require在inner2目录下的所有文件(包括子目录)
var myDir = dr('./dir',{dirBlackList:['inner'],fileBlackList:['c.json']});
//将不会require在inner目录下的所有文件(包括子目录),以及名为c.json的单独文件

Keywords

FAQs

Package last updated on 09 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc