Skip to content

Commit

Permalink
fix(file-io): fix ensureDir() for local file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 20, 2022
1 parent efcde16 commit 4ae95c2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/file-io/src/ensure-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { existsSync, mkdirSync } from "fs";
import { sep } from "path";

/**
* Checks if the directory for given file path already exists, and if not the
* Checks if the directory for given file `path` already exists, and if not the
* case, creates it. Returns true if the latter case.
*
* @remarks
* If `path` only contains a filename (without any directory structure), the
* function does nothing.
*
* @param path
*/
export const ensureDirForFile = (path: string) => {
const dir = path.substring(0, path.lastIndexOf(sep));
return !existsSync(dir)
return dir.length > 0 && !existsSync(dir)
? (mkdirSync(dir, { recursive: true }), true)
: false;
};

0 comments on commit 4ae95c2

Please sign in to comment.