-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofile.js
42 lines (35 loc) · 1.21 KB
/
autofile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var async = require('async');
module.exports = function (task) {
task
.id('mkdir')
.name('Make directory')
.description('Make directory recursively, just like `mkdir -p`')
.author('Indigo United')
.option('dirs', 'The directory you want to create. Accepts a directory or an array of directories.')
.option('mode', 'The directory permissions.', '0777')
.setup(function (opt, ctx, next) {
if (typeof opt.mode !== 'number') {
opt.mode = parseInt(opt.mode, 8);
}
next();
})
.do(function (opt, ctx, next) {
var dirs = Array.isArray(opt.dirs) ? opt.dirs : [opt.dirs];
var error;
async.forEach(dirs, function (dir, next) {
dir = path.normalize(dir);
fs.stat(dir, function (err) {
if (!err || err.code !== 'ENOENT') {
error = new Error('EEXIST, target already exists \'' + dir + '\'');
error.code = 'EEXISTS';
return next(error);
}
mkdirp(dir, opt.mode, next);
});
}, next);
});
};