-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.js
More file actions
160 lines (140 loc) Β· 3.88 KB
/
Copy pathFileSystem.js
File metadata and controls
160 lines (140 loc) Β· 3.88 KB
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
/*
* DotPHP - Require PHP files under Node.js with Uniter
* Copyright (c) Dan Phillimore (asmblah)
* https://github.com/uniter/dotphp/
*
* Released under the MIT license
* https://github.com/uniter/dotphp/raw/master/MIT-LICENSE.txt
*/
'use strict';
var _ = require('microdash'),
path = require('path'),
Promise = require('lie');
/**
* Abstraction layer around Node's FS module for PHP programs to use
*
* @param {fs} fs
* @param {StreamFactory} streamFactory
* @param {Process} process
* @constructor
*/
function FileSystem(fs, streamFactory, process) {
/**
* @type {fs}
*/
this.fs = fs;
/**
* @type {Process}
*/
this.process = process;
/**
* @type {StreamFactory}
*/
this.streamFactory = streamFactory;
}
_.extend(FileSystem.prototype, {
/**
* Determines whether the specified directory exists.
*
* @returns {boolean}
*/
isDirectory: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath);
try {
return fileSystem.fs.statSync(realPath).isDirectory();
} catch (e) {
return false;
}
},
/**
* Determines whether the specified file exists.
*
* @param {string} filePath
* @returns {boolean}
*/
isFile: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath);
try {
return fileSystem.fs.statSync(realPath).isFile();
} catch (e) {
return false;
}
},
/**
* Opens a Stream for the specified file asynchronously.
*
* @param {string} filePath
* @returns {Promise} Resolves with a Stream for the file on success, rejects on failure
*/
open: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath);
return new Promise(function (resolve, reject) {
fileSystem.fs.open(realPath, 'w+', function (error, fd) {
if (error) {
return reject(error);
}
resolve(fileSystem.streamFactory.create(realPath, fd));
});
});
},
/**
* Opens a Stream for the specified file synchronously
*
* @param {string} filePath
* @returns {Stream}
*/
openSync: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath),
fd = fileSystem.fs.openSync(realPath, 'w+');
return fileSystem.streamFactory.create(realPath, fd);
},
/**
* Converts the specified module path to a full one,
* normalizing any parent- or current-directory symbols
*
* @param {string} filePath
* @returns {string|null}
*/
realPath: function (filePath) {
var fileSystem = this,
relativePath = path.resolve(fileSystem.process.cwd(), filePath);
try {
return fileSystem.fs.realpathSync(relativePath);
} catch (error) {
return null;
}
},
/**
* Deletes a file asynchronously
*
* @param {string} filePath
* @returns {Promise} Resolves on success, rejects on failure
*/
unlink: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath);
return new Promise(function (resolve, reject) {
fileSystem.fs.unlink(realPath, function (error) {
if (error) {
return reject(error);
}
resolve();
});
});
},
/**
* Deletes a file synchronously
*
* @param {string} filePath
*/
unlinkSync: function (filePath) {
var fileSystem = this,
realPath = fileSystem.realPath(filePath);
fileSystem.fs.unlinkSync(realPath);
}
});
module.exports = FileSystem;