when saving a file with edit, make the missing dirs if needed

This commit is contained in:
payonel 2017-05-22 23:42:40 -07:00
parent f2b5e01730
commit b02c60ef11

View File

@ -1,4 +1,3 @@
local event = require("event")
local fs = require("filesystem") local fs = require("filesystem")
local keyboard = require("keyboard") local keyboard = require("keyboard")
local shell = require("shell") local shell = require("shell")
@ -17,6 +16,12 @@ if #args == 0 then
end end
local filename = shell.resolve(args[1]) local filename = shell.resolve(args[1])
local file_parentpath = fs.path(filename)
if fs.exists(file_parentpath) and not fs.isDirectory(file_parentpath) then
io.stderr:write(string.format("Not a directory: %s\n", file_parentpath))
return 1
end
local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly() local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly()
@ -502,6 +507,9 @@ local keyBindHandlers = {
end end
fs.copy(filename, backup) fs.copy(filename, backup)
end end
if not fs.exists(file_parentpath) then
fs.makeDirectory(file_parentpath)
end
local f, reason = io.open(filename, "w") local f, reason = io.open(filename, "w")
if f then if f then
local chars, firstLine = 0, true local chars, firstLine = 0, true