1 -------------------------------------------------------------------------------
2 -- Utilities shared by all modules. Categorize better if > 5 public functions
3 -------------------------------------------------------------------------------
5 -------------------------------------------------------------------------------
7 -------------------------------------------------------------------------------
10 local function create_directory(directory)
11 local stat = fs.fs_stat(directory)
13 if stat.type == 'directory' then
16 error('Expected directory but found file at: ' .. directory)
19 local parent = directory:match('(.+)/[^/]*$')
21 create_directory(parent)
23 local success, error = fs.fs_mkdir(directory, 493)
25 error('Could not directory at: ' .. directory .. '. ' .. error)
29 -------------------------------------------------------------------------------
31 -------------------------------------------------------------------------------
34 function Util.ensure_directory_exists(path)
35 local full_path = vim.fn.expand(path)
36 create_directory(path)
41 function Util.join(...)
44 return table.concat(paths, separator):gsub(separator..'+', separator)
47 function Util.directory_name(file_path)
48 local pattern = '(.+)/[^/]+$'
49 return file_path:match(pattern)
54 function Util.is_valid_date(date_string)
55 local pattern = '(%d+)-(%d+)-(%d+)'
56 local year, month, day = date_string:match(pattern)
57 if year and month and day then
63 function Util.is_before_today(date_string)
64 local pattern = '(%d+)-(%d+)-(%d+)'
65 local year, month, day = date_string:match(pattern)
66 if year and month and day then
67 local today = os.date('*t')
68 local today_date = os.time({year = today.year, month = today.month, day = today.day})
69 local date = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)})
70 return date < today_date