file-reader
Advanced tools
Comparing version 0.2.2 to 1.0.0
82
index.js
/*! | ||
* file-reader <https://github.com/jonschlinkert/file-reader> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Copyright (c) 2014-2015, Jon Schlinkert. | ||
* Licensed under the MIT license. | ||
@@ -10,21 +10,36 @@ */ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var fs = require('fs-utils'); | ||
var mapFiles = require('map-files'); | ||
var extend = require('extend-shallow'); | ||
var yaml = require('read-yaml'); | ||
module.exports = function(patterns, options) { | ||
/** | ||
* Expose `readFiles` | ||
*/ | ||
module.exports = readFiles; | ||
function readFiles(patterns, options) { | ||
return mapFiles(patterns, extend({ | ||
name: camelize, | ||
read: function(fp) { | ||
var ext = path.extname(fp); | ||
if (!reader.hasOwnProperty(ext)) { | ||
ext = '.txt'; | ||
} | ||
return reader[ext](path.resolve(fp)); | ||
} | ||
renameKey: camelize, | ||
read: readFile | ||
}, options)); | ||
}; | ||
} | ||
/** | ||
* Expose `readFile` | ||
*/ | ||
module.exports.file = readFile; | ||
function readFile(fp, options) { | ||
var ext = path.extname(fp); | ||
if (!reader.hasOwnProperty(ext)) { | ||
ext = '.txt'; | ||
} | ||
return reader[ext](path.resolve(fp), options); | ||
} | ||
/** | ||
* This is just a minimal start, pull requests welcome | ||
@@ -39,15 +54,28 @@ * for adding extensions/readers to the list. | ||
var reader = { | ||
// Functions | ||
'.js' : require, | ||
// requireable | ||
'.js': require, | ||
'.json': require, | ||
// Strings | ||
'.hbs' : fs.readFileSync, | ||
'.md' : fs.readFileSync, | ||
'.tmpl': fs.readFileSync, | ||
'.txt' : fs.readFileSync, | ||
// common string formats | ||
'.txt': readString, | ||
'.md': readString, | ||
'.markdown': readString, | ||
'.mdown': readString, | ||
// Objects | ||
'.json': require, | ||
'.yaml': fs.readYAMLSync, | ||
'.yml' : fs.readYAMLSync, | ||
'.hbs': readString, | ||
'.htm': readString, | ||
'.html': readString, | ||
'.slim': readString, | ||
'.swig': readString, | ||
'.tmpl': readString, | ||
'.css': readString, | ||
'.less': readString, | ||
'.sass': readString, | ||
'.scss': readString, | ||
'.styl': readString, | ||
// common object formats | ||
'.yaml': readYaml, | ||
'.yml': readYaml, | ||
}; | ||
@@ -75,1 +103,9 @@ | ||
} | ||
function readString(fp) { | ||
return fs.readFileSync(fp, 'utf8'); | ||
} | ||
function readYaml(fp, options) { | ||
return yaml.sync(fp); | ||
} |
{ | ||
"name": "file-reader", | ||
"description": "Read a glob of files, dynamically choosing the reader or requiring the files based on the file extension.", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/file-reader", | ||
@@ -17,8 +17,6 @@ "author": { | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/file-reader/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/file-reader/blob/master/LICENSE" | ||
}, | ||
"main": "index.js", | ||
@@ -31,6 +29,10 @@ "engines": { | ||
}, | ||
"dependencies": { | ||
"extend-shallow": "^0.2.0", | ||
"map-files": "^0.3.0", | ||
"read-yaml": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "^4.0.4", | ||
"verb": ">= 0.2.6" | ||
"should": "^4.6.1" | ||
}, | ||
@@ -53,8 +55,3 @@ "keywords": [ | ||
"yaml" | ||
], | ||
"dependencies": { | ||
"extend-shallow": "^0.1.1", | ||
"fs-utils": "^0.6.0", | ||
"map-files": "^0.2.0" | ||
} | ||
} | ||
] | ||
} |
@@ -5,30 +5,48 @@ # file-reader [![NPM version](https://badge.fury.io/js/file-reader.svg)](http://badge.fury.io/js/file-reader) | ||
## Install | ||
#### Install with [npm](npmjs.org): | ||
## Install with [npm](npmjs.org) | ||
```bash | ||
npm i file-reader --save-dev | ||
npm i file-reader --save | ||
``` | ||
## Run tests | ||
## Usage | ||
```bash | ||
npm test | ||
``` | ||
Read a glob of files: | ||
## Usage | ||
```js | ||
var reader = require('file-reader'); | ||
var read = require('file-reader'); | ||
console.log(reader('*.js')); | ||
read('*.js'); | ||
//=> { a: [Function: aaa], b: [Function: bbb], c: [Function: ccc] } | ||
console.log(reader('*.txt')); | ||
read('*.txt'); | ||
//=> { a: 'AAA', b: 'BBB', c: 'CCC' } | ||
console.log(reader('*.{yml,json}')); | ||
read('*.{yml,json}'); | ||
//=> { a: { a: 'a' }, b: { b: 'b' }, c: { c: 'c' } } | ||
``` | ||
Read a single file (you must supply the full file path, no glob patterns): | ||
```js | ||
var read = require('file-reader'); | ||
read.file('a.js'); | ||
//=> { a: [Function: foo] } | ||
read.file('a.txt'); | ||
//=> { a: 'foo' } | ||
read('a.yml'); | ||
//=> { a: { foo: 'bar' } } | ||
``` | ||
## Run tests | ||
Install dev dependencies: | ||
```bash | ||
npm i -d && npm test | ||
``` | ||
## Author | ||
@@ -42,3 +60,3 @@ | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert, contributors. | ||
Copyright (c) 2014-2015 Jon Schlinkert | ||
Released under the MIT license | ||
@@ -48,2 +66,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 02, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 21, 2015._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
0
64
5512
4
89
+ Addedread-yaml@^1.0.0
+ Addedextend-shallow@0.2.0(transitive)
+ Addedmap-files@0.3.0(transitive)
+ Addedread-yaml@1.1.0(transitive)
- Removedfs-utils@^0.6.0
- Removedansi-wrap@0.1.0(transitive)
- Removedansi-yellow@0.1.1(transitive)
- Removedasync@1.5.2(transitive)
- Removedasync-array-reduce@0.1.0(transitive)
- Removedbluebird@2.11.0(transitive)
- Removeddelete@0.2.1(transitive)
- Removedexpand-tilde@1.2.2(transitive)
- Removedextend-shallow@0.1.1(transitive)
- Removedfs-utils@0.6.5(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@5.0.157.2.3(transitive)
- Removedglobal-modules@0.2.3(transitive)
- Removedglobal-prefix@0.1.5(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhomedir-polyfill@1.0.3(transitive)
- Removedini@1.3.8(transitive)
- Removedis-absolute@0.2.6(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-relative@0.2.1(transitive)
- Removedis-unc-path@0.1.2(transitive)
- Removedis-valid-glob@0.3.0(transitive)
- Removedis-windows@0.2.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedkind-of@2.0.1(transitive)
- Removedlazy-cache@0.1.00.2.7(transitive)
- Removedmap-files@0.2.2(transitive)
- Removedmatched@0.3.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removednormalize-path@0.3.0(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedparse-passwd@1.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedresolve-dir@0.1.1(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedunc-path-regex@0.1.2(transitive)
- Removedwhich@1.3.1(transitive)
Updatedextend-shallow@^0.2.0
Updatedmap-files@^0.3.0