Comparing version 0.0.0 to 0.0.1
129
lib/index.js
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var File, fs, nodefn; | ||
var File, fs, nodefn, path; | ||
fs = require('fs'); | ||
path = require('path'); | ||
nodefn = require('when/node'); | ||
File = (function() { | ||
function File(path) { | ||
/** | ||
* @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} [opts.base=./] Used for relative pathing. This will not be | ||
resolved to an absolute path. Typically where a glob starts. | ||
*/ | ||
function File(path, opts) { | ||
var _ref; | ||
this.path = path; | ||
if (opts == null) { | ||
opts = {}; | ||
} | ||
this.base = (_ref = opts.base) != null ? _ref : './'; | ||
this._resolvePaths(); | ||
} | ||
@@ -16,4 +32,15 @@ | ||
/** | ||
* Normalize & resolve paths. Call if the File.path changes | ||
* @private | ||
*/ | ||
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 | ||
* @param {String|null} [options.encoding=null] ignored if data is a buffer | ||
* @param {String|null} [options.encoding=null] | ||
* @param {String} [options.flag='r'] | ||
@@ -66,2 +93,60 @@ * @return {Promise} | ||
/** | ||
* Rename the file | ||
* @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)); | ||
}; | ||
/** | ||
* Delete the file | ||
* @return {Promise} | ||
*/ | ||
File.prototype.unlink = function() { | ||
return nodefn.call(fs.unlink, this.path); | ||
}; | ||
/** | ||
* Return a Stat object for the file | ||
* @return {Promise} | ||
*/ | ||
File.prototype.stat = function() { | ||
return nodefn.call(fs.stat, this.path); | ||
}; | ||
/** | ||
* Get the extension of a file | ||
* @return {String} | ||
*/ | ||
File.prototype.extname = function() { | ||
return path.extname(this.path); | ||
}; | ||
/** | ||
* Get the dirname of the file | ||
* @return {String} | ||
*/ | ||
File.prototype.dirname = function() { | ||
return path.dirname(this.path); | ||
}; | ||
return File; | ||
@@ -73,40 +158,2 @@ | ||
/* | ||
rename(oldPath, newPath, callback) | ||
ftruncate(fd, len, callback) | ||
truncate(path, len, callback) | ||
chown(path, uid, gid, callback) | ||
fchown(fd, uid, gid, callback) | ||
lchown(path, uid, gid, callback) | ||
chmod(path, mode, callback) | ||
fchmod(fd, mode, callback) | ||
lchmod(path, mode, callback) | ||
stat(path, callback) | ||
lstat(path, callback) | ||
fstat(fd, callback) | ||
link(srcpath, dstpath, callback) | ||
symlink(srcpath, dstpath, [type], callback) | ||
readlink(path, callback) | ||
realpath(path, [cache], callback) | ||
unlink(path, callback) | ||
rmdir(path, callback) | ||
mkdir(path, [mode], callback) | ||
readdir(path, callback) | ||
close(fd, callback) | ||
open(path, flags, [mode], callback) | ||
utimes(path, atime, mtime, callback) | ||
futimes(fd, atime, mtime, callback) | ||
write(fd, buffer, offset, length, position, callback) | ||
read(fd, buffer, offset, length, position, callback) | ||
readFile(path, [options], callback) | ||
writeFile(path, data, [options], callback) | ||
appendFile(path, data, [options], callback) | ||
watchFile(path, [options], listener) | ||
unwatchFile(path, [listener]) | ||
watch(path, [options], [listener]) | ||
*/ | ||
}).call(this); |
{ | ||
"name": "fobject", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "A simple promise-based wrapper for file operations that treats files as objects.", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -8,9 +8,25 @@ # fobject | ||
File = require 'fobject' | ||
configFile = new File('./config.json') | ||
configFile.read().done( | ||
(data) -> | ||
console.log "contents of #{configFile.path}: #{data}" | ||
(error) -> | ||
console.log "something went wrong: #{error}" | ||
configFile = new File('config.json') | ||
configFile.read().done((data) -> | ||
console.log "contents of #{configFile.path}: #{data}" | ||
) | ||
``` | ||
Also, this includes a 2nd wrapper that extends the first and lets you cache the contents of the file directly in the object | ||
```coffee | ||
File = require 'fobject/cached' | ||
logFile = new File('log') | ||
logFile.load().then( -> | ||
console.log logFile.content # print out the logs | ||
logFile.content += 'this is a demo\n' # add a line | ||
).then( | ||
logFile.save | ||
).done( -> | ||
console.log('the logs are saved, and since we only added to the string, the | ||
`File.save()` call was optimized into a single `append()`') | ||
) | ||
``` | ||
## docs | ||
Read the source code for now... it's all well annotated in JSDoc. |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
15503
228
32
9
1