|
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. |
0 commit comments