Class: File
  
  
  
  
  
    - Inherits:
- 
      IO
      
        
        show all
      
    
    - Defined in:
- opal/stdlib/headless_browser/file.rb,
 opal/stdlib/deno/file.rb,
 opal/stdlib/nodejs/file.rb,
 opal/stdlib/nashorn/file.rb
 
Overview
  
    backtick_javascript: true
   
 
  
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 
  
  
  
  
  
    | 
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 | # File 'opal/stdlib/deno/file.rb', line 242
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 Deno.openSync(path, flags)})`
  super(fd, flags)
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    Returns the value of attribute path.
   
 
  
  
    | 
258
259
260 | # File 'opal/stdlib/deno/file.rb', line 258
def path
  @path
end | 
 
    
   
  
    Class Method Details
    
      
  
  
    .absolute_path(path, basedir = nil)  ⇒ Object 
  
  
  
  
    | 
236
237
238 | # File 'opal/stdlib/deno/file.rb', line 236
def self.absolute_path(path, basedir = nil)
  raise 'File::absolute_path is currently unsupported in Deno!'
end | 
 
    
      
  
  
    .delete(path)  ⇒ Object 
  
  
    Also known as:
    unlink
    
  
  
  
    | 
127
128
129 | # File 'opal/stdlib/deno/file.rb', line 127
def self.delete(path)
  `executeIOAction(function(){return Deno.removeSync(#{path})})`
end | 
 
    
      
  
  
    .directory?(path)  ⇒ Boolean 
  
  
  
  
    | 
170
171
172
173
174
175
176
177
178
179
180 | # File 'opal/stdlib/deno/file.rb', line 170
def self.directory?(path)
  return false unless exist? path
  result = `executeIOAction(function(){return !!Deno.lstatSync(path).isDirectory})`
  unless result
    realpath = realpath(path)
    if realpath != path
      result = `executeIOAction(function(){return !!Deno.lstatSync(realpath).isDirectory})`
    end
  end
  result
end | 
 
    
      
  
  
    .exist?(path)  ⇒ Boolean 
  
  
  
  
    | 
135
136
137
138 | # File 'opal/stdlib/deno/file.rb', line 135
def self.exist?(path)
  path = path.path if path.respond_to? :path
  `return executeIOAction(function(){return Deno.statSync(#{path})})`
end | 
 
    
      
  
  
    .file?(path)  ⇒ Boolean 
  
  
  
  
    | 
182
183
184
185
186
187
188
189
190
191
192 | # File 'opal/stdlib/deno/file.rb', line 182
def self.file?(path)
  return false unless exist? path
  result = `executeIOAction(function(){return !!Deno.lstatSync(path).isFile})`
  unless result
    realpath = realpath(path)
    if realpath != path
      result = `executeIOAction(function(){return !!Deno.lstatSync(realpath).isFile})`
    end
  end
  result
end | 
 
    
      
  
  
    .join(*paths)  ⇒ Object 
  
  
  
  
    | 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | # File 'opal/stdlib/deno/file.rb', line 154
def self.join(*paths)
      paths = paths.map(&:to_s)
  prefix = paths.first&.start_with?('//') ? '/' : ''
  path = prefix
  paths.each do |pth|
    path << if pth.end_with?('/') || pth.start_with?('/')
              pth
            else
              '/' + pth
            end
  end
  path
end | 
 
    
      
  
  
    .mtime(path)  ⇒ Object 
  
  
  
  
    | 
228
229
230 | # File 'opal/stdlib/deno/file.rb', line 228
def self.mtime(path)
  `return executeIOAction(function(){return Deno.statSync(#{path}).mtime})`
end | 
 
    
      
  
  
    .open(path, mode = 'r')  ⇒ Object 
  
  
  
  
    | 
210
211
212
213
214
215
216
217
218
219
220
221 | # File 'opal/stdlib/deno/file.rb', line 210
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 
  
  
  
  
    | 
118
119
120 | # File 'opal/stdlib/deno/file.rb', line 118
def self.read(path)
  `return executeIOAction(function(){return Deno.readFileSync(#{path}).toString()})`
end | 
 
    
      
  
  
    .readable?(path)  ⇒ Boolean 
  
  
  
  
    | 
194
195
196
197
198
199
200
201
202
203
204 | # File 'opal/stdlib/deno/file.rb', line 194
def self.readable?(path)
  return false unless exist? path
  %{
    try {
      Deno.openSync(path, {read: true}).close();
      return true;
    } catch (error) {
      return false;
    }
  }
end | 
 
    
      
  
  
    .realpath(pathname, dir_string = nil, cache = nil, &block)  ⇒ Object 
  
  
  
  
    | 
140
141
142
143
144
145
146
147
148
149
150
151
152 | # File 'opal/stdlib/deno/file.rb', line 140
def self.realpath(pathname, dir_string = nil, cache = nil, &block)
  pathname = join(dir_string, pathname) if dir_string
  if block_given?
    `
      Deno.realpath(#{pathname}, #{cache}, function(error, realpath){
        if (error) Opal.IOError.$new(error.message)
        else #{block.call(`realpath`)}
      })
      `
  else
    `return executeIOAction(function(){return Deno.realpathSync(#{pathname}, #{cache})})`
  end
end | 
 
    
      
  
  
    .size(path)  ⇒ Object 
  
  
  
  
    | 
206
207
208 | # File 'opal/stdlib/deno/file.rb', line 206
def self.size(path)
  `return executeIOAction(function(){return Deno.lstatSync(path).size})`
end | 
 
    
      
  
  
    .stat(path)  ⇒ Object 
  
  
  
  
    | 
223
224
225
226 | # File 'opal/stdlib/deno/file.rb', line 223
def self.stat(path)
  path = path.path if path.respond_to? :path
  File::Stat.new(path)
end | 
 
    
      
  
  
    .symlink(path, new_path)  ⇒ Object 
  
  
  
  
    | 
140
141
142
143 | # File 'opal/stdlib/nodejs/file.rb', line 140
def self.symlink(path, new_path)
  `executeIOAction(function(){return __fs__.symlinkSync(#{path}, #{new_path})})`
  0
end | 
 
    
      
  
  
    .symlink?(path)  ⇒ Boolean 
  
  
  
  
    | 
232
233
234 | # File 'opal/stdlib/deno/file.rb', line 232
def self.symlink?(path)
  `return executeIOAction(function(){return Deno.lstatSync(#{path}).isSymLink})`
end | 
 
    
      
  
  
    .write(path, data)  ⇒ Object 
  
  
  
  
    | 
122
123
124
125 | # File 'opal/stdlib/deno/file.rb', line 122
def self.write(path, data)
  `executeIOAction(function(){return Deno.writeFileSync(#{path}, __textEncoder__.encode(#{data}));})`
  data.size
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    | 
293
294
295
296 | # File 'opal/stdlib/deno/file.rb', line 293
def close
  `executeIOAction(function(){return #{@fd}.close()})`
  super
end | 
 
    
      
  
  
    | 
289
290
291 | # File 'opal/stdlib/deno/file.rb', line 289
def flush
  end | 
 
    
      
  
  
    | 
298
299
300 | # File 'opal/stdlib/deno/file.rb', line 298
def mtime
  `return executeIOAction(function(){return Deno.statSync(#{@path}).mtime})`
end | 
 
    
      
  
  
    #sysread(bytes)  ⇒ Object 
  
  
  
  
    | 
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 | # File 'opal/stdlib/deno/file.rb', line 260
def sysread(bytes)
  if @eof
    raise EOFError, 'end of file reached'
  else
    if @binary_flag
      %x{
        var buf = executeIOAction(function(){return Deno.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 Deno.readFileSync(#{@path}).toString('utf8')})`
    end
    @eof = true
    @lineno = res.size
    res
  end
end | 
 
    
      
  
  
    #write(string)  ⇒ Object 
  
  
  
  
    | 
285
286
287 | # File 'opal/stdlib/deno/file.rb', line 285
def write(string)
  `executeIOAction(function(){return #{@fd}.writeSync(__textEncoder__.encode(#{string}))})`
end |