Skip to content

Commit eea6ea5

Browse files
committed
v0.0.5 - split the index.js file, fix some bugs, add documentation
1 parent 0752618 commit eea6ea5

File tree

9 files changed

+636
-391
lines changed

9 files changed

+636
-391
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,140 @@
1-
node-file-pointer
2-
=================
1+
[file-pointer](https://github.com/DeadAlready/node-file-pointer) is a object wrapper to handle files.
2+
3+
# Installation
4+
5+
$ npm install file-pointer
6+
7+
# Essence
8+
9+
[file-pointer](https://github.com/DeadAlready/node-file-pointer) is meant to provide an object wrapper
10+
around files and folders for easier manipulation and handling. It does this by creating a number of non-enumberable
11+
properties that provide easy access to various manipulations with the underlying file.
12+
13+
# Usage
14+
15+
These are various examples of using the file-pointer library
16+
17+
var fp = require('file-pointer');
18+
19+
// Basic
20+
var obj = fp.create('./node_modules'); // returns Pointer
21+
22+
fp.create('./node_modules', function (err, obj) {
23+
// Obj is a Folder, since the stats were mapped and it was determined to be one
24+
});
25+
26+
// Direct calls
27+
28+
var file = new fp.File('./index.js');
29+
var folder = new fp.Folder('./node_modules');
30+
var pointer = new fp.Pointer('./test');
31+
32+
// use the previously created pointer to create a File
33+
var testFile = new fp.File({pointer: pointer});
34+
35+
// Create a Folder by defining opts.type
36+
var syncFolder = fp.create({filePath: './node_modules, type:'directory'});
37+
38+
# Added properties
39+
40+
The main objective of file-pointer is to add convenience methods to the objects for easy usage.
41+
These are either values or functions. All values are preceeded by a single underscore and all functions with two underscores.
42+
43+
## Pointer
44+
45+
The following properties are added to Pointer object and since File and Folder inherit from Pointer then
46+
they are accessible on all types.
47+
48+
* _parent - placeholder for possible parent folder
49+
* _path - full path to file
50+
* _name - filename
51+
* _base - file name without extension
52+
* _type - 'file','directory' or undefined
53+
* _ext - file extension
54+
* _stats - cached FSStat object or undefined
55+
56+
* __remove() - if parent is defined Folder, remove pointer from it
57+
* __delete(callback) - delete file from system
58+
* __stats([force], [callback]) - return _stats or ask for fs.stat
59+
* __listen(name, fn) - add an event listener
60+
* __emit(event, [arg1], [arg2], [...]) - emit an event with arguments
61+
* __startWatch() - open fs.watch and emit "change" and "error" events
62+
* __stopWatch() - close fs.watch
63+
64+
None of these properties are enumerable and only _type is configurable.
65+
This means that:
66+
67+
var fp = require('file-pointer');
68+
69+
var p = new fp.Pointer('./node_modules');
70+
71+
console.log(p); // Will log an apparently empty object {}
72+
console.log(p._name); // node_modules
73+
p._name = 'test';
74+
console.log(p._name); // node_modules
75+
76+
## File
77+
78+
These properties are added to the Pointer object properties.
79+
80+
* _type = 'file' // No longer configurable
81+
82+
* __read([callback]) - if a callback is provided utf8 content is returned, otherwise a readable stream
83+
* __write([content], [callback]) - if no arguments a writeable stream is returned,
84+
otherwise contents will be written to file and callback executed on completion
85+
* __delete([force],[callback]) - will delete the file from file system, force will determine if rimraf is used instead of fs.unlink
86+
87+
88+
## Folder
89+
90+
These properties are added to the Pointer object properties.
91+
92+
* _type = 'directory' // No longer configurable
93+
* _pointers - cached list of fs.readdir results or undefined
94+
95+
* __add(filePath, [content], [force], [callback]) - add a file or directory to the filesystem under current Folder,
96+
will use File.__create or Folder.__create depending on existance of content
97+
* __delete([force],[callback]) - delete folder from system, force will decide if rimraf or fs.rmdir is used
98+
* __list(callback) - use fs.readdir and return results
99+
* __removeChild(opts) - remove a child object either by property name or object
100+
101+
102+
## Public API
103+
104+
The file-pointer will expose the following methods of interest.
105+
106+
### create(opts, [callback])
107+
108+
Function for creating a file-pointer object
109+
110+
* opts - can be either string or object with the following properties
111+
** filePath: required
112+
** type: 'file' or 'directory'
113+
** stats: FSStat object
114+
** parent: parent object
115+
** watch: wheter to start watching the underlying file
116+
117+
* callback - if defined then fs.stat function is used to determine the type of the object
118+
119+
## License
120+
121+
The MIT License (MIT)
122+
Copyright (c) 2012 Karl Düüna
123+
124+
Permission is hereby granted, free of charge, to any person obtaining a copy of
125+
this software and associated documentation files (the "Software"), to deal in
126+
the Software without restriction, including without limitation the rights to
127+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
128+
the Software, and to permit persons to whom the Software is furnished to do so,
129+
subject to the following conditions:
130+
131+
The above copyright notice and this permission notice shall be included in all
132+
copies or substantial portions of the Software.
133+
134+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
135+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
136+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
137+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
138+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
139+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
140+
SOFTWARE.

lib/.DS_Store

6 KB
Binary file not shown.

lib/file.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var utils = require('./utils');
5+
var Pointer = require('./pointer');
6+
7+
function File(obj) {
8+
if (!(this instanceof File)) {
9+
return new File(obj);
10+
}
11+
12+
obj = utils.normalize(obj);
13+
14+
var self = this;
15+
var pointer = obj.pointer || new Pointer(obj);
16+
17+
// Copy properties from pointer
18+
Object.getOwnPropertyNames(pointer).forEach(function (prop) {
19+
Object.defineProperty(self, prop, Object.getOwnPropertyDescriptor(pointer, prop));
20+
});
21+
22+
Object.defineProperties(self, {
23+
// Overwrite type to file
24+
_type: {
25+
value: 'file'
26+
},
27+
// Function for reading file or creating readStream
28+
__read: {
29+
value: function (callback) {
30+
if (callback) {
31+
fs.readFile(self._path, 'utf8', callback);
32+
return;
33+
}
34+
return fs.createReadStream(self._path);
35+
}
36+
},
37+
// Function for writing to file or creating writeStream
38+
__write: {
39+
value: function (content, callback) {
40+
if (content === undefined && callback === undefined) {
41+
return fs.createWriteStream(self._path);
42+
}
43+
if (typeof content !== 'string') {
44+
throw new TypeError('Content must be string');
45+
}
46+
callback = callback || function (err) {
47+
if (err) {
48+
pointer.__emit('error', err);
49+
}
50+
};
51+
fs.writeFile(self._path, content, callback);
52+
}
53+
},
54+
// Function for deleting a file
55+
__delete: {
56+
value: function (force, callback) {
57+
if (force instanceof Function) {
58+
callback = force;
59+
force = false;
60+
}
61+
62+
pointer.__stopWatch();
63+
64+
callback = callback || function (err) {
65+
if (err) {
66+
pointer.__emit('error', err);
67+
}
68+
};
69+
70+
function end(err) {
71+
if (err) {
72+
callback(err);
73+
return;
74+
}
75+
callback(null, self.__remove());
76+
}
77+
78+
var func = force ? rimraf : fs.unlink;
79+
func(self._path, end);
80+
}
81+
}
82+
});
83+
}
84+
85+
// Define a method for creating a file in the filesystem
86+
Object.defineProperty(File, '__create', {
87+
value: function(filePath, content, force, callback) {
88+
if(typeof force === 'function') {
89+
callback = force;
90+
force = false;
91+
}
92+
93+
var opts = utils.normalize(filePath);
94+
95+
function createFile(again) {
96+
fs.writeFile(opts.filePath, content, 'utf8', function(err){
97+
if(err) {
98+
if(!again) {
99+
callback(err);
100+
return;
101+
}
102+
Folder.__create(path.dirname(opts.filePath), true, function (err) {
103+
if(err) {
104+
callback(err);
105+
return;
106+
}
107+
createFile(false);
108+
});
109+
return;
110+
}
111+
var f = new File(opts);
112+
callback(null, f);
113+
});
114+
}
115+
116+
createFile(force);
117+
}
118+
});
119+
120+
module.exports = File;

0 commit comments

Comments
 (0)