-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
index.js
177 lines (152 loc) · 4.29 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
var path = require('path');
var toDest = require('./lib/dest');
var invalid = require('./lib/invalid');
var utils = require('./lib/utils');
var base = require('./lib/base');
/**
* Copy a filepath, vinyl file, array of files, or glob of files to the
* given destination `directory`, with `options` and callback function that
* exposes `err` and the array of vinyl files that are created by the copy
* operation.
*
* ```js
* copy('*.js', 'dist', function(err, file) {
* // exposes the vinyl `file` created when the file is copied
* });
* ```
* @param {String|Object|Array} `patterns` Filepath(s), vinyl file(s) or glob of files.
* @param {String} `dir` Destination directory
* @param {Object|Function} `options` or callback function
* @param {Function} `cb` Callback function if no options are specified
* @api public
*/
function copy(patterns, dir, options, cb) {
if (arguments.length < 3) {
return invalid.apply(null, arguments);
}
if (typeof options === 'function') {
cb = options;
options = {};
}
var opts = utils.extend({cwd: process.cwd()}, options);
opts.cwd = path.resolve(opts.cwd);
patterns = utils.arrayify(patterns);
if (!utils.hasGlob(patterns)) {
copyEach(patterns, dir, opts, cb);
return;
}
opts.patterns = patterns;
if (!opts.srcBase) {
opts.srcBase = path.resolve(opts.cwd, utils.parent(patterns));
}
utils.glob(patterns, opts, function(err, files) {
if (err) {
cb(err);
return;
}
copyEach(files, dir, opts, cb);
});
}
/**
* Copy an array of files to the given destination `directory`, with
* `options` and callback function that exposes `err` and the array of
* vinyl files that are created by the copy operation.
*
* ```js
* copy.each(['foo.txt', 'bar.txt', 'baz.txt'], 'dist', function(err, files) {
* // exposes the vinyl `files` created when the files are copied
* });
* ```
* @name .copy.each
* @param {Array} `files` Filepaths or vinyl files.
* @param {String} `dir` Destination directory
* @param {Object|Function} `options` or callback function
* @param {Function} `cb` Callback function if no options are specified
* @api public
*/
function copyEach(files, dir, options, cb) {
if (arguments.length < 3) {
return invalid.apply(null, arguments);
}
if (typeof options === 'function') {
cb = options;
options = {};
}
var opts = utils.extend({}, options);
if (typeof opts.cwd === 'undefined') {
opts.cwd = process.cwd();
}
if (!opts.srcBase && opts.patterns) {
opts.srcBase = path.resolve(opts.cwd, utils.parent(opts.patterns));
}
utils.each(files, function(filename, next) {
var filepath = path.resolve(opts.cwd, filename);
if (utils.isDirectory(filepath)) {
next()
return;
}
copyOne(filepath, dir, opts, next);
}, function(err, arr) {
if (err) {
cb(err);
return;
}
cb(null, arr.filter(Boolean));
});
}
/**
* Copy a single `file` to the given `dest` directory, using
* the specified options and callback function.
*
* ```js
* copy.one('foo.txt', 'dist', function(err, file) {
* if (err) throw err;
* // exposes the vinyl `file` that is created when the file is copied
* });
* ```
* @name .copy.one
* @param {String|Object} `file` Filepath or vinyl file
* @param {String} `dir` Destination directory
* @param {Object|Function} `options` or callback function
* @param {Function} `cb` Callback function if no options are specified
* @api public
*/
function copyOne(file, dir, options, cb) {
if (arguments.length < 3) {
return invalid.apply(null, arguments);
}
if (typeof options === 'function') {
cb = options;
options = {};
}
var opts = utils.extend({}, options);
if (typeof opts.cwd === 'undefined') {
opts.cwd = process.cwd();
}
if (typeof file === 'string') {
file = path.resolve(opts.cwd, file);
}
if (!opts.srcBase && opts.patterns) {
opts.srcBase = path.resolve(opts.cwd, utils.parent(opts.patterns));
}
toDest(dir, file, opts, function(err, out) {
if (err) {
cb(err);
return;
}
base(file, out.path, opts, function(err) {
if (err) {
cb(err);
return;
}
cb(null, out);
});
});
}
/**
* Expose `copy`
*/
module.exports = copy;
module.exports.one = copyOne;
module.exports.each = copyEach;