-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofile.js
47 lines (40 loc) · 1.51 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
43
44
45
46
47
'use strict';
var fs = require('fs');
var async = require('async');
var glob = require('glob');
module.exports = function (task) {
task
.id('chmod')
.name('Change mode')
.description('Change mode of files and folders.')
.author('Indigo United')
.option('files', 'Which file to chmod. Accepts a filename and array of filenames. Also note that the filenames can be minimatch patterns.')
.option('mode', 'The mode to apply.', '0777')
.option('glob', 'The options to pass to glob (check https://npmjs.org/package/glob for details).', 'null')
.setup(function (opt, ctx, next) {
if (typeof opt.mode !== 'number') {
opt.mode = parseInt(opt.mode, 8);
}
next();
})
.do(function (opt, ctx, next) {
var files = Array.isArray(opt.files) ? opt.files : [opt.files];
var error;
async.forEach(files, function (file, next) {
glob(file, opt.glob, function (err, files) {
if (err) {
return next(err);
}
if (!files.length) {
error = new Error('ENOENT, no such file or directory \'' + file + '\'');
error.code = 'ENOENT';
return next(error);
}
async.forEach(files, function (file, next) {
ctx.log.debugln('Changing mode for file: ' + file);
fs.chmod(file, opt.mode, next);
}, next);
});
}, next);
});
};