-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
240 lines (189 loc) · 5.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var fs = require( 'fs' );
var pa = require( 'path' );
var Q = undefined;
fs.mkdirParent = function (dirPath, mode, callback) {
//Call the standard fs.mkdir
fs.mkdir( dirPath, mode, function (error) {
//When it fail in this way, do the custom steps
if (error && (error.errno === 34 || error.code === 'ENOENT')) {
//Create all the parents recursively
fs.mkdirParent( pa.dirname( dirPath ), mode, callback );
//And then the directory
fs.mkdirParent( dirPath, mode, callback );
}
//Manually run the callback since we used our own callback to do all these
callback && callback( error );
} );
};
function getQ() {
if (Q == undefined) {
try {
Q = require( 'q' );
}
catch (e) {
console.log( "Q Module must be includes!" );
}
}
return Q;
}
var isifs = function () {
var p = isifs.prototype;
var scope = this;
var Q = undefined;
p.existsQ = function (path) {
Q = getQ();
if (!Q) throw Error( 'Failed to removeQ' );
var deferred = Q.defer();
fs.exists( path, function (exists) {
deferred.resolve( exists )
} );
return deferred.promise;
};
/**
*
* @param path
* @param keepFolder
*/
p.removeSync = function (path, keepFolder) {
keepFolder = keepFolder === true;
if (fs.existsSync( path )) {
fs.readdirSync( path ).forEach( function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync( curPath ).isDirectory()) { // recurse
scope.removeSync( curPath );
} else { // delete file
fs.unlinkSync( curPath );
}
} );
if (!keepFolder)
fs.rmdirSync( path );
}
};
/**
*
* @param path
* @returns {*|promise}
*/
p.removeQ = function (path) {
Q = getQ();
if (!Q) throw Error( 'Failed to removeQ' );
var fs_unlink = Q.denodeify( fs.unlink );
var deferred = Q.defer();
fs.stat( path, function (err, obj) {
if (err) return deferred.reject( err );
var isDir = obj.isDirectory();
if (!isDir) {
fs_unlink( path )
.then( function () {
deferred.resolve( true );
} )
.catch( function (err) {
deferred.reject( err );
} );
}
} );
return deferred.promise;
};
/**
*
* @param path
* @returns {*|promise}
*/
p.createFolderQ = function (path) {
Q = getQ();
if (!Q) throw Error( 'Failed to removeQ' );
var deferred = Q.defer();
fs.exists( path, function (exists) {
if (exists)
return deferred.resolve( false );
fs.mkdirParent( path, undefined, function (err, obj) {
if (err) return deferred.reject( err );
deferred.resolve( true );
} );
} );
return deferred.promise;
};
p.getPathParts = function (filename) {
filename = filename.replace( new RegExp( "/", "g" ), '\\' );
return filename.split( '\\' );
};
p.uniqueFilenameSync = function (filename) {
var tempFilename = filename;
var i = 1;
while (fs.existsSync( tempFilename )) {
tempFilename = scope.appendToFilename( filename, '_' + i );
++i;
}
return tempFilename;
};
p.moveQ = function (oldPath, newPath) {
Q = getQ();
if (!Q) throw Error( 'Failed to moveQ: Module "Q" is missing.' );
var deferred = Q.defer();
function copy() {
var readStream = fs.createReadStream( oldPath );
var writeStream = fs.createWriteStream( newPath );
readStream.on( 'error', function (err) {
return deferred.reject( err );
} );
writeStream.on( 'error', function (err) {
return deferred.reject( err );
} );
readStream.on( 'close', function () {
fs.unlink( oldPath, function (err, obj) {
if (err) return deferred.reject( err );
deferred.resolve( newPath );
} );
} );
readStream.pipe( writeStream );
}
scope.createFolderQ( pa.dirname( newPath ) )
.finally( function () {
fs.rename( oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
return copy();
} else {
return deferred.reject( err );
}
}
deferred.resolve( newPath );
} );
} );
return deferred.promise;
};
p.appendToFilename = function (filePath, value) {
var ext = pa.extname( filePath );
var position = filePath.length - ext.length;
return [filePath.slice( 0, position ), value, filePath.slice( position )].join( '' );
};
p.cwd = p.currentDir = p.baseDir = function () {
return process.cwd();
};
p.getJsonContent = function (jsonPath) {
Q = getQ();
if (!Q) throw Error( 'Failed to moveQ: Module "Q" is missing.' );
var deferred = Q.defer();
fs.exists( jsonPath, function (jsonExists) {
if (!jsonExists) return deferred.reject();
fs.readFile( jsonPath, function (err, data) {
if (err) return deferred.reject( err );
try {
var jsonObject = JSON.parse( data );
deferred.resolve( jsonObject );
} catch (ex) {
console.log( 'Failed to load file: ', jsonPath );
return deferred.reject( ex );
}
} );
} );
return deferred.promise;
};
p.saveJsonContentToFile = function (jsonPath, jsonContent) {
Q = getQ();
if (!Q) throw Error( 'Failed to moveQ: Module "Q" is missing.' );
var writeFile = Q.denodeify( fs.writeFile );
return writeFile( jsonPath, JSON.stringify( jsonContent, null, 4 ) )
}
};
module.exports = new isifs();