mkp
(mk path) is a node.js module that recursively creates paths, both directory and file with brace expansion support. It means, you can do stuff like mkdir -p /some/dir/{foo,bar}/baz
, but in node.js!
npm i mkp
-
input
{String | Array<String>}
string or an array of strings of normal paths (file and dir) and/or braces patterns. -
options
{Object}
optional (all options arefalse
by default) -
noext
{Boolean}
- the default behavior is if path doesn't have extname, it is created as a directory. If
{noext: true}
then paths without extname will be created as files and not dirs. It is basically used when you want to create file(s) without extname, like/some/filenoext
.
- the default behavior is if path doesn't have extname, it is created as a directory. If
-
return
cb(err)
with possible err or null.
Create files and/or dirs. input
can be file, dir, things like /some/foo-{1..3}/file.js
or an array of each or combination of all of them. It doesn't matter. Just put it there, you most likely get what you want. It couldn't be simpler than this. Please checkout examples down below.
create files and dirs sync. input
and options
are the same as async version. It throws err if any found.
const mkp = require('mkp')
var dir = 'some/nested/dir'
try {
mkp.sync(dir)
console.log('done!')
} catch (er) {
console.error(er)
}
In all examples, it is assumed
const mkp = require('mkp')
is declared before.
dirs (input is string)
var dir = 'some/nested/dir'
mkp(dir, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var dirs = 'some/nested/dir-{a,b,c}'
mkp(dirs, (er) => {
if (er) console.error(er)
else console.log('done!')
// all 'some/nested/dir-a', 'some/nested/dir-b', 'some/nested/dir-c' created!
})
var dirs = 'some/nested/dir-{a..c}'
mkp(dirs, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var dirs = 'some/nested/{foo,bar}/dir-{a..e..2}'
mkp(dirs, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var dirs = 'some/nested/{foo,{1..3},bar}/dir'
mkp(dirs, (er) => {
if (er) console.error(er)
else console.log('done!')
})
files (input is string)
var file = 'some/nested/file.js'
mkp(file, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var files = 'some/nested/file-{a,b,c}.js'
mkp(files, (er) => {
if (er) console.error(er)
else console.log('done!')
// all 'some/nested/file-a.js', 'some/nested/file-b.js', 'some/nested/file-c.js' created!
})
var files = 'some/nested/file-{a..c}.js'
mkp(files, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var files = 'some/nested/{foo,bar}/file-{a..e..2}.js'
mkp(files, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var files = 'some/nested/{foo,{1..3},bar}/file.js'
mkp(files, (er) => {
if (er) console.error(er)
else console.log('done!')
})
#####noext
option (useful when you want to create files with no extname)
var file = 'some/nested/filenoext'
// without {noext: true}, it will be created as dir
mkp(file, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var file = 'some/nested/filenoext'
// now it will be created as file
mkp(file, {noext: true}, (er) => {
if (er) console.error(er)
else console.log('done!')
})
input is array
var dirs = ['some/nested/dir-{a,b,c}', 'some/other/nested/dir']
mkp(dirs, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var files = ['some/nested/file-{a,b,c}.js', 'some/other/nested/file.md']
mkp(files, (er) => {
if (er) console.error(er)
else console.log('done!')
})
var paths = [
'some/nested/dir-{a,b,c}',
'some/nested/file-{a,b,c}.js',
'some/other/nested/dir',
'some/other/nested/file.js'
]
mkp(paths, (er) => {
if (er) console.error(er)
else console.log('done!')
})
Licensed under MIT