~kungtotte/dtt

62e1609a3b5f3169612812d24475f5cc04d5cf1c — Thomas Landin 4 months ago da9f5e8
Fix a directory creation bug in initCmd

There was a bug where initCmd would only work when you supplied a
directory to it, not when running it in an empty existing directory.

It now accounts for both use-cases properly, which also let us remove
some conditionals further down in the procedure making everything
simpler. Which is nice.
1 files changed, 33 insertions(+), 42 deletions(-)

M src/dtt.nim
M src/dtt.nim => src/dtt.nim +33 -42
@@ 15,7 15,7 @@ const
  output_dir = "output"

let
  version = "0.1.1"
  version = "0.1.2"

type
  DttError {.pure.} = enum


@@ 27,11 27,9 @@ type
    UnknownError

proc initCmd(dir: string, force: bool = false) =
  var wd: string = dir
  if dir == "nil":
    wd = os.getCurrentDir()
  else:
    os.createDir(os.getCurrentDir() / dir)
  let wd = if dir == "nil": getCurrentDir() else: getCurrentDir() / dir
  if not existsDir(wd):
    createDir(wd)

  # TODO: Update this so that init will re-create any -missing- files
  # but won't overwrite existing files.


@@ 39,42 37,35 @@ proc initCmd(dir: string, force: bool = false) =
  # to create and as we walk that list we check them one by one if the target
  # already exists, overriding it with `force`.
  if not force:
    if os.existsDir(wd):
      for f in walkDirRec(wd, {pcFile, pcDir}, relative = true):
        case getFileInfo(wd / f).kind:
          of pcDir:
            if os.existsDir(wd / f):
              stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
              quit(ord(DttError.DirectoryNotEmpty))
          of pcFile:
            if os.existsFile(wd / f):
              stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
              quit(ord(DttError.DirectoryNotEmpty))
          else:
            stderr.writeLine(fmt"Unknown error occurred: {f} is neither file nor directory")
            quit(ord(DttError.UnknownError))

  if os.existsDir(wd):
    try:
      os.createDir(wd / content_dir)
      os.createDir(wd / template_dir)
      os.createDir(wd / output_dir)
      os.createDir(wd / output_dir / css_dir)
    except OSError:
      let
        e = repr(getCurrentException())
        msg = getCurrentExceptionMsg()
      stderr.writeLine(fmt"{bin}: Exception: {e} with message: {msg}")
      quit(ord(DttError.CreateDirectoryFailed))

    writeFile(wd / "config.cfg", tmpl.cfg)
    writeFile(wd / template_dir / "page.mustache", tmpl.page)
    writeFile(wd / template_dir / "footer.mustache", tmpl.footer)
    writeFile(wd / output_dir / css_dir / "style.css", tmpl.style)

  else:
    stderr.writeLine(fmt"{bin}: Directory '{wd}' does not exist.")
    quit(ord(DttError.DirectoryNotFound))
    for f in walkDirRec(wd, {pcFile, pcDir}, relative = true):
      case getFileInfo(wd / f).kind:
        of pcDir:
          if os.existsDir(wd / f):
            stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
            quit(ord(DttError.DirectoryNotEmpty))
        of pcFile:
          if os.existsFile(wd / f):
            stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
            quit(ord(DttError.DirectoryNotEmpty))
        else:
          stderr.writeLine(fmt"Unknown error occurred: {f} is neither file nor directory")
          quit(ord(DttError.UnknownError))
  try:
    os.createDir(wd / content_dir)
    os.createDir(wd / template_dir)
    os.createDir(wd / output_dir)
    os.createDir(wd / output_dir / css_dir)
  except OSError:
    let
      e = repr(getCurrentException())
      msg = getCurrentExceptionMsg()
    stderr.writeLine(fmt"{bin}: Exception: {e} with message: {msg}")
    quit(ord(DttError.CreateDirectoryFailed))

  writeFile(wd / "config.cfg", tmpl.cfg)
  writeFile(wd / template_dir / "page.mustache", tmpl.page)
  writeFile(wd / template_dir / "footer.mustache", tmpl.footer)
  writeFile(wd / output_dir / css_dir / "style.css", tmpl.style)


proc cleanCmd(all: bool = false, force: bool = false) =