Permalink
Browse files

Mkdir :: support to multiple dirs and mode

  • Loading branch information...
1 parent fa18f84 commit 43dab1c88124468d3dd807d16043c4c9d1d8bd36 @filipediasf filipediasf committed Dec 4, 2012
Showing with 48 additions and 21 deletions.
  1. +2 −2 .jshintrc
  2. +46 −19 tasks/mkdir.js
View
4 .jshintrc
@@ -18,7 +18,7 @@
"noarg": true,
"noempty": false,
"nonew": true,
- "plusplus": true,
+ "plusplus": false,
"regexp": true,
"undef": true,
"unused": true,
@@ -40,7 +40,7 @@
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
- "loopfunc": false,
+ "loopfunc": true,
"multistr": false,
"onecase": true,
"regexdash": false,
View
65 tasks/mkdir.js
@@ -1,5 +1,7 @@
var mkdirp = require('mkdirp');
var fs = require('fs');
+var utils = require('amd-utils');
+var async = require('async');
var task = {
'id' : 'mkdir',
@@ -8,33 +10,58 @@ var task = {
'options' : {
'dir': {
'description': 'The directory you want to create'
+ },
+ 'mode': {
+ 'description': 'The directory permissions',
+ 'default': '0777'
+
+ }
+ },
+ 'filter': function (opt) {
+ if (!utils.lang.isNumber(opt.mode)) {
+ opt.mode = parseInt(opt.mode, 8);
}
},
- 'tasks' :
+ 'tasks' :
[
{
'task' : function (opt, next) {
- fs.stat(opt.dir, function (err, stat) {
- if (!err || err.code !== 'ENOENT') {
- if (stat && !stat.isDirectory()) {
- next(new Error('Passed dir already exists and is not a directory.'));
- } else {
- next(err);
- }
- }
- mkdirp(opt.dir, function (err) {
- if (err) {
- next(err);
- }
- else {
- next();
- }
- });
- });
+ if (utils.lang.isString(opt.dir)) {
+ opt.dir = [opt.dir];
+ }
+ var tasks = [];
+ for (var i = 0, l = opt.dir.length; i < l; ++i) {
+ tasks.push(function (i) {
+ fs.stat(opt.dir[i], function (err, stat) {
+ if (!err || err.code !== 'ENOENT') {
+ if (stat && !stat.isDirectory()) {
+ next(new Error('Passed dir already exists and is not a directory.'));
+ } else {
+ next(err);
+ }
+ }
+
+ mkdirp(opt.dir[i], opt.mode, function (err) {
+ if (err) {
+ next(err);
+ }
+ else {
+ next();
+ }
+ });
+ });
+ }.bind(this, i));
+ }
+ async.parallel(tasks, next);
},
- description: 'make dir "{{dir}}"'
+ description: 'Make dir'
+ /*
+ description: function (opt) {
+ return 'Making dirs: ' opt.dir.join(',');
+ }*/
+
}
]
};

0 comments on commit 43dab1c

Please sign in to comment.