-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder-fs.js
143 lines (131 loc) · 4.65 KB
/
builder-fs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @file File system build helpers.
* @copyright Ben Mackie 2015
* @license MIT
*/
var fs = require('fs');
var path = require('path');
var del = require('del');
var _ = require('underscore');
var s = require('underscore.string');
var glob = require("glob");
var walk = require("walk");
var fse = require("fs-extra");
var debug = require('./depfile').debug('fs');
module.exports = function(builder, util) {
var _this = builder;
var _util = util;
var defaultCleanOptions = {
recursive : true
}
var defaultCopyOptions = {
extensions : ['js'],
recursive : true,
overwrite : false
}
_this.del = function(dir) {
if (!_.isArray(dir)) {
return _util.addResolver(function(resolve, reject) {
del(dir, function() {
debug('finished del');
resolve();
});
});
} else {
var resolvers = _.map(dir, function(dir) {
return function(resolve, reject) {
del(dir, function() {
debug('finished del');
resolve();
});
}
});
return _util.addResolvers(resolvers);
}
}
_this.clean = function(dir, options) {
options = _.extend(defaultCleanOptions, options);
var path = dir + (options.recursive ? '/**' : '') + '/*.es6';
return _util.addResolver(function(resolve, reject) {
globOptions = {};
glob(path, globOptions, function (er, files) {
files.forEach(function(file) {
var stem = s.strLeftBack(file, '.');
var srcFile = stem + '.js';
if (fs.existsSync(srcFile)) {
debug(' Deleting file %s...', srcFile)
fs.unlinkSync(srcFile);
}
var mapFile = stem + '.js.map';
if (fs.existsSync(mapFile)) {
debug(' Deleting file %s...', mapFile)
fs.unlinkSync(mapFile);
}
});
debug('finished clean')
resolve();
});
});
}
_this.copy = function(sourceDir, targetDir, options) {
// Validate args.
var opt = _.extend(defaultCopyOptions, options);
if (!sourceDir)
throw new Error('Invalid source directory.');
if (!targetDir)
throw new Error('Invalid target directory.');
/*
if (typeof extensions === 'string')
extensions = [extensions];
else if (!_.isArray(extensions))
throw new Error('Invalid extensions.');
*/
// TODO: Gulp approach is nicer and doesn't have dependencies
// but won't support non-clobber option until v4. When that is
// out use this approach instead and preserve "don't clobber by default".
/*
// Calc full source path.
var sourcePath = sourceDir;
if (opt.recursive)
sourcePath += '/**';
sourcePath += '/*';
if (opt.extensions) {
if (opt.extensions.length > 1)
sourcePath += '.{' + opt.extensions.join() + '}';
else if (opt.extensions)
sourcePath += '.' + opt.extensions[0];
}
// All that's left is to do the copy...
//debug('copying from %s', sourcePath);
return _util.addStream(function() {
return gulp.src(sourcePath)
.pipe(gulp.dest(targetDir));
});
return _this;
*/
sourceDir += '/';
targetDir += '/';
return _util.addResolver(function(resolve, reject) {
var walker = walk.walk(sourceDir, {followLinks:false});
walker.on("file", function (root, fileStats, next) {
var name = fileStats.name;
var ext = s.strRightBack(name, '.');
if (opt.extensions == null || opt.extensions.indexOf(ext) >= 0) {
if (opt.overwrite || !fs.existsSync(targetDir + name)) {
debug('copying %s (extensions %s)...', name, opt.extensions);
fse.copySync(sourceDir + name, targetDir + name);
}
}
next();
});
walker.on("errors", function (root, nodeStatsArray, next) {
next();
});
walker.on("end", function () {
debug("finished copy");
resolve();
});
});
return _this;
}
};