UNPKG

3.08 kBJavaScriptView Raw
1var FS = require('fs');
2var Path = require('path');
3
4var io = module.exports = function (path) {
5
6 this.path = path;
7
8 // TODO: define setter for path
9
10 this.name = Path.basename (path);
11
12 this.extension = Path.extname (path).substr (1);
13
14};
15
16io.prototype.relative = function (relPath) {
17 return Path.relative (this.path, relPath instanceof io ? relPath.path : relPath);
18};
19
20
21io.prototype.isFile = function () {
22 return this.stats ? this.stats.isFile () : null;
23};
24
25io.prototype.isDirectory = function () {
26 return this.stats ? this.stats.isDirectory () : null;
27};
28
29io.prototype.fileIO = io.prototype.file_io = function () {
30 var path = Path.join.apply(Path, arguments);
31 return new io(Path.resolve(this.path, path));
32};
33
34io.prototype.chmod = function (mode, cb) {
35 var p = this.path;
36 FS.chmod (p, mode, function (err) {
37 cb (err);
38 });
39};
40
41io.prototype.mkdir = function (mode, callback) {
42 if ("function" === typeof mode && callback === undefined) {
43 callback = mode;
44 mode = 0777; // node defaults
45 }
46 return FS.mkdir (this.path, mode, callback && callback.bind (this));
47};
48
49
50io.prototype.writeStream = function (options) {
51 return FS.createWriteStream (this.path, options);
52};
53
54io.prototype.readStream = function (options, cb) {
55
56 if (arguments.length == 1)
57 cb = arguments[0];
58
59 var self = this;
60
61 this.stat (function (err, stats) {
62
63 var readStream = null;
64
65 if (!err && stats.isFile()) {
66 readStream = FS.createReadStream (this.path, options);
67 readStream.pause();
68 }
69
70 cb.call (self, readStream, stats);
71 });
72};
73
74io.prototype.scanTree = function (cb) {
75 var self = this;
76
77 FS.readdir (this.path, function (err, files) {
78 // console.log (err, files);
79 for (var i = 0; i < files.length; i++) {
80
81 var f = files[i] = new io (Path.join (self.path, files[i]));
82
83 f.stat (self.scanSubTree, cb);
84 }
85 });
86};
87
88io.prototype.findUp = function (fileName, cb, errCb) {
89 var self = this;
90
91 if (!cb || cb.constructor != Function)
92 return;
93
94 var fileIO = this.fileIO (fileName);
95 fileIO.stat (function (err, stats) {
96 if (!err) {
97 var result = cb (this, stats);
98 if (result)
99 return;
100 }
101 if (self.parent().path == self.path) {
102 errCb ();
103 return;
104 }
105
106 self.parent().findUp(fileName, cb, errCb);
107 });
108};
109
110io.prototype.scanSubTree = function (err, stats, cb) {
111 var scanFurther = 0;
112 if (cb)
113 scanFurther = cb (this);
114// console.log (scanFurther, this.isDirectory ());
115 if (scanFurther && this.isDirectory ())
116 this.scanTree (cb);
117};
118
119io.prototype.stat = function (cb) {
120 var self = this;
121
122 var a = arguments;
123 FS.stat (this.path, function (err, stats) {
124 self.stats = stats;
125// console.log (self.path);
126 if (cb)
127 cb.call (self, err, stats, a[1]);
128 });
129};
130
131io.prototype.parent = function () {
132 return new io (Path.dirname (this.path));
133};
134
135io.prototype.readFile = function (cb) {
136 var self = this;
137 FS.readFile(this.path, function (err, data) {
138 cb.call (self, err, data);
139 });
140};
141
142io.prototype.writeFile = function (data, cb) {
143 var self = this;
144 FS.writeFile(this.path, data, function (err) {
145 if (cb)
146 cb.call (self, err);
147 });
148};