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

fobject

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

fobject - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

311

lib/cached.js

@@ -1,199 +0,196 @@

// Generated by CoffeeScript 1.8.0
(function() {
var CachedFile, File, W,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
// Generated by CoffeeScript 1.10.0
var CachedFile, File, W,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
File = require('./');
File = require('./');
W = require('when');
W = require('when');
CachedFile = (function(_super) {
__extends(CachedFile, _super);
CachedFile = (function(superClass) {
extend(CachedFile, superClass);
/**
* The encoding of the file. Used as a default for `File.read` and
* `File.write`.
*/
/**
* The encoding of the file. Used as a default for `File.read` and
* `File.write`.
*/
CachedFile.prototype.encoding = void 0;
CachedFile.prototype.encoding = void 0;
/**
* The content of the file. Can be modified directly and if it is edited
File.save() will be needed to persist the changes on disk.
* @type {String}
*/
/**
* The content of the file. Can be modified directly and if it is edited
File.save() will be needed to persist the changes on disk.
* @type {String}
*/
CachedFile.prototype.content = '';
CachedFile.prototype.content = '';
/**
* The content loaded from the disk (cannot be modified because we need this
when saving to avoid needing to re-read the file).
* @type {String}
* @private
*/
/**
* The content loaded from the disk (cannot be modified because we need this
when saving to avoid needing to re-read the file).
* @type {String}
* @private
*/
CachedFile.prototype._savedContent = void 0;
CachedFile.prototype._savedContent = void 0;
/**
* The time at which the file was last loaded. Used to determine if we are
overwriting new data.
* @type {Integer}
* @private
*/
/**
* The time at which the file was last loaded. Used to determine if we are
overwriting new data.
* @type {Integer}
* @private
*/
CachedFile.prototype._loadTime = void 0;
CachedFile.prototype._loadTime = void 0;
/**
* @param {String|null} [options.encoding='utf8']
*/
/**
* @param {String|null} [options.encoding='utf8']
*/
function CachedFile(path, options) {
var _ref;
if (options == null) {
options = {};
}
this.encoding = (_ref = options.encoding) != null ? _ref : 'utf8';
CachedFile.__super__.constructor.call(this, path, options);
function CachedFile(path, options) {
var ref;
if (options == null) {
options = {};
}
this.encoding = (ref = options.encoding) != null ? ref : 'utf8';
CachedFile.__super__.constructor.call(this, path, options);
}
/**
* Read from the file
* @param {String} [options.flag='r']
* @return {Promise}
*/
/**
* Read from the file
* @param {String} [options.flag='r']
* @return {Promise}
*/
CachedFile.prototype.read = function(options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.read.call(this, options);
};
CachedFile.prototype.read = function(options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.read.call(this, options);
};
/**
* Write `data` to the file
* @param {String|Buffer} data
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
/**
* Write `data` to the file
* @param {String|Buffer} data
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
CachedFile.prototype.write = function(data, options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.write.call(this, data, options);
};
CachedFile.prototype.write = function(data, options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.write.call(this, data, options);
};
/**
* Append `data` to the file
* @param {String|Buffer} data
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
/**
* Append `data` to the file
* @param {String|Buffer} data
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
CachedFile.prototype.append = function(data, options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.append.call(this, data, options);
};
CachedFile.prototype.append = function(data, options) {
if (options == null) {
options = {};
}
options.encoding = this.encoding;
return CachedFile.__super__.append.call(this, data, options);
};
/**
* Check the file mod-time to see if the file has been edited since the last
time we loaded it. Also, make sure that we've loaded the file.
* @return {Promise}
*/
/**
* Check the file mod-time to see if the file has been edited since the last
time we loaded it. Also, make sure that we've loaded the file.
* @return {Promise}
*/
CachedFile.prototype._isFileNewerOnDisk = function() {
return this.stat().then((function(_this) {
return function(stat) {
return (_this._loadTime != null) && stat.mtime > _this._loadTime;
};
})(this));
};
CachedFile.prototype._isFileNewerOnDisk = function() {
return this.stat().then((function(_this) {
return function(stat) {
return (_this._loadTime != null) && stat.mtime > _this._loadTime;
};
})(this));
};
/**
* Write CachedFile.content to the disk, using optimizations like appending
rather than overwriting entirely.
* @param {Boolean} [overwrite=true] Even if the file has been modified since
the last load, overwrite it.
* @return {Promise}
*/
/**
* Write CachedFile.content to the disk, using optimizations like appending
rather than overwriting entirely.
* @param {Boolean} [overwrite=true] Even if the file has been modified since
the last load, overwrite it.
* @return {Promise}
*/
CachedFile.prototype.save = function(overwrite) {
var promise;
if (overwrite == null) {
overwrite = true;
}
promise = W.resolve();
if (!overwrite) {
promise.then(this._isFileNewerOnDisk).then(function(isFileNewerOnDisk) {
if (isFileNewerOnDisk) {
throw new Error('File has been modified since last load');
}
});
}
return promise.then((function(_this) {
return function() {
if ((_this._savedContent != null) && _this.content.slice(0, _this._savedContent.length) === _this._savedContent) {
if (!overwrite) {
return _this.append(_this.content.slice(_this._savedContent.length));
} else {
return _this._isFileNewerOnDisk().then(function(isFileNewerOnDisk) {
if (isFileNewerOnDisk) {
return _this.write(_this.content);
} else {
return _this.append(_this.content.slice(_this._savedContent.length));
}
});
}
CachedFile.prototype.save = function(overwrite) {
var promise;
if (overwrite == null) {
overwrite = true;
}
promise = W.resolve();
if (!overwrite) {
promise.then(this._isFileNewerOnDisk).then(function(isFileNewerOnDisk) {
if (isFileNewerOnDisk) {
throw new Error('File has been modified since last load');
}
});
}
return promise.then((function(_this) {
return function() {
if ((_this._savedContent != null) && _this.content.slice(0, _this._savedContent.length) === _this._savedContent) {
if (!overwrite) {
return _this.append(_this.content.slice(_this._savedContent.length));
} else {
return _this.write(_this.content);
return _this._isFileNewerOnDisk().then(function(isFileNewerOnDisk) {
if (isFileNewerOnDisk) {
return _this.write(_this.content);
} else {
return _this.append(_this.content.slice(_this._savedContent.length));
}
});
}
};
})(this)).then((function(_this) {
return function() {
_this._loadTime = Date.now();
_this._savedContent = _this.content;
};
})(this));
};
} else {
return _this.write(_this.content);
}
};
})(this)).then((function(_this) {
return function() {
_this._loadTime = Date.now();
_this._savedContent = _this.content;
};
})(this));
};
/**
* Load the file from the disk, overwriting anything in CachedFile.content
* @return {Promise}
*/
/**
* Load the file from the disk, overwriting anything in CachedFile.content
* @return {Promise}
*/
CachedFile.prototype.load = function() {
return this.read().then((function(_this) {
return function(data) {
var _loadTime;
_loadTime = Date.now();
return _this._savedContent = _this.content = data;
};
})(this));
};
CachedFile.prototype.load = function() {
return this.read().then((function(_this) {
return function(data) {
var _loadTime;
_loadTime = Date.now();
return _this._savedContent = _this.content = data;
};
})(this));
};
return CachedFile;
return CachedFile;
})(File);
})(File);
module.exports = CachedFile;
}).call(this);
module.exports = CachedFile;

@@ -1,223 +0,220 @@

// Generated by CoffeeScript 1.8.0
(function() {
var File, fs, nodefn, path, semver,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
// Generated by CoffeeScript 1.10.0
var File, fs, nodefn, path, semver,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
fs = require('graceful-fs');
fs = require('graceful-fs');
path = require('path');
path = require('path');
nodefn = require('when/node');
nodefn = require('when/node');
semver = require('semver');
semver = require('semver');
File = (function() {
File = (function() {
/**
* @class
* @name File
* @param {String} path The path to the file. This will be resolved to an
absolute path, so even if you change your cwd you can still access the same
file.
* @param {String} [options.base=./] Used for relative pathing. This will not
be resolved to an absolute path. Typically where a glob starts.
*/
function File(path, options) {
var _ref;
this.path = path;
if (options == null) {
options = {};
}
this._processOptionsObject = __bind(this._processOptionsObject, this);
this.dirname = __bind(this.dirname, this);
this.extname = __bind(this.extname, this);
this.stat = __bind(this.stat, this);
this.unlink = __bind(this.unlink, this);
this.rename = __bind(this.rename, this);
this.append = __bind(this.append, this);
this.write = __bind(this.write, this);
this.read = __bind(this.read, this);
this._resolvePaths = __bind(this._resolvePaths, this);
this.base = (_ref = options.base) != null ? _ref : './';
this._resolvePaths();
/**
* @class
* @name File
* @param {String} path The path to the file. This will be resolved to an
absolute path, so even if you change your cwd you can still access the same
file.
* @param {String} [options.base=./] Used for relative pathing. This will not
be resolved to an absolute path. Typically where a glob starts.
*/
function File(path1, options) {
var ref;
this.path = path1;
if (options == null) {
options = {};
}
this._processOptionsObject = bind(this._processOptionsObject, this);
this.dirname = bind(this.dirname, this);
this.extname = bind(this.extname, this);
this.stat = bind(this.stat, this);
this.unlink = bind(this.unlink, this);
this.rename = bind(this.rename, this);
this.append = bind(this.append, this);
this.write = bind(this.write, this);
this.read = bind(this.read, this);
this._resolvePaths = bind(this._resolvePaths, this);
this.base = (ref = options.base) != null ? ref : './';
this._resolvePaths();
}
/**
* Normalize & resolve paths. Call if the File.path changes
* @function
* @name _resolvePaths
* @private
*/
/**
* Normalize & resolve paths. Call if the File.path changes
* @function
* @name _resolvePaths
* @private
*/
File.prototype._resolvePaths = function() {
this.path = path.resolve(this.base, this.path);
return this.relative = path.relative(this.base, this.path);
};
File.prototype._resolvePaths = function() {
this.path = path.resolve(this.base, this.path);
return this.relative = path.relative(this.base, this.path);
};
/**
* Read from the file
* @function
* @name read
* @param {String|null} [options.encoding=null]
* @param {String} [options.flag='r']
* @return {Promise}
*/
/**
* Read from the file
* @function
* @name read
* @param {String|null} [options.encoding=null]
* @param {String} [options.flag='r']
* @return {Promise}
*/
File.prototype.read = function(options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.readFile, this.path, this._processOptionsObject(options));
};
File.prototype.read = function(options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.readFile, this.path, this._processOptionsObject(options));
};
/**
* Write `data` to the file
* @function
* @name write
* @param {String|Buffer} data
* @param {String|null} [options.encoding='utf8'] ignored if data is a
buffer
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
/**
* Write `data` to the file
* @function
* @name write
* @param {String|Buffer} data
* @param {String|null} [options.encoding='utf8'] ignored if data is a
buffer
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
File.prototype.write = function(data, options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.writeFile, this.path, data, this._processOptionsObject(options));
};
File.prototype.write = function(data, options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.writeFile, this.path, data, this._processOptionsObject(options));
};
/**
* Append `data` to the file
* @function
* @name append
* @param {String|Buffer} data
* @param {String|null} [options.encoding='utf8'] ignored if data is a
buffer
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
/**
* Append `data` to the file
* @function
* @name append
* @param {String|Buffer} data
* @param {String|null} [options.encoding='utf8'] ignored if data is a
buffer
* @param {Number} [options.mode=438] default is 0666 in Octal
* @param {String} [options.flag='w']
* @return {Promise}
*/
File.prototype.append = function(data, options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.appendFile, this.path, data, this._processOptionsObject(options));
};
File.prototype.append = function(data, options) {
if (options == null) {
options = {};
}
return nodefn.call(fs.appendFile, this.path, data, this._processOptionsObject(options));
};
/**
* Rename the file
* @function
* @name rename
* @param {String} newPath The new path for the file. Will be resolved
relative to File.base.
* @return {Promise}
*/
/**
* Rename the file
* @function
* @name rename
* @param {String} newPath The new path for the file. Will be resolved
relative to File.base.
* @return {Promise}
*/
File.prototype.rename = function(newPath) {
newPath = path.resolve(this.base, newPath);
return nodefn.call(fs.rename, this.path, newPath).then((function(_this) {
return function() {
_this.path = newPath;
return _this._resolvePaths();
};
})(this));
};
File.prototype.rename = function(newPath) {
newPath = path.resolve(this.base, newPath);
return nodefn.call(fs.rename, this.path, newPath).then((function(_this) {
return function() {
_this.path = newPath;
return _this._resolvePaths();
};
})(this));
};
/**
* Delete the file
* @function
* @name unlink
* @return {Promise}
*/
/**
* Delete the file
* @function
* @name unlink
* @return {Promise}
*/
File.prototype.unlink = function() {
return nodefn.call(fs.unlink, this.path);
};
File.prototype.unlink = function() {
return nodefn.call(fs.unlink, this.path);
};
/**
* Return a Stat object for the file
* @function
* @name stat
* @return {Promise}
*/
/**
* Return a Stat object for the file
* @function
* @name stat
* @return {Promise}
*/
File.prototype.stat = function() {
return nodefn.call(fs.stat, this.path);
};
File.prototype.stat = function() {
return nodefn.call(fs.stat, this.path);
};
/**
* Get the extension of a file
* @function
* @name extname
* @return {String}
*/
/**
* Get the extension of a file
* @function
* @name extname
* @return {String}
*/
File.prototype.extname = function() {
return path.extname(this.path);
};
File.prototype.extname = function() {
return path.extname(this.path);
};
/**
* Get the dirname of the file
* @function
* @name dirname
* @return {String}
*/
/**
* Get the dirname of the file
* @function
* @name dirname
* @return {String}
*/
File.prototype.dirname = function() {
return path.dirname(this.path);
};
File.prototype.dirname = function() {
return path.dirname(this.path);
};
/**
* Determine if we're using the new version of the FS API that supports an
options object.
* @return {Boolean} True if the version is >= 0.10.0 (when the options object
was introduced).
*/
/**
* Determine if we're using the new version of the FS API that supports an
options object.
* @return {Boolean} True if the version is >= 0.10.0 (when the options object
was introduced).
*/
File.prototype._isOptionsObjectSupported = function() {
return semver.gte(process.version, '0.10.0');
};
File.prototype._isOptionsObjectSupported = function() {
return semver.gte(process.version, '0.10.0');
};
/**
* The pre-v0.10.0 fs functions took a encoding parameter and no options
object. This function deals with that difference.
* @param {[type]} options [description]
*/
/**
* The pre-v0.10.0 fs functions took a encoding parameter and no options
object. This function deals with that difference.
* @param {[type]} options [description]
*/
File.prototype._processOptionsObject = function(options) {
var length, optionNames;
if (this._isOptionsObjectSupported()) {
return options;
File.prototype._processOptionsObject = function(options) {
var length, optionNames;
if (this._isOptionsObjectSupported()) {
return options;
} else {
optionNames = Object.keys(options);
length = optionNames.length;
if (length === 0 || (length === 1 && optionNames[0] === 'encoding')) {
return options.encoding;
} else {
optionNames = Object.keys(options);
length = optionNames.length;
if (length === 0 || (length === 1 && optionNames[0] === 'encoding')) {
return options.encoding;
} else {
throw new Error("Node version <= 0.10.0 only supports an encoding option. Called with " + (optionNames.join()));
}
throw new Error("Node version <= 0.10.0 only supports an encoding option. Called with " + (optionNames.join()));
}
};
}
};
return File;
return File;
})();
})();
module.exports = File;
}).call(this);
module.exports = File;
{
"name": "fobject",
"version": "0.0.3",
"version": "0.0.4",
"description": "A simple promise-based wrapper for file operations that treats files as objects.",

@@ -22,3 +22,3 @@ "main": "./lib",

"author": "Sean Lang <slang800@gmail.com>",
"license": "MIT",
"license": "GPL-3.0",
"bugs": {

@@ -29,12 +29,11 @@ "url": "https://github.com/slang800/fobject/issues"

"devDependencies": {
"coffee-script": "^1.7.1",
"docme": "^0.2.1",
"mocha": "^1.18.2",
"should": "^3.2.0"
"coffee-script": "^1.10.0",
"mocha": "^2.4.5",
"should": "^8.3.1"
},
"dependencies": {
"graceful-fs": "^3.0.2",
"semver": "^4.1.0",
"when": "^3.0.1"
"graceful-fs": "^4.1.3",
"semver": "^5.1.0",
"when": "^3.7.7"
}
}
# fobject
[![Build Status](https://travis-ci.org/slang800/fobject.svg?branch=master)](https://travis-ci.org/slang800/fobject)
[![Build Status](http://img.shields.io/travis/slang800/fobject.svg?style=flat-square)](https://travis-ci.org/slang800/fobject) [![NPM version](http://img.shields.io/npm/v/fobject.svg?style=flat-square)](https://www.npmjs.org/package/fobject) [![NPM license](http://img.shields.io/npm/l/fobject.svg?style=flat-square)](https://www.npmjs.org/package/fobject)

@@ -4,0 +4,0 @@ A simple promise-based wrapper for file operations that treats files as objects.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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