------------------------------------------------------------------------------- -- Utilities shared by all modules. Categorize better if > 5 public functions ------------------------------------------------------------------------------- local Util = {} ------------------------------------------------------------------------------- -- Internal Functions ------------------------------------------------------------------------------- local fs = vim.loop local function create_directory(directory) local stat = fs.fs_stat(directory) if stat then if stat.type == 'directory' then return else error('Expected directory but found file at: ' .. directory) end else local parent = directory:match('(.+)/[^/]*$') if parent then create_directory(parent) end local success, error = fs.fs_mkdir(directory, 493) if not success then error('Could not directory at: ' .. directory .. '. ' .. error) end end end ------------------------------------------------------------------------------- -- Public Interface ------------------------------------------------------------------------------- function Util.ensure_directory_exists(path) local full_path = vim.fn.expand(path) create_directory(path) end function Util.join(...) local separator = '/' local paths = {...} return table.concat(paths, separator):gsub(separator..'+', separator) end function Util.directory_name(file_path) local pattern = '(.+)/[^/]+$' return file_path:match(pattern) end return Util