Skip to content

Instantly share code, notes, and snippets.

@MurhafSousli
Last active May 28, 2018 21:58
Show Gist options
  • Save MurhafSousli/6a72ac2c4e0fc940ce327e20b9378775 to your computer and use it in GitHub Desktop.
Save MurhafSousli/6a72ac2c4e0fc940ce327e20b9378775 to your computer and use it in GitHub Desktop.
Create directory recursively for a giving path
import { existsSync, mkdirSync } from 'fs';
import { dirname } from 'path';
function makeDirectory(target: string) {
// check if parent directory exists
const parentDir = dirname(target);
if (!existsSync(parentDir)) {
makeDirectory(parentDir);
}
// check if directory exists
if (!existsSync(target)) {
mkdirSync(target);
}
}
@MurhafSousli
Copy link
Author

Usage:

makeDirectory('build/x/y/z');

Output:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment