UNPKG

1.47 kBJavaScriptView Raw
1(function() {
2 module.exports = function($) {
3 var gulp, gulpIf, normalizePath, normalizePathToArray, using, wrapList;
4 ({normalizePath, normalizePathToArray, wrapList} = $.fn);
5 gulp = require('gulp');
6 gulpIf = require('gulp-if');
7 using = require('gulp-using');
8
9 // return
10 return $.copy_ = async function(...arg) {
11 var msg, option, source, target;
12
13 // source, target, [option]
14 [source, target, option] = (function() {
15 switch (arg.length) {
16 case 2:
17 return [arg[0], arg[1], null];
18 case 3:
19 return arg;
20 default:
21 throw new Error('invalid argument length');
22 }
23 })();
24 source = normalizePathToArray(source);
25 target = normalizePath(target);
26 if (!source.length) {
27 return $;
28 }
29 await new Promise(function(resolve) {
30 var rename;
31
32 // require
33 rename = require('gulp-rename');
34 return gulp.src(source, {
35 allowEmpty: true
36 }).pipe(using()).pipe(gulpIf(!!option, rename(option))).pipe(gulp.dest(function(e) {
37 return target || e.base;
38 })).on('end', function() {
39 return resolve();
40 });
41 });
42 msg = `copied ${wrapList(source)} to ${wrapList(target)}`;
43 if (option) {
44 msg += `, as '${$.parseString(option)}'`;
45 }
46 $.info('copy', msg);
47 return $; // return
48 };
49 };
50
51}).call(this);