Codebase list node-read-file / 1951844
first commit jonschlinkert 10 years ago
15 changed file(s) with 420 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # Enforce Unix newlines
1 *.* text eol=lf
2 *.css text eol=lf
3 *.html text eol=lf
4 *.js text eol=lf
5 *.json text eol=lf
6 *.less text eol=lf
7 *.md text eol=lf
8 *.yml text eol=lf
9
10 *.jpg binary
11 *.gif binary
12 *.png binary
13 *.jpeg binary
0 # Numerous always-ignore extensions
1 *.csv
2 *.dat
3 *.diff
4 *.err
5 *.gz
6 *.log
7 *.orig
8 *.out
9 *.pid
10 *.rej
11 *.seed
12 *.swo
13 *.swp
14 *.vi
15 *.yo-rc.json
16 *.zip
17 *~
18 .ruby-version
19 lib-cov
20
21 # OS or Editor folders
22 *.esproj
23 *.sublime-project
24 *.sublime-workspace
25 ._*
26 .cache
27 .DS_Store
28 .idea
29 .project
30 .settings
31 .tmproj
32 nbproject
33 Thumbs.db
34
35 # Komodo
36 *.komodoproject
37 .komodotools
38
39 # grunt-html-validation
40 validation-status.json
41 validation-report.json
42
43 # Vendor packages
44 node_modules
45 bower_components
46 vendor
47
48 # General folders and files to ignore
49 _gh_pages
50 tmp
51 temp
52 TODO.md
0 {
1 "esnext": true,
2 "boss": true,
3 "curly": true,
4 "eqeqeq": true,
5 "eqnull": true,
6 "immed": true,
7 "latedef": true,
8 "newcap": true,
9 "noarg": true,
10 "node": true,
11 "sub": true,
12 "undef": true,
13 "unused": true,
14 "globals": {
15 "define": true,
16 "before": true,
17 "after": true,
18 "describe": true,
19 "it": true
20 }
21 }
0 Copyright (c) 2014 Jon Schlinkert, contributors.
1
2 Permission is hereby granted, free of charge, to any person
3 obtaining a copy of this software and associated documentation
4 files (the "Software"), to deal in the Software without
5 restriction, including without limitation the rights to use,
6 copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the
8 Software is furnished to do so, subject to the following
9 conditions:
10
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
0 # read-file [![NPM version](https://badge.fury.io/js/read-file.png)](http://badge.fury.io/js/read-file)
1
2 > Lightweight methods for reading from the file system, async and sync, with extras for stripping byte order marks and normalizing newlines.
3
4 ## Install
5 Install with [npm](npmjs.org):
6
7 ```bash
8 npm i read-file --save-dev
9 ```
10
11
12 ## api
13 #### readFile
14
15 Read files asynchronously.
16
17 ```js
18 var file = require('read-file');
19 file.readFileSync('foo.txt');
20 ```
21
22 #### readFileSync
23
24 Read files synchronously.
25
26 ```js
27 var file = require('read-file');
28 file.readFile('foo.txt', callback);
29 ```
30
31 #### encoding
32
33 Default encoding is `utf8`, this can be changed by passing an encoding as a second param:
34
35 ```js
36 var file = require('read-file');
37 file.readFileSync('foo.txt', 'utf8');
38 // or
39 file.readFile('foo.txt', 'utf8', callback);
40 ```
41
42 #### normalizeNL
43
44 Normalize all line endings to newlines, `\n`.
45
46 ```js
47 var file = require('read-file');
48 var str = file.readFileSync('foo.txt');
49 file.normalizeNL(str);
50 ```
51
52 #### stripBOM
53
54 Strip byte order marks.
55
56 ```js
57 var file = require('read-file');
58 var str = file.readFileSync('foo.txt');
59 file.stripBOM(str);
60 ```
61
62
63 ## Authors
64
65 **Jon Schlinkert**
66
67 + [github/jonschlinkert](https://github.com/jonschlinkert)
68 + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
69
70 **Brian Woodward**
71
72 + [github/doowb](https://github.com/doowb)
73 + [twitter/doowb](http://twitter.com/jonschlinkert)
74
75 ## License
76 Copyright (c) 2014 Jon Schlinkert, contributors.
77 Released under the MIT license
78
79 ***
80
81 _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 09, 2014._
0 # {%= name %} {%= badge("fury") %}
1
2 > {%= description %}
3
4 ## Install
5 {%= include("install") %}
6
7 ## api
8 {%= docs("api") %}
9
10 ## Authors
11 {%= contrib("authors") %}
12
13 ## License
14 {%= copyright() %}
15 {%= license() %}
16
17 ***
18
19 {%= include("footer") %}
0 ### readFile
1
2 Read files asynchronously.
3
4 ```js
5 var file = require('read-file');
6 file.readFileSync('foo.txt');
7 ```
8
9 ### readFileSync
10
11 Read files synchronously.
12
13 ```js
14 var file = require('read-file');
15 file.readFile('foo.txt', callback);
16 ```
17
18 ### encoding
19
20 Default encoding is `utf8`, this can be changed by passing an encoding as a second param:
21
22 ```js
23 var file = require('read-file');
24 file.readFileSync('foo.txt', 'utf8');
25 // or
26 file.readFile('foo.txt', 'utf8', callback);
27 ```
28
29 ### normalizeNL
30
31 Normalize all line endings to newlines, `\n`.
32
33 ```js
34 var file = require('read-file');
35 var str = file.readFileSync('foo.txt');
36 file.normalizeNL(str);
37 ```
38
39 ### stripBOM
40
41 Strip byte order marks.
42
43 ```js
44 var file = require('read-file');
45 var str = file.readFileSync('foo.txt');
46 file.stripBOM(str);
47 ```
0 /**
1 * read-file <https://github.com/assemble/read-file>
2 *
3 * Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors.
4 * Licensed under the MIT license.
5 */
6
7 const fs = require('graceful-fs');
8 const os = require('os');
9 const async = require('async');
10 const file = module.exports = {};
11
12 // Normalize to newlines
13 file.normalizeNL = function(str) {
14 return str.replace(/\r\n|\n/g, '\n');
15 };
16
17 file.preserveBOM = false;
18 file.stripBOM = function(str) {
19 // Transform EOL
20 var contents = (os.EOL === '\n') ? str : str.replace(os.EOL, '\n');
21 // Strip UTF BOM
22 if (!file.preserveBOM && contents.charCodeAt(0) === 0xFEFF) {
23 contents = contents.substring(1);
24 contents = contents.replace(/^\uFEFF/, '');
25 }
26 return contents;
27 };
28
29
30 // Read file synchronously
31 file.readFileSync = function(filepath, enc) {
32 enc = enc || 'utf8';
33 var buffer = fs.readFileSync(String(filepath), enc);
34 try {
35 return file.stripBOM(buffer);
36 } catch (err) {
37 err.message = 'Failed to read "' + filepath + '": ' + err.message;
38 throw err;
39 }
40 };
41
42
43 // Read file async
44 file.readFile = function (filepath, enc, callback) {
45 if (enc && typeof enc === 'function') {
46 callback = enc;
47 enc = 'utf8';
48 }
49
50 async.waterfall([
51
52 function (next) {
53 fs.readFile(String(filepath), (enc || 'utf8'), next);
54 }, function (contents, next) {
55 try {
56 next(null, file.stripBOM(contents));
57 } catch (err) {
58 err.message = 'Failed to read "' + filepath + '": ' + err.message;
59 next(err);
60 }
61 }
62 ],
63 callback);
64 };
0 {
1 "name": "read-file",
2 "description": "Lightweight methods for reading from the file system, async and sync, with extras for stripping byte order marks and normalizing newlines.",
3 "version": "0.1.0",
4 "homepage": "https://github.com/jonschlinkert/read-file",
5 "author": {
6 "name": "Jon Schlinkert",
7 "url": "https://github.com/jonschlinkert"
8 },
9 "repository": {
10 "type": "git",
11 "url": "git://github.com/jonschlinkert/read-file.git"
12 },
13 "bugs": {
14 "url": "https://github.com/jonschlinkert/read-file/issues"
15 },
16 "licenses": [
17 {
18 "type": "MIT",
19 "url": "https://github.com/jonschlinkert/read-file/blob/master/LICENSE-MIT"
20 }
21 ],
22 "keywords": [
23 "ascyn",
24 "bom",
25 "byte order mark",
26 "file system",
27 "file",
28 "fs",
29 "newline",
30 "normalize",
31 "read async",
32 "read file",
33 "read sync",
34 "read",
35 "return",
36 "sync"
37 ],
38 "main": "index.js",
39 "engines": {
40 "node": ">=0.8"
41 },
42 "scripts": {
43 "test": "mocha -R test"
44 },
45 "devDependencies": {
46 "verb": "~0.2.0",
47 "chai": "~1.9.1"
48 },
49 "dependencies": {
50 "graceful-fs": "~2.0.3",
51 "async": "~0.7.0"
52 }
53 }
0 {
1 "foo": {
2 "bar": "baz"
3 }
4 }
0 FILE CONTENTS!!!
0 foo:
1 bar: baz
0 --reporter progress
0 /**
1 * read-file <https://github.com/assemble/read-file>
2 *
3 * Copyright (c) 2014 Jon Schlinkert, contributors.
4 * Licensed under the MIT license.
5 */
6
7 const expect = require('chai').expect;
8 const path = require('path');
9 const file = require('../');
10
11 describe('file system methods', function () {
12
13 var testTxtFile = 'test/fixtures/test.txt';
14 var testTxtContents = 'FILE CONTENTS!!!';
15
16 it('should read the file', function () {
17 var expected = testTxtContents;
18 var actual = file.readFileSync(testTxtFile);
19 expect(actual).to.eql(expected);
20 });
21
22 it('should read the file (async)', function (done) {
23 var expected = testTxtContents;
24 file.readFile(testTxtFile, function (err, actual) {
25 expect(actual).to.eql(expected);
26 done();
27 });
28 });
29 });