Make Directories

Path: /src/method/mkdirs.coffee compiled to /lib/method/mkdirs.js

The basicfs.mkdir() will only create one directory. While this extension gives additional methods (in plural) which will also create the full path if possible.

With the maxnum parameter you can control the maximum number of directories to create. So if more are neccessary it will given an Error and won’t create something for this task.

If an EEXIST code will be thrown internally this signals that the directory is already there so this methods will succeed without doing anything and without Error. All other errors will be given back.

Example Use

CoffeeScript Code
fs = require 'alinex-fs' fs.mkdirs '/tmp/some/directory', (err, made) -> return console.error err if err if made console.log "Directory starting from #{made} was created." console.log 'Directory now exists!'

Exported Methods

mkdirs()

Usage:mkdirs(dir, mode, maxnum, cb)

Parameter
  • dir - String directory path to create
  • mode - String|Integer optional the permission mode for the directories (may be given as string like ‘775’)
  • maxnum - Integer optional the maximum number of directories to create
  • cb - function(<Error>, <Integer>) optional callback method given the path of the first directory which was created or null if nothing had to be done. The errors may be:
    • ENOENT Not allowed to create as many directories: …
    • and others

mkdirsSync()

Usage:mkdirsSync(dir, mode, maxnum)

Parameter
  • dir - String directory path to create
  • mode - String|Integer optional the permission mode for the directories (may be given as string like ‘775’)
  • maxnum - Integer optional the maximum number of directories to create
Return
String the path of the first directory which was created or null if nothing had to be done
Throws
  • Error if anything out of order happened with the messages
    • ENOENT Not allowed to create as many directories: …
    • and others

Debugging

Debugging is possible using environment setting:

DEBUG=fs:mkdirs
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with/multiple/dirs? +0ms
fs:mkdirs -> parent is missing +1ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with/multiple? +1ms
fs:mkdirs -> parent is missing +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with? +0ms
fs:mkdirs -> parent is missing +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp? +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp created +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with created +29ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with/multiple created +0ms
fs:mkdirs directory /home/alex/github/node-fs/test/temp/with/multiple/dirs created +0ms