Skip to content

Commit

Permalink
chore(fs): add nil check + slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 8, 2024
1 parent 3bc64da commit 2cce26d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lua/rocks/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function fs.file_exists(location)
return false
end

--- Write `contents` to a file
--- Write `contents` to a file asynchronously
---@param location string file path
---@param mode string mode to open the file for
---@param contents string file contents
---@param callback? function
function fs.write_file(location, mode, contents, callback)
local dir = vim.fn.fnamemodify(location, ":h")
local dir = vim.fs.dirname(location)
fs.mkdir_p(dir)
-- 644 sets read and write permissions for the owner, and it sets read-only
-- mode for the group and others
Expand All @@ -55,9 +55,13 @@ function fs.write_file(location, mode, contents, callback)
if write_err then
local msg = ("Error writing %s: %s"):format(location, err)
log.error(msg)
vim.notify(msg, vim.log.levels.ERROR)
vim.schedule(function()
vim.notify(msg, vim.log.levels.ERROR)
end)
end
if file then
uv.fs_close(file)
end
uv.fs_close(file)
if callback then
callback()
end
Expand Down

0 comments on commit 2cce26d

Please sign in to comment.