Class: File
- Inherits:
-
IO
show all
- Defined in:
- opal/stdlib/nodejs/file.rb,
opal/stdlib/nashorn/file.rb
Defined Under Namespace
Classes: Stat
Constant Summary
collapse
- ALT_SEPARATOR =
`__path__.sep`
Instance Attribute Summary collapse
Attributes inherited from IO
#lineno
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from IO
binread, #initialize_before_node_io
Constructor Details
#initialize(path, flags = 'r') ⇒ File
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'opal/stdlib/nodejs/file.rb', line 247
def initialize(path, flags = 'r')
@binary_flag = flags.include?('b')
flags = flags.delete('b')
encoding_option_rx = /:(.*)/
if encoding_option_rx.match?(flags)
`handle_unsupported_feature("Encoding option (:encoding) is unsupported by Node.js openSync method and will be removed.")`
flags = flags.sub(encoding_option_rx, '')
end
@path = path
fd = `executeIOAction(function(){return __fs__.openSync(path, flags)})`
super(fd, flags)
end
|
Instance Attribute Details
Returns the value of attribute path.
263
264
265
|
# File 'opal/stdlib/nodejs/file.rb', line 263
def path
@path
end
|
Class Method Details
.absolute_path(path, basedir = nil) ⇒ Object
239
240
241
242
243
|
# File 'opal/stdlib/nodejs/file.rb', line 239
def self.absolute_path(path, basedir = nil)
path = path.respond_to?(:to_path) ? path.to_path : path
basedir ||= Dir.pwd
`return __path__.normalize(__path__.resolve(#{basedir.to_str}, #{path.to_str})).split(__path__.sep).join(__path__.posix.sep)`
end
|
.delete(path) ⇒ Object
Also known as:
unlink
138
139
140
|
# File 'opal/stdlib/nodejs/file.rb', line 138
def self.delete(path)
`executeIOAction(function(){return __fs__.unlinkSync(#{path})})`
end
|
.directory?(path) ⇒ Boolean
173
174
175
176
177
178
179
180
181
182
183
|
# File 'opal/stdlib/nodejs/file.rb', line 173
def self.directory?(path)
return false unless exist? path
result = `executeIOAction(function(){return !!__fs__.lstatSync(path).isDirectory()})`
unless result
realpath = realpath(path)
if realpath != path
result = `executeIOAction(function(){return !!__fs__.lstatSync(realpath).isDirectory()})`
end
end
result
end
|
.exist?(path) ⇒ Boolean
146
147
148
149
|
# File 'opal/stdlib/nodejs/file.rb', line 146
def self.exist?(path)
path = path.path if path.respond_to? :path
`return executeIOAction(function(){return __fs__.existsSync(#{path})})`
end
|
.file?(path) ⇒ Boolean
185
186
187
188
189
190
191
192
193
194
195
|
# File 'opal/stdlib/nodejs/file.rb', line 185
def self.file?(path)
return false unless exist? path
result = `executeIOAction(function(){return !!__fs__.lstatSync(path).isFile()})`
unless result
realpath = realpath(path)
if realpath != path
result = `executeIOAction(function(){return !!__fs__.lstatSync(realpath).isFile()})`
end
end
result
end
|
.join(*paths) ⇒ Object
165
166
167
168
169
170
171
|
# File 'opal/stdlib/nodejs/file.rb', line 165
def self.join(*paths)
paths = paths.map(&:to_s)
prefix = paths.first&.start_with?('//') ? '/' : ''
`#{prefix} + __path__.posix.join.apply(__path__, #{paths})`
end
|
.mtime(path) ⇒ Object
231
232
233
|
# File 'opal/stdlib/nodejs/file.rb', line 231
def self.mtime(path)
`return executeIOAction(function(){return __fs__.statSync(#{path}).mtime})`
end
|
.open(path, mode = 'r') ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'opal/stdlib/nodejs/file.rb', line 213
def self.open(path, mode = 'r')
file = new(path, mode)
if block_given?
begin
yield(file)
ensure
file.close
end
else
file
end
end
|
.read(path) ⇒ Object
129
130
131
|
# File 'opal/stdlib/nodejs/file.rb', line 129
def self.read(path)
`return executeIOAction(function(){return __fs__.readFileSync(#{path}).toString()})`
end
|
.readable?(path) ⇒ Boolean
197
198
199
200
201
202
203
204
205
206
207
|
# File 'opal/stdlib/nodejs/file.rb', line 197
def self.readable?(path)
return false unless exist? path
%{
try {
__fs__.accessSync(path, __fs__.R_OK);
return true;
} catch (error) {
return false;
}
}
end
|
.realpath(pathname, dir_string = nil, cache = nil, &block) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'opal/stdlib/nodejs/file.rb', line 151
def self.realpath(pathname, dir_string = nil, cache = nil, &block)
pathname = join(dir_string, pathname) if dir_string
if block_given?
`
__fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
if (error) Opal.IOError.$new(error.message)
else #{block.call(`realpath`)}
})
`
else
`return executeIOAction(function(){return __fs__.realpathSync(#{pathname}, #{cache})})`
end
end
|
.size(path) ⇒ Object
209
210
211
|
# File 'opal/stdlib/nodejs/file.rb', line 209
def self.size(path)
`return executeIOAction(function(){return __fs__.lstatSync(path).size})`
end
|
.stat(path) ⇒ Object
226
227
228
229
|
# File 'opal/stdlib/nodejs/file.rb', line 226
def self.stat(path)
path = path.path if path.respond_to? :path
File::Stat.new(path)
end
|
.symlink?(path) ⇒ Boolean
235
236
237
|
# File 'opal/stdlib/nodejs/file.rb', line 235
def self.symlink?(path)
`return executeIOAction(function(){return __fs__.lstatSync(#{path}).isSymbolicLink()})`
end
|
.write(path, data) ⇒ Object
133
134
135
136
|
# File 'opal/stdlib/nodejs/file.rb', line 133
def self.write(path, data)
`executeIOAction(function(){return __fs__.writeFileSync(#{path}, #{data})})`
data.size
end
|
Instance Method Details
298
299
300
301
|
# File 'opal/stdlib/nodejs/file.rb', line 298
def close
`executeIOAction(function(){return __fs__.closeSync(#{@fd})})`
super
end
|
294
295
296
|
# File 'opal/stdlib/nodejs/file.rb', line 294
def flush
`executeIOAction(function(){return __fs__.fsyncSync(#{@fd})})`
end
|
303
304
305
|
# File 'opal/stdlib/nodejs/file.rb', line 303
def mtime
`return executeIOAction(function(){return __fs__.statSync(#{@path}).mtime})`
end
|
#sysread(bytes) ⇒ Object
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'opal/stdlib/nodejs/file.rb', line 265
def sysread(bytes)
if @eof
raise EOFError, 'end of file reached'
else
if @binary_flag
%x{
var buf = executeIOAction(function(){return __fs__.readFileSync(#{@path})})
var content
if (is_utf8(buf)) {
content = buf.toString('utf8')
} else {
// coerce to utf8
content = __utf8TextDecoder__.decode(__textEncoder__.encode(buf.toString('binary')))
}
}
res = `content`
else
res = `executeIOAction(function(){return __fs__.readFileSync(#{@path}).toString('utf8')})`
end
@eof = true
@lineno = res.size
res
end
end
|
#write(string) ⇒ Object
290
291
292
|
# File 'opal/stdlib/nodejs/file.rb', line 290
def write(string)
`executeIOAction(function(){return __fs__.writeSync(#{@fd}, #{string})})`
end
|