mirror of https://github.com/freeCodeCamp/devdocs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
904 B
50 lines
904 B
require 'fileutils'
|
|
require 'find'
|
|
|
|
module Docs
|
|
class FileStore < AbstractStore
|
|
private
|
|
|
|
def read_file(path)
|
|
File.read(path)
|
|
end
|
|
|
|
def create_file(path, value)
|
|
FileUtils.mkpath File.dirname(path)
|
|
|
|
if value.is_a? Tempfile
|
|
FileUtils.move(value, path)
|
|
else
|
|
File.write(path, value)
|
|
end
|
|
end
|
|
|
|
alias_method :update_file, :create_file
|
|
|
|
def delete_file(path)
|
|
if File.directory?(path)
|
|
FileUtils.rmtree(path, secure: true)
|
|
else
|
|
FileUtils.rm(path)
|
|
end
|
|
end
|
|
|
|
def file_exist?(path)
|
|
File.exists?(path)
|
|
end
|
|
|
|
def file_mtime(path)
|
|
File.mtime(path)
|
|
end
|
|
|
|
def list_files(path)
|
|
Find.find path do |file|
|
|
next if file == path
|
|
Find.prune if File.basename(file)[0] == '.'
|
|
yield file
|
|
Find.prune unless File.exists?(file)
|
|
end
|
|
end
|
|
end
|
|
end
|