Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "fs-utils", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main": [ | ||
@@ -5,0 +5,0 @@ "index.js" |
200
index.js
@@ -10,22 +10,27 @@ /** | ||
// Node.js | ||
var fs = require('graceful-fs'); | ||
var path = require('path'); | ||
var os = require('os'); | ||
var fs = require('graceful-fs'); | ||
var path = require('path'); | ||
var os = require('os'); | ||
// node_modules | ||
var paths = require('path-utils'); | ||
var async = require('async'); | ||
var rimraf = require('rimraf'); | ||
var glob = require('globule'); | ||
var YAML = require('js-yaml'); | ||
var _ = require('lodash'); | ||
var paths = require('path-utils'); | ||
var async = require('async'); | ||
var rimraf = require('rimraf'); | ||
var glob = require('globule'); | ||
var YAML = require('js-yaml'); | ||
var _ = require('lodash'); | ||
// Export the `file` object | ||
var file = module.exports = {}; | ||
/** | ||
* File utils | ||
*/ | ||
// Export the `_fs` object | ||
var _fs = module.exports = {}; | ||
// Build regex based on os EOL | ||
file.EOLre = new RegExp(os.EOL, 'g'); | ||
_fs.EOLre = new RegExp(os.EOL, 'g'); | ||
// Normalize line endings | ||
file.normalizeEOL = function(str) { | ||
_fs.normalizeEOL = function(str) { | ||
return str.replace(/\r\n|\n/g, os.EOL); | ||
@@ -35,13 +40,12 @@ }; | ||
// Normalize to newlines | ||
file.normalizeNL = function(str) { | ||
_fs.normalizeNL = function(str) { | ||
return str.replace(/\r\n|\n/g, '\n'); | ||
}; | ||
file.arrayify = function(val) { | ||
_fs.arrayify = function(val) { | ||
return !Array.isArray(val) ? [val] : val; | ||
}; | ||
// Default encoding | ||
file.encoding = function(options) { | ||
_fs.encoding = function(options) { | ||
options = options || {}; | ||
@@ -51,8 +55,8 @@ return options.encoding || 'utf8'; | ||
file.preserveBOM = false; | ||
file.stripBOM = function(str) { | ||
_fs.preserveBOM = false; | ||
_fs.stripBOM = function(str) { | ||
// Transform EOL | ||
var contents = (os.EOL === '\n') ? str : str.replace(file.EOLre, '\n'); | ||
var contents = (os.EOL === '\n') ? str : str.replace(_fs.EOLre, '\n'); | ||
// Strip UTF BOM | ||
if (!file.preserveBOM && contents.charCodeAt(0) === 0xFEFF) { | ||
if (!_fs.preserveBOM && contents.charCodeAt(0) === 0xFEFF) { | ||
contents = contents.substring(1); | ||
@@ -64,12 +68,13 @@ contents = contents.replace(/^\uFEFF/, ''); | ||
/** | ||
* Read Files | ||
* Read | ||
*/ | ||
// Read file synchronously | ||
file.readFileSync = function(filepath, options) { | ||
_fs.readFileSync = function(filepath, options) { | ||
options = options || {}; | ||
var buffer = fs.readFileSync(String(filepath), file.encoding(options)); | ||
var buffer = fs.readFileSync(String(filepath), _fs.encoding(options)); | ||
try { | ||
return file.stripBOM(buffer); | ||
return _fs.stripBOM(buffer); | ||
} catch (err) { | ||
@@ -82,4 +87,4 @@ err.message = 'Failed to read "' + filepath + '": ' + err.message; | ||
// Read JSON file synchronously and parse content as JSON | ||
file.readJSONSync = function(filepath) { | ||
var buffer = file.readFileSync(filepath); | ||
_fs.readJSONSync = function(filepath) { | ||
var buffer = _fs.readFileSync(filepath); | ||
try { | ||
@@ -94,4 +99,4 @@ return JSON.parse(buffer); | ||
// Read YAML file synchronously and parse content as JSON | ||
file.readYAMLSync = function(filepath) { | ||
var buffer = file.readFileSync(filepath); | ||
_fs.readYAMLSync = function(filepath) { | ||
var buffer = _fs.readFileSync(filepath); | ||
try { | ||
@@ -105,8 +110,8 @@ return YAML.load(buffer); | ||
// Read optional JSON. Ben Alman, https://gist.github.com/2876125 | ||
file.readOptionalJSON = function(filepath) { | ||
// Read optional JSON | ||
// Ben Alman, https://gist.github.com/2876125 | ||
_fs.readOptionalJSON = function(filepath) { | ||
var buffer = {}; | ||
try { | ||
buffer = file.readJSONSync(filepath); | ||
buffer = _fs.readJSONSync(filepath); | ||
} catch (e) {} | ||
@@ -116,6 +121,6 @@ return buffer; | ||
file.readOptionalYAML = function(filepath) { | ||
_fs.readOptionalYAML = function(filepath) { | ||
var buffer = {}; | ||
try { | ||
buffer = file.readYAMLSync(filepath); | ||
buffer = _fs.readYAMLSync(filepath); | ||
} catch (e) {} | ||
@@ -125,19 +130,14 @@ return buffer; | ||
/** | ||
* Data file reader factory | ||
* Automatically determines the reader based on extension. | ||
* Use instead of grunt.file.readJSON or grunt.file.readYAML | ||
*/ | ||
file.readDataSync = function(filepath, options) { | ||
// Determine the reader based on extension. | ||
_fs.readDataSync = function(filepath, options) { | ||
options = options || {}; | ||
var ext = path.extname(filepath); | ||
var reader = file.readJSONSync; | ||
var reader = _fs.readJSONSync; | ||
switch(ext) { | ||
case '.json': | ||
reader = file.readJSONSync; | ||
reader = _fs.readJSONSync; | ||
break; | ||
case '.yml': | ||
case '.yaml': | ||
reader = file.readYAMLSync; | ||
reader = _fs.readYAMLSync; | ||
break; | ||
@@ -149,3 +149,8 @@ } | ||
/** | ||
* Expand (glob / minimatch) | ||
*/ | ||
/** | ||
* @param {String} filepath The filepath to read or string pattern to expand then read | ||
@@ -156,3 +161,3 @@ * @param {Object} options Object of options. 'namespace' will use the basename of | ||
*/ | ||
file.expandDataFiles = function (filepath, options) { | ||
_fs.expandDataFiles = function (filepath, options) { | ||
var opts = _.extend({}, options); | ||
@@ -164,3 +169,3 @@ opts.data = opts.data || {}; | ||
var name = paths.basename(fp); | ||
if (file.isEmptyFile(fp)) { | ||
if (_fs.isEmptyFile(fp)) { | ||
if(opts.verbose) {console.warn('Skipping empty file:'.yellow, fp);} | ||
@@ -173,6 +178,6 @@ } else { | ||
// If it can't be required, try to read it directly. | ||
contents = file.readDataSync(fp); | ||
contents = _fs.readDataSync(fp); | ||
} | ||
if(opts.namespace === true) { | ||
// Extend the data into an object named for the file. | ||
// Extend the data into an object named for the _fs. | ||
opts.data[name] = _.cloneDeep(_.extend(opts.data, contents)); | ||
@@ -189,3 +194,9 @@ } else if(opts.namespace === 'only') { | ||
file.getStatsSync = function (filepath) { | ||
/** | ||
* fs stats | ||
*/ | ||
_fs.getStatsSync = function (filepath) { | ||
try { | ||
@@ -199,3 +210,3 @@ return fs.statSync(filepath); | ||
file.getStats = function (filepath, callback) { | ||
_fs.getStats = function (filepath, callback) { | ||
try { | ||
@@ -211,3 +222,3 @@ return fs.stat(filepath, callback); | ||
// multiple matches are found, only the first is returned | ||
file.getFile = function(filepath, options) { | ||
_fs.getFile = function(filepath, options) { | ||
var str = glob.find(filepath, options)[0]; | ||
@@ -217,2 +228,4 @@ return str ? String(str) : null; | ||
/** | ||
@@ -222,3 +235,3 @@ * Make directories | ||
file.mkdir = function (dest, callback) { | ||
_fs.mkdir = function (dest, callback) { | ||
var destpath = path.dirname(dest); | ||
@@ -229,3 +242,3 @@ fs.exists(destpath, function (exist) { | ||
} else { | ||
file.mkdir(destpath, function () { | ||
_fs.mkdir(destpath, function () { | ||
fs.mkdir(dest, callback); | ||
@@ -239,3 +252,3 @@ }); | ||
// don't already exist | ||
file.mkdirSync = function (dirpath, mode) { | ||
_fs.mkdirSync = function (dirpath, mode) { | ||
mode = mode || parseInt('0777', 8) & (~process.umask()); | ||
@@ -247,3 +260,3 @@ if (!fs.existsSync(dirpath)) { | ||
} else { | ||
file.mkdirSync(parentDir); | ||
_fs.mkdirSync(parentDir); | ||
fs.mkdirSync(dirpath, mode); | ||
@@ -256,3 +269,3 @@ } | ||
// built-in mkdir functions. | ||
file.mkdirp = function (dir) { | ||
_fs.mkdirp = function (dir) { | ||
require('mkdirp')(dir, function (err) { | ||
@@ -262,3 +275,3 @@ if (err) {console.error(err); } | ||
}; | ||
file.mkdirpSync = function (dir) { | ||
_fs.mkdirpSync = function (dir) { | ||
require('mkdirp').sync(dir); | ||
@@ -268,2 +281,3 @@ }; | ||
/** | ||
@@ -273,3 +287,3 @@ * Write | ||
file.writeFile = function (dest, content, callback) { | ||
_fs.writeFile = function (dest, content, callback) { | ||
var destpath = path.dirname(dest); | ||
@@ -280,3 +294,3 @@ fs.exists(destpath, function (exists) { | ||
} else { | ||
file.mkdir(destpath, function () { | ||
_fs.mkdir(destpath, function () { | ||
fs.writeFile(dest, content, callback); | ||
@@ -289,37 +303,37 @@ }); | ||
// Write files to disk, synchronously | ||
file.writeFileSync = function(dest, content, options) { | ||
_fs.writeFileSync = function(dest, content, options) { | ||
options = options || {}; | ||
var dirpath = path.dirname(dest); | ||
if (!file.exists(dirpath)) { | ||
file.mkdirSync(dirpath); | ||
if (!_fs.exists(dirpath)) { | ||
_fs.mkdirSync(dirpath); | ||
} | ||
fs.writeFileSync(dest, content, file.encoding(options)); | ||
fs.writeFileSync(dest, content, _fs.encoding(options)); | ||
}; | ||
file.writeJSONSync = function(dest, content, options) { | ||
_fs.writeJSONSync = function(dest, content, options) { | ||
options = options || {}; | ||
options.indent = options.indent || 2; | ||
content = JSON.stringify(content, null, options.indent); | ||
file.writeFileSync(dest, content); | ||
_fs.writeFileSync(dest, content); | ||
}; | ||
file.writeYAMLSync = function(dest, content, options) { | ||
_fs.writeYAMLSync = function(dest, content, options) { | ||
options = options || {}; | ||
options.indent = options.indent || 2; | ||
content = YAML.dump(content, null, options.indent); | ||
file.writeFileSync(dest, content); | ||
_fs.writeFileSync(dest, content); | ||
}; | ||
// @example: file.writeDataSync('foo.yaml', {name: "Foo"}); | ||
file.writeDataSync = function(dest, content, options) { | ||
// @example: _fs.writeDataSync('foo.yml', {foo: "bar"}); | ||
_fs.writeDataSync = function(dest, content, options) { | ||
options = options || {}; | ||
var ext = options.ext || path.extname(dest); | ||
var writer = file.writeJSONSync; | ||
var writer = _fs.writeJSONSync; | ||
switch(ext) { | ||
case '.json': | ||
writer = file.writeJSONSync; | ||
writer = _fs.writeJSONSync; | ||
break; | ||
case '.yml': | ||
case '.yaml': | ||
writer = file.writeYAMLSync; | ||
writer = _fs.writeYAMLSync; | ||
break; | ||
@@ -331,2 +345,3 @@ } | ||
/** | ||
@@ -337,9 +352,9 @@ * Copy | ||
// Copy files synchronously and process any templates within | ||
file.copyFileSync = function (src, dest, options) { | ||
_fs.copyFileSync = function (src, dest, options) { | ||
var opts = _.extend({}, {process: true}, options || {}); | ||
src = file.readFileSync(src); | ||
src = _fs.readFileSync(src); | ||
if(opts.process === true) { | ||
src = template.process(src, opts.data, opts); | ||
} | ||
file.writeFileSync(dest, src, opts); | ||
_fs.writeFileSync(dest, src, opts); | ||
}; | ||
@@ -354,3 +369,3 @@ | ||
// Remove any directories and child directories that exist | ||
file.rmdirSync = function () { | ||
_fs.rmdirSync = function () { | ||
var dirpath = path.join.apply(path, arguments); | ||
@@ -364,3 +379,3 @@ if (fs.existsSync(dirpath)) { | ||
} else if (fs.statSync(filepath).isDirectory()) { | ||
file.rmdirSync(filepath); | ||
_fs.rmdirSync(filepath); | ||
} else { | ||
@@ -374,3 +389,3 @@ fs.unlinkSync(filepath); | ||
// file.delete was sourced and modified from grunt.file | ||
// _fs.delete was sourced and modified from grunt.file | ||
// https://github.com/gruntjs/grunt/blob/master/lib/grunt/file.js | ||
@@ -380,7 +395,7 @@ // https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT | ||
// Delete folders and files recursively | ||
file.delete = function(filepath, options) { | ||
_fs.delete = function(filepath, options) { | ||
filepath = String(filepath); | ||
options = options || {}; | ||
if (!file.exists(filepath)) { | ||
if (!_fs.exists(filepath)) { | ||
console.warn('Cannot delete nonexistent file.'); | ||
@@ -408,3 +423,3 @@ return false; | ||
file.rmdir = function (dirpath, callback) { | ||
_fs.rmdir = function (dirpath, callback) { | ||
if (!_.isFunction(callback)) {callback = function () {};} | ||
@@ -435,3 +450,3 @@ fs.readdir(dirpath, function (err, files) { | ||
// Does the filepath actually exist? | ||
file.exists = function() { | ||
_fs.exists = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
@@ -442,8 +457,21 @@ return fs.existsSync(filepath); | ||
// If the file actually exists, does it have any content? | ||
file.isEmptyFile = function() { | ||
_fs.isEmptyFile = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if (!file.exists(filepath)) {return false;} | ||
filepath = file.readFileSync(filepath); | ||
if (!_fs.exists(filepath)) {return false;} | ||
filepath = _fs.readFileSync(filepath); | ||
return (filepath.length === 0 || filepath === '') ? true : false; | ||
}; | ||
// Is the path a directory? | ||
_fs.isDir = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if (!fs.existsSync(filepath)) {return false;} | ||
return fs.statSync(filepath).isDirectory(); | ||
}; | ||
// Is the path a file? | ||
_fs.isFile = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if (!fs.existsSync(filepath)) {return false;} | ||
return fs.statSync(filepath).isFile(); | ||
}; |
{ | ||
"name": "fs-utils", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "File system extras and utilities to extend the Node.js fs module.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -0,0 +0,0 @@ # fs-utils [![NPM version](https://badge.fury.io/js/fs-utils.png)](http://badge.fury.io/js/fs-utils) |
@@ -32,12 +32,15 @@ /** | ||
it('should return true the path exists', function () { | ||
var expected = true; | ||
var actual = file.exists(testTxtPath); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should return false the path does not exist', function () { | ||
var expected = false; | ||
var actual = file.exists('.', 'some', 'random', 'file.json'); | ||
expect(actual).to.eql(expected); | ||
describe('file system methods', function () { | ||
it('should return true the path exists', function () { | ||
var expected = true; | ||
var actual = file.exists(testTxtPath); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should return false the path does not exist', function () { | ||
var expected = false; | ||
var actual = file.exists('.', 'some', 'random', 'file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
@@ -44,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
20264
538