viddo/atom-textual-velocity

View on GitHub
lib/getValidDirFromPath.js

Summary

Maintainability
A
0 mins
Test Coverage
/* @flow */

import fs from "fs-plus";
import path from "path";

export default function validatePath(notesPath: string) {
  notesPath = notesPath.trim();

  if (notesPath === "") {
    notesPath = path.join(atom.configDirPath, "notes");
  }

  notesPath = fs.absolute(notesPath); // e.g. ~/something => /Users/alice/something

  if (!fs.isAbsolute(notesPath)) {
    notesPath = path.join(fs.getHomeDirectory(), notesPath);
  }

  notesPath = path.resolve(notesPath); // removes any trailing slash

  if (!fs.existsSync(notesPath)) {
    fs.mkdirSync(notesPath, 0o755);
  }

  return notesPath;
}