1 | var FS = require('fs');
|
2 | var Path = require('path');
|
3 |
|
4 | var io = module.exports = function (path) {
|
5 |
|
6 | this.path = path;
|
7 |
|
8 |
|
9 |
|
10 | this.name = Path.basename (path);
|
11 |
|
12 | this.extension = Path.extname (path).substr (1);
|
13 |
|
14 | };
|
15 |
|
16 | io.prototype.relative = function (relPath) {
|
17 | return Path.relative (this.path, relPath instanceof io ? relPath.path : relPath);
|
18 | };
|
19 |
|
20 |
|
21 | io.prototype.isFile = function () {
|
22 | return this.stats ? this.stats.isFile () : null;
|
23 | };
|
24 |
|
25 | io.prototype.isDirectory = function () {
|
26 | return this.stats ? this.stats.isDirectory () : null;
|
27 | };
|
28 |
|
29 | io.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 |
|
34 | io.prototype.chmod = function (mode, cb) {
|
35 | var p = this.path;
|
36 | FS.chmod (p, mode, function (err) {
|
37 | cb (err);
|
38 | });
|
39 | };
|
40 |
|
41 | io.prototype.mkdir = function (mode, callback) {
|
42 | if ("function" === typeof mode && callback === undefined) {
|
43 | callback = mode;
|
44 | mode = 0777;
|
45 | }
|
46 | return FS.mkdir (this.path, mode, callback && callback.bind (this));
|
47 | };
|
48 |
|
49 |
|
50 | io.prototype.writeStream = function (options) {
|
51 | return FS.createWriteStream (this.path, options);
|
52 | };
|
53 |
|
54 | io.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 |
|
74 | io.prototype.scanTree = function (cb) {
|
75 | var self = this;
|
76 |
|
77 | FS.readdir (this.path, function (err, files) {
|
78 |
|
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 |
|
88 | io.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 |
|
110 | io.prototype.scanSubTree = function (err, stats, cb) {
|
111 | var scanFurther = 0;
|
112 | if (cb)
|
113 | scanFurther = cb (this);
|
114 |
|
115 | if (scanFurther && this.isDirectory ())
|
116 | this.scanTree (cb);
|
117 | };
|
118 |
|
119 | io.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 |
|
126 | if (cb)
|
127 | cb.call (self, err, stats, a[1]);
|
128 | });
|
129 | };
|
130 |
|
131 | io.prototype.parent = function () {
|
132 | return new io (Path.dirname (this.path));
|
133 | };
|
134 |
|
135 | io.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 |
|
142 | io.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 | };
|