-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofile.js
60 lines (50 loc) · 2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
var fs = require('fs');
var glob = require('glob');
var async = require('async');
module.exports = function (task) {
task
.id('scaffolding-close')
.name('Scaffolding: close.')
.description('Close {{placeholders}} in files.')
.author('Indigo United')
.option('files', 'Which files to process. Accepts a filename and array of filenames. Also note that the filenames can be minimatch patterns.')
.option('placeholders', 'Which placeholders to close. Accepts a string, or an array of strings.')
.option('trim', 'Trim leading or trailing spaces', 'true')
.option('glob', 'The options to pass to glob (check https://npmjs.org/package/glob for details).', null)
.do(function (opt, ctx, next) {
opt.glob = opt.glob || {};
var files = Array.isArray(opt.files) ? opt.files : [opt.files];
var placeholders = Array.isArray(opt.placeholders) ? opt.placeholders : [opt.placeholders];
var data = {};
opt.glob.mark = true;
placeholders.forEach(function (placeholder) {
data[placeholder] = '';
});
// data is done at this time
// For each item in the files array, perform a glob
async.forEach(files, function (file, next) {
glob(file, opt.glob, function (err, matches) {
if (err) {
return next(err);
}
var files = matches.filter(function (match) {
return !/[\/\\]$/.test(match);
});
// For each file in the glob result,
// perform the interpolation
async.forEach(files, function (file, next) {
ctx.log.debugln('Reading file: ' + file);
fs.readFile(file, function (err, contents) {
if (err) {
return next(err);
}
contents = ctx.string.interpolate(contents.toString(), data, { trim: opt.trim });
ctx.log.debugln('Writing file: ' + file);
fs.writeFile(file, contents, next);
});
}, next);
});
}, next);
});
};